|
1 | 1 | # Ginkelsoft DataTables |
2 | 2 |
|
3 | | -Ginkelsoft DataTables is a flexible and easy-to-use package for managing tabular data in your Laravel projects. It supports both a **base DataTable** class for traditional server-rendered apps and an **optional Livewire** component for dynamic, AJAX-driven experiences. You can easily add filtering, searching, sorting, and bulk actions with minimal setup. |
| 3 | +Ginkelsoft DataTables is a flexible and easy-to-use package for managing tabular data in Laravel projects. It supports both a **base DataTable** class for traditional server-rendered apps and an **optional Livewire** component for dynamic, AJAX-driven experiences. You can easily add filtering, searching, sorting, and bulk actions with minimal setup. |
4 | 4 |
|
5 | 5 | --- |
6 | 6 |
|
7 | 7 | ## Table of Contents |
8 | 8 |
|
9 | 9 | 1. [Requirements](#requirements) |
10 | 10 | 2. [Installation](#installation) |
11 | | -3. [Usage (Without Livewire)](#usage-without-livewire) |
12 | | -4. [Usage (With Livewire)](#usage-with-livewire) |
13 | | -5. [Working with Actions](#working-with-actions) |
14 | | -6. [Additional Features](#additional-features) |
15 | | -7. [Contributing](#contributing) |
16 | | -8. [License](#license) |
| 11 | +3. Usage |
| 12 | +4. Usage With Livewire |
| 13 | +5. [Filters](#filters) |
| 14 | +6. [Actions & Bulk Actions](#actions--bulk-actions) |
| 15 | +7. [Additional Features](#additional-features) |
| 16 | +8. [Contributing](#contributing) |
| 17 | +9. [License](#license) |
17 | 18 |
|
18 | 19 | --- |
19 | 20 |
|
@@ -41,72 +42,131 @@ Ginkelsoft DataTables is a flexible and easy-to-use package for managing tabular |
41 | 42 |
|
42 | 43 | --- |
43 | 44 |
|
44 | | -## Usage (With Livewire) |
| 45 | +## Usage |
| 46 | + |
| 47 | +You can use this package in a standard Laravel Blade view without Livewire. |
| 48 | + |
| 49 | +### Example |
| 50 | + |
| 51 | +#### Controller: |
| 52 | + |
| 53 | +```php |
| 54 | +use Ginkelsoft\DataTables\DataTable; |
| 55 | +use Ginkelsoft\DataTables\Column; |
| 56 | +use Ginkelsoft\DataTables\Filters\TextFilter; |
| 57 | +use Ginkelsoft\DataTables\Filters\SelectFilter; |
| 58 | +use Ginkelsoft\DataTables\Filters\DateFilter; |
| 59 | +use Ginkelsoft\DataTables\Filters\BooleanFilter; |
| 60 | +use App\Models\User; |
| 61 | + |
| 62 | +public function index() |
| 63 | +{ |
| 64 | + $query = User::query(); |
| 65 | + |
| 66 | + $datatable = (new DataTable($query)) |
| 67 | + ->setColumns([ |
| 68 | + Column::make('id', 'ID'), |
| 69 | + Column::make('name', 'Name'), |
| 70 | + Column::make('email', 'Email'), |
| 71 | + ]) |
| 72 | + ->setFilters([ |
| 73 | + new TextFilter('name'), |
| 74 | + new SelectFilter('role', null, ['admin' => 'Admin', 'user' => 'User']), |
| 75 | + new DateFilter('created_at'), |
| 76 | + new BooleanFilter('active') |
| 77 | + ]) |
| 78 | + ->setPerPage(10); |
| 79 | + |
| 80 | + $rows = $datatable->getRows(); |
| 81 | + |
| 82 | + return view('users.index', compact('rows')); |
| 83 | +} |
| 84 | +``` |
45 | 85 |
|
46 | | -If you prefer an **AJAX-driven** workflow with real-time sorting, searching, and pagination: |
| 86 | +#### Blade Template: |
47 | 87 |
|
48 | 88 | ```blade |
49 | | -<livewire:datatable |
50 | | - model="App\\Models\\User" |
51 | | - :columns="['id', 'name', 'email']" |
52 | | - :actions="[ |
53 | | - ['label' => 'Edit', 'route' => 'users.edit', 'class' => 'bg-blue-500 hover:bg-blue-600 text-white px-2 py-1 rounded', 'style' => 'margin-right: 5px;'], |
54 | | - ['label' => 'Delete', 'route' => 'users.destroy', 'class' => 'bg-red-500 hover:bg-red-600 text-white px-2 py-1 rounded'] |
55 | | - ]" |
56 | | - :bulkActions="[ |
57 | | - 'delete' => ['label' => 'Delete', 'route' => 'users.bulk.delete'], |
58 | | - 'export' => ['label' => 'Export', 'route' => 'users.bulk.export'] |
59 | | - ]" |
60 | | -/> |
| 89 | +<table> |
| 90 | + <thead> |
| 91 | + <tr> |
| 92 | + <th>ID</th> |
| 93 | + <th>Name</th> |
| 94 | + <th>Email</th> |
| 95 | + </tr> |
| 96 | + </thead> |
| 97 | + <tbody> |
| 98 | + @foreach($rows as $row) |
| 99 | + <tr> |
| 100 | + <td>{{ $row->id }}</td> |
| 101 | + <td>{{ $row->name }}</td> |
| 102 | + <td>{{ $row->email }}</td> |
| 103 | + </tr> |
| 104 | + @endforeach |
| 105 | + </tbody> |
| 106 | +</table> |
| 107 | +
|
| 108 | +{{ $rows->links() }} |
61 | 109 | ``` |
62 | 110 |
|
63 | | -You can now: |
| 111 | +### Actions & Bulk Actions |
64 | 112 |
|
65 | | -- **Search** by typing in the search field (if your component includes that feature). |
66 | | -- **Sort** by clicking column headers. |
67 | | -- **Paginate** without page reload. |
68 | | -- **Select rows** individually or choose to select all. |
| 113 | +#### Row Actions: |
69 | 114 |
|
70 | | ---- |
71 | | - |
72 | | -## Working with Actions |
73 | | - |
74 | | -### Row Actions with Custom Attributes |
| 115 | +```blade |
| 116 | +@foreach($rows as $row) |
| 117 | + <a href="{{ route('users.edit', $row->id) }}" class="bg-blue-500 text-white px-3 py-1 rounded">Edit</a> |
| 118 | + <a href="{{ route('users.destroy', $row->id) }}" class="bg-red-500 text-white px-3 py-1 rounded">Delete</a> |
| 119 | +@endforeach |
| 120 | +``` |
75 | 121 |
|
76 | | -You can now add **custom CSS classes and HTML attributes** to actions: |
| 122 | +#### Bulk Actions: |
77 | 123 |
|
78 | 124 | ```blade |
79 | | -:actions="[ |
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?\')']] |
82 | | -]" |
| 125 | +<form action="{{ route('users.bulk.delete') }}" method="POST"> |
| 126 | + @csrf |
| 127 | + <input type="hidden" name="ids" value="{{ implode(',', $selectedRows) }}"> |
| 128 | + <button type="submit" class="bg-red-500 text-white px-3 py-1 rounded">Delete Selected</button> |
| 129 | +</form> |
83 | 130 | ``` |
84 | 131 |
|
85 | | -These actions will be rendered as buttons with the specified styles. |
| 132 | +--- |
86 | 133 |
|
87 | | -### Bulk Actions |
| 134 | +## Usage With Livewire |
88 | 135 |
|
89 | | -For bulk actions (like deleting multiple rows), define them as follows: |
| 136 | +For an **AJAX-driven** workflow with real-time sorting, searching, and pagination: |
90 | 137 |
|
91 | 138 | ```blade |
92 | | -:bulkActions="[ |
93 | | - 'delete' => ['label' => 'Delete', 'route' => 'users.bulk.delete'], |
94 | | - 'export' => ['label' => 'Export', 'route' => 'users.bulk.export'] |
95 | | -]" |
| 139 | +<livewire:datatable |
| 140 | + model="App\\Models\\User" |
| 141 | + :columns="['id', 'name', 'email']" |
| 142 | + :actions="[ |
| 143 | + ['label' => 'Edit', 'route' => 'users.edit'], |
| 144 | + ['label' => 'Delete', 'route' => 'users.destroy'] |
| 145 | + ]" |
| 146 | + :bulkActions="[ |
| 147 | + 'delete' => ['label' => 'Delete', 'route' => 'users.bulk.delete'], |
| 148 | + 'export' => ['label' => 'Export', 'route' => 'users.bulk.export'] |
| 149 | + ]" |
| 150 | + :filters="[ |
| 151 | + ['column' => 'name', 'type' => 'text'], |
| 152 | + ['column' => 'role', 'type' => 'select', 'options' => ['admin' => 'Admin', 'user' => 'User']], |
| 153 | + ['column' => 'created_at', 'type' => 'date'], |
| 154 | + ['column' => 'active', 'type' => 'boolean'] |
| 155 | + ]" |
| 156 | +/> |
96 | 157 | ``` |
97 | 158 |
|
98 | | -When multiple rows are selected, the component redirects to the specified route with `ids` in the query/string, so you can handle them in your controller. |
99 | | - |
100 | 159 | --- |
101 | 160 |
|
102 | 161 | ## Additional Features |
103 | 162 |
|
| 163 | +- **Multi-language Support:** English, Dutch, German, French, Spanish. |
104 | 164 | - **Search Class** for multi-column searching. |
105 | 165 | - **Filter Class** for custom filters (status, categories, etc.). |
106 | 166 | - **Sorting Class** for ascending/descending ordering. |
107 | 167 | - **Select All** (with confirmation modal) to choose between only visible rows or all rows. |
108 | | -- **Custom Actions** now support custom classes and inline styles. |
109 | | -- **Prevent row selection when clicking an action button**, ensuring that clicking an action does not check the row checkbox. |
| 168 | +- **Custom Actions** now support classes and inline styles. |
| 169 | +- **Prevent row selection when clicking an action button** to avoid accidental selection. |
110 | 170 |
|
111 | 171 | --- |
112 | 172 |
|
|
0 commit comments