Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ You can now:

## Working with Actions

### Row Actions
### Row Actions with Custom Attributes

Actions now support custom **classes** and **styles** for better UI customization:
You can now add **custom CSS classes and HTML attributes** to actions:

```blade
:actions="[
['label' => 'Edit', 'route' => 'users.edit', 'class' => 'bg-blue-500 hover:bg-blue-600 text-white px-2 py-1 rounded', 'style' => 'margin-right: 5px;'],
['label' => 'Delete', 'route' => 'users.destroy', 'class' => 'bg-red-500 hover:bg-red-600 text-white px-2 py-1 rounded']
['label' => 'Edit', 'route' => 'users.edit', 'attributes' => ['class' => 'bg-green-500 text-white px-3 py-1 rounded']],
['label' => 'Delete', 'route' => 'users.destroy', 'attributes' => ['class' => 'bg-red-500 text-white px-3 py-1 rounded', 'onclick' => 'return confirm(\'Are you sure?\')']]
]"
```

Expand Down
16 changes: 9 additions & 7 deletions resources/views/vendor/datatables/actions.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<div class="flex items-center space-x-2">
@foreach($actions as $action)
<a href="{{ route($action['route'], $row->id) }}"
class="{{ $action['class'] ?? 'px-2 py-1 text-sm font-medium text-white bg-blue-500 rounded hover:bg-blue-600' }}"
style="{{ $action['style'] ?? '' }}"
onclick="event.stopPropagation();">
{{ $action['label'] }}
</a>
@foreach($actions as $action)
<a href="{{ route($action['route'], $row->id) }}"
onclick="event.stopPropagation();"
@foreach($action['attributes'] ?? [] as $key => $value)
{{ $key }}="{{ $value }}"
@endforeach
>
{{ $action['label'] }}
</a>
@endforeach
</div>
34 changes: 15 additions & 19 deletions src/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,40 @@

namespace Ginkelsoft\DataTables;

/**
* Class representing an action that can be performed on a DataTable row.
*/
class Action
{
/** @var string The name of the action */
public string $name;

/** @var string The label displayed for the action */
public string $label;

/** @var string The route associated with the action */
public string $route;
public array $attributes = [];

/**
* Action constructor.
*
* @param string $name The action name
* @param string $label The action label
* @param string $route The route associated with the action
* @param string $name Unique action name.
* @param string $label Button label.
* @param string $route Named route for the action.
* @param array $attributes Additional HTML attributes (e.g., classes, styles).
*/
public function __construct(string $name, string $label, string $route)
public function __construct(string $name, string $label, string $route, array $attributes = [])
{
$this->name = $name;
$this->label = $label;
$this->route = $route;
$this->attributes = $attributes;
}

/**
* Static method to create a new action instance.
* Factory method to create an action instance.
*
* @param string $name The action name
* @param string $label The action label
* @param string $route The route associated with the action
* @return self A new Action instance
* @param string $name Unique action name.
* @param string $label Button label.
* @param string $route Named route for the action.
* @param array $attributes Additional HTML attributes (e.g., classes, styles).
* @return self
*/
public static function make(string $name, string $label, string $route): self
public static function make(string $name, string $label, string $route, array $attributes = []): self
{
return new self($name, $label, $route);
return new self($name, $label, $route, $attributes);
}
}