diff --git a/docs/action-btn.md b/docs/action-btn.md index db566d5..817f603 100644 --- a/docs/action-btn.md +++ b/docs/action-btn.md @@ -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') @@ -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 'View'; -}), +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 "View"; + }) + ->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 'View'; + } +} ``` -### `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 'View'; - }), - // 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. \ No newline at end of file +```php +ActionButton::make('View')->setColumnTag('a'), +```