Skip to content

Commit

Permalink
detailed action btn documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
LeMatosDeFuk committed Dec 13, 2023
1 parent 353db7d commit 31fc6af
Showing 1 changed file with 57 additions and 34 deletions.
91 changes: 57 additions & 34 deletions docs/action-btn.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# LaraGrid Action Buttons

Action buttons in LaraGrid are used to add interactive buttons to each row in the grid. These buttons can be used to perform actions related to the corresponding record, such as viewing details, editing, or deleting the record.
# LaraGrid Action Button Class

The `ActionButton` class is a part of the LaraGrid package. It is used to define an interactive button in the grid. Each instance of the `ActionButton` class represents an action button in the grid.

## Creating an Action Button

To create an action button, you use the `make` method of the `ActionButton` class. This static method creates a new instance of the `ActionButton` class. It takes one argument: the label.
To create an action button, you use the `make` method. This static method creates a new instance of the `ActionButton` class. It takes one argument: the label.

```php
ActionButton::make('View')
Expand All @@ -14,55 +15,77 @@ ActionButton::make('View')

The `ActionButton` class provides several methods to manipulate an action button in the grid.

### `setRenderer(Closure $renderer)`
### `setRenderer(callable $renderer): static`

This method sets a closure that returns the HTML for the action button. The closure receives the model instance for the current row as an argument.
This method sets the renderer for the action button. You can pass a callable, a class method, or a string.

```php
ActionButton::make('View')->setRenderer(function (Model $model) {
return '<a href="' . route('detail', $model->id) . '">View</a>';
}),
public function setRenderer(callable $renderer): static
```

### `setAttributes(Closure $attributes)`

This method sets a closure that returns an array of HTML attributes for the action button. The closure receives the model instance for the current row as an argument.
```php
ActionButton::make('View')
->setRenderer(function (Model $model) {
return "<span class='bg-blue-200'>View</span>";
})
->setRenderer(fn(Model $model) => view('test'))
->setRenderer([MyClass::class, 'myMethod'])
->setRenderer('Random Text')
```

```php
ActionButton::make('View')->setAttributes([
'target' => '_blank',
'href' => 'https://google.com'
'class' => 'btn btn-primary'
// etc...
]),
class MyClass
{
public static function myMethod(Model $model)
{
return '<a href="' . route('detail', $model->id) . '">View</a>';
}
}
```

### `setColumnTag(string $columnTag)`
### `setAttributes(array $attributes): static`

This method sets the HTML tag for the action button.
This method sets the attributes of the action button. The attributes are HTML attributes that are applied to the action button tag. You can pass a callable, a class method, or an array.

```php
ActionButton::make('View')->setColumnTag('a'),
public function setAttributes(array $attributes): static
```

## Usage

The `ActionButton` class is used in the `getColumns` method of your Livewire component to define the action buttons that will be displayed in the grid.
```php
ActionButton::make('View')
->setAttributes(function (Model $model) {
return [
'target' => '_blank',
'href' => 'https://google.com',
'class' => 'btn btn-primary'
];
})
->setAttributes([MyClass::class, 'myMethod'])
->setAttributes(['class' => 'btn btn-primary'])
```

```php
protected function getColumns(): array
class MyClass
{
return [
Column::make('id', 'ID'),
Column::make('name', 'Name'),
ActionButton::make('View')->setRenderer(function (Model $model) {
return '<a href="' . route('detail', $model->id) . '">View</a>';
}),
// Add more columns as needed
];
public static function myMethod(Model $model)
{
return [
'target' => '_blank',
'href' => 'https://google.com',
'class' => 'btn btn-primary'
];
}
}
```

In this example, we have three columns: 'ID', 'Name', and an action button 'View'. The 'View' button links to the detail page of the corresponding record.
### `setColumnTag(string $columnTag): static`

This method sets the HTML tag for the action button.

```php
public function setColumnTag(string $columnTag): static
```

Remember, the `ActionButton` class extends the `BaseColumn` class, so it inherits all the methods and properties of the `BaseColumn` class. This includes methods for setting the renderer and attributes of the column, as well as the column tag.
```php
ActionButton::make('View')->setColumnTag('a'),
```

0 comments on commit 31fc6af

Please sign in to comment.