Skip to content

Commit 6d36b7f

Browse files
Actions are now flexible and customizable! (#7)
1 parent c4365e3 commit 6d36b7f

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ You can now:
7171

7272
## Working with Actions
7373

74-
### Row Actions
74+
### Row Actions with Custom Attributes
7575

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

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<div class="flex items-center space-x-2">
2-
@foreach($actions as $action)
3-
<a href="{{ route($action['route'], $row->id) }}"
4-
class="{{ $action['class'] ?? 'px-2 py-1 text-sm font-medium text-white bg-blue-500 rounded hover:bg-blue-600' }}"
5-
style="{{ $action['style'] ?? '' }}"
6-
onclick="event.stopPropagation();">
7-
{{ $action['label'] }}
8-
</a>
2+
@foreach($actions as $action)
3+
<a href="{{ route($action['route'], $row->id) }}"
4+
onclick="event.stopPropagation();"
5+
@foreach($action['attributes'] ?? [] as $key => $value)
6+
{{ $key }}="{{ $value }}"
97
@endforeach
8+
>
9+
{{ $action['label'] }}
10+
</a>
11+
@endforeach
1012
</div>

src/Action.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,40 @@
22

33
namespace Ginkelsoft\DataTables;
44

5-
/**
6-
* Class representing an action that can be performed on a DataTable row.
7-
*/
85
class Action
96
{
10-
/** @var string The name of the action */
117
public string $name;
12-
13-
/** @var string The label displayed for the action */
148
public string $label;
15-
16-
/** @var string The route associated with the action */
179
public string $route;
10+
public array $attributes = [];
1811

1912
/**
2013
* Action constructor.
2114
*
22-
* @param string $name The action name
23-
* @param string $label The action label
24-
* @param string $route The route associated with the action
15+
* @param string $name Unique action name.
16+
* @param string $label Button label.
17+
* @param string $route Named route for the action.
18+
* @param array $attributes Additional HTML attributes (e.g., classes, styles).
2519
*/
26-
public function __construct(string $name, string $label, string $route)
20+
public function __construct(string $name, string $label, string $route, array $attributes = [])
2721
{
2822
$this->name = $name;
2923
$this->label = $label;
3024
$this->route = $route;
25+
$this->attributes = $attributes;
3126
}
3227

3328
/**
34-
* Static method to create a new action instance.
29+
* Factory method to create an action instance.
3530
*
36-
* @param string $name The action name
37-
* @param string $label The action label
38-
* @param string $route The route associated with the action
39-
* @return self A new Action instance
31+
* @param string $name Unique action name.
32+
* @param string $label Button label.
33+
* @param string $route Named route for the action.
34+
* @param array $attributes Additional HTML attributes (e.g., classes, styles).
35+
* @return self
4036
*/
41-
public static function make(string $name, string $label, string $route): self
37+
public static function make(string $name, string $label, string $route, array $attributes = []): self
4238
{
43-
return new self($name, $label, $route);
39+
return new self($name, $label, $route, $attributes);
4440
}
4541
}

0 commit comments

Comments
 (0)