|
1 |
| -# A Table Component for Laravel Livewire |
| 1 | +# A dynamic table component for Laravel Livewire |
2 | 2 |
|
3 |
| -[](https://packagist.org/packages/rappasoft/livewire-tables) |
4 |
| - |
| 3 | +[](https://packagist.org/packages/rappasoft/laravel-livewire-tables) |
5 | 4 | [](https://github.styleci.io/repos/242222088)
|
6 |
| -[](https://scrutinizer-ci.com/g/rappasoft/livewire-tables/?branch=master) |
7 |
| -[](https://scrutinizer-ci.com/g/rappasoft/livewire-tables) |
8 |
| -[](https://packagist.org/packages/rappasoft/livewire-tables) |
| 5 | +[](https://packagist.org/packages/rappasoft/laravel-livewire-tables) |
9 | 6 |
|
10 |
| -A Laravel Livewire extension for data tables. |
| 7 | +A Laravel Livewire component for data tables. |
| 8 | + |
| 9 | +**This package is currently in development and the source is constantly changing, use at your own risk.** |
11 | 10 |
|
12 | 11 | ## Installation
|
13 | 12 |
|
14 | 13 | You can install the package via composer:
|
15 | 14 |
|
16 | 15 | ``` bash
|
17 |
| -composer require rappasoft/livewire-tables |
| 16 | +composer require rappasoft/laravel-livewire-tables |
18 | 17 | ```
|
19 | 18 |
|
20 |
| -## Version Compatibility |
| 19 | +## Usage |
21 | 20 |
|
22 |
| - Laravel | Livewire Tables |
23 |
| -:---------|:---------- |
24 |
| - 7.x | 1.x |
| 21 | +### Creating Tables |
25 | 22 |
|
26 |
| -## Publish |
| 23 | +To create a table component you can start with the below stub: |
27 | 24 |
|
28 |
| -You can publish the configuration file to override the defaults: |
| 25 | +``` |
| 26 | +<?php |
| 27 | +
|
| 28 | +namespace App\Http\Livewire; |
| 29 | +
|
| 30 | +use App\User; |
| 31 | +use Illuminate\Database\Eloquent\Builder; |
| 32 | +use Rappasoft\LivewireTables\Http\Livewire\Column; |
| 33 | +use Rappasoft\LivewireTables\Http\Livewire\TableComponent; |
| 34 | +
|
| 35 | +class UsersTable extends TableComponent |
| 36 | +{ |
| 37 | +
|
| 38 | + public function query() : Builder |
| 39 | + { |
| 40 | + return User::with('role') |
| 41 | + ->withCount('permissions'); |
| 42 | + } |
| 43 | +
|
| 44 | + public function columns() : array |
| 45 | + { |
| 46 | + return [ |
| 47 | + Column::make('ID') |
| 48 | + ->searchable() |
| 49 | + ->sortable(), |
| 50 | + Column::make('Name') |
| 51 | + ->searchable() |
| 52 | + ->sortable(), |
| 53 | + Column::make('E-mail', 'email') |
| 54 | + ->searchable() |
| 55 | + ->sortable(), |
| 56 | + Column::make('Role', 'role.name') |
| 57 | + ->searchable() |
| 58 | + ->sortable(), |
| 59 | + Column::make('Permissions', 'permissions_count') |
| 60 | + ->sortable(), |
| 61 | + Column::make('Actions') |
| 62 | + ->view('backend.auth.user.includes.actions'), |
| 63 | + ]; |
| 64 | + } |
| 65 | +} |
29 | 66 |
|
30 |
| -``` bash |
31 |
| -php artisan vendor:publish --provider="Rappasoft\LivewireTables\LivewireTablesServiceProvider" |
32 | 67 | ```
|
33 | 68 |
|
34 |
| -## Usage |
| 69 | +Your component must implement two methods: |
35 | 70 |
|
| 71 | +``` |
| 72 | +/** |
| 73 | + * This defines the start of the query, usually Model::query() but can also eagar load relationships and counts. |
| 74 | + */ |
| 75 | +public function query() : Builder; |
36 | 76 |
|
| 77 | +/** |
| 78 | + * This defines the columns of the table, they don't necessarily have to map to columns on the table. |
| 79 | + */ |
| 80 | +public function columns() : array; |
| 81 | +``` |
37 | 82 |
|
38 |
| -### Testing |
| 83 | +### Defining Columns |
39 | 84 |
|
40 |
| -``` bash |
41 |
| -composer test |
| 85 | +You can define the columns of your table with the column class: |
| 86 | + |
| 87 | +``` |
| 88 | +Column::make('Name', 'column_name') |
42 | 89 | ```
|
43 | 90 |
|
44 |
| -### Changelog |
| 91 | +The first parameter is the name of the table header. The second parameter is the name of the table column. You can leave blank and the lowercase snake_case version will be used by default. |
| 92 | + |
| 93 | +Here are a list of the column method you can chain to build your columns: |
| 94 | + |
| 95 | +``` |
| 96 | +/** |
| 97 | + * This column is searchable, with no callback it will search the column by name or by the supplied relationship, using a callback overrides the default searching functionality. |
| 98 | + */ |
| 99 | +public function searchable(callable $callable = null) : self; |
| 100 | +
|
| 101 | +/** |
| 102 | + * This column is sortable, with no callback it will sort the column by name and sort order defined on the components $sortDirection variable |
| 103 | + */ |
| 104 | +public function sortable(callable $callable = null) : self; |
| 105 | +
|
| 106 | +/** |
| 107 | + * The columns output will be put through {!! !!} instead of {{ }}. |
| 108 | + */ |
| 109 | +public function unescaped() : self; |
| 110 | +
|
| 111 | +/** |
| 112 | + * The columns output will be put through the Laravel HtmlString class. |
| 113 | + */ |
| 114 | +public function html() : self; |
| 115 | +
|
| 116 | +/** |
| 117 | + * This column will not look on the table for the column name, it will look on the model for the given attribute. Useful for custom attributes like getFullNameAttribute: Column::make('Full Name', 'full_name')->customAttribute() |
| 118 | + */ |
| 119 | +public function customAttribute() : self; |
| 120 | +
|
| 121 | +/** |
| 122 | + * This view will be used for the column, can still be used with sortable and searchable. |
| 123 | + */ |
| 124 | +public function view($view) : self; |
| 125 | +``` |
| 126 | + |
| 127 | +### Properties |
| 128 | + |
| 129 | +You can override any of these in your table component: |
| 130 | + |
| 131 | +#### Table |
| 132 | + |
| 133 | +| Property | Default | Usage | |
| 134 | +| -------- | ------- | ----- | |
| 135 | +| $tableHeaderEnabled | true | Whether or not to display the table header | |
| 136 | +| $tableFooterEnabled | false | Whether or not to display the table footer | |
| 137 | +| $tableClass | table table-striped | The class to set on the table | |
| 138 | +| $tableHeaderClass | *none* | The class to set on the thead of the table | |
| 139 | +| $tableFooterClass | *none* | The class to set on the tfoot of the table | |
| 140 | +| $responsive | table-responsive | Tables wrapping div class | |
| 141 | + |
| 142 | +#### Searching |
| 143 | + |
| 144 | +| Property | Default | Usage | |
| 145 | +| -------- | ------- | ----- | |
| 146 | +| $searchEnabled | true | Whether or not searching is enabled | |
| 147 | +| $searchDebounce | 350 | Amount of time in ms to wait to send the search query and refresh the table | |
| 148 | +| $disableSearchOnLoading | true | Whether or not to disable the search bar when it is searching/loading new data | |
| 149 | +| $search | *none* | The initial search string | |
| 150 | +| $searchLabel | Search... | The placeholder for the search box | |
| 151 | +| $noResultsMessage | There are no results to display for this query. | The message to display when there are no results | |
| 152 | + |
| 153 | +#### Sorting |
| 154 | + |
| 155 | +| Property | Default | Usage | |
| 156 | +| -------- | ------- | ----- | |
| 157 | +| $sortField | id | The initial field to be sorting by | |
| 158 | +| $sortDirection | asc | The initial direction to sort | |
| 159 | + |
| 160 | +#### Pagination |
| 161 | + |
| 162 | +| Property | Default | Usage | |
| 163 | +| -------- | ------- | ----- | |
| 164 | +| $paginationEnabled | true | Displays per page and pagination links | |
| 165 | +| $perPageOptions | [10, 25, 50] | The options to limit the amount of results per page | |
| 166 | +| $perPage | 25 | Amount of items to show per page | |
| 167 | +| $perPageLabel | Per Page | The label for the per page filter | |
| 168 | + |
| 169 | +#### Loading |
| 170 | + |
| 171 | +| Property | Default | Usage | |
| 172 | +| -------- | ------- | ----- | |
| 173 | +| $loadingIndicator | false | Whether or not to show a loading indicator when searching | |
| 174 | +| $loadingMessage | Loading... | The loading message that gets displayed | |
| 175 | + |
| 176 | +#### Offline |
| 177 | + |
| 178 | +| Property | Default | Usage | |
| 179 | +| -------- | ------- | ----- | |
| 180 | +| $offlineIndicator | true | Whether or not to display an offline message when there is no connection | |
| 181 | +| $offlineMessage | You are not currently connected to the internet. | The message to display when offline | |
| 182 | + |
| 183 | +#### Checkboxes |
| 184 | + |
| 185 | +| Property | Default | Usage | |
| 186 | +| -------- | ------- | ----- | |
| 187 | +| $checkbox | false | Whether or not checkboxes are enabled | |
| 188 | +| $checkboxLocation | left | The side to put the checkboxes on | |
| 189 | +| $checkboxAttribute | id | The model attribute to bind to the checkbox array | |
| 190 | +| $checkboxAll | false | Whether or not all checkboxes are currently selected | |
| 191 | +| $checkboxValues | [] | The currently selected values of the checkboxes | |
| 192 | + |
| 193 | +#### Other |
| 194 | + |
| 195 | +| Property | Default | Usage | |
| 196 | +| -------- | ------- | ----- | |
| 197 | +| $wrapperClass | *none* | The classes applied to the wrapper div | |
| 198 | +| $refresh | false | Whether or not to refresh the table at a certain interval. false = off, If it's an integer it will be treated as milliseconds (2000 = refresh every 2 seconds), If it's a string it will call that function every 5 seconds. |
| 199 | + |
| 200 | +### Table Methods |
| 201 | + |
| 202 | +``` |
| 203 | +/** |
| 204 | + * Used to set a class on a table header based on the column attribute |
| 205 | + */ |
| 206 | +public function setTableHeadClass($attribute) : ?string; |
| 207 | +
|
| 208 | +/** |
| 209 | + * Used to set a ID on a table header based on the column attribute |
| 210 | + */ |
| 211 | +public function setTableHeadId($attribute) : ?string; |
| 212 | +
|
| 213 | +/** |
| 214 | + * Used to set any attributes on a table header based on the column attribute |
| 215 | + * ['name' => 'my-custom-name', 'data-key' => 'my-custom-key'] |
| 216 | + */ |
| 217 | +public function setTableHeadAttributes($attribute) : array; |
| 218 | +
|
| 219 | +/** |
| 220 | + * Used to set a class on a table row |
| 221 | + * You have the entre model of the row to work with |
| 222 | + */ |
| 223 | +public function setTableRowClass($model) : ?string; |
| 224 | +
|
| 225 | +/** |
| 226 | + * Used to set a ID on a table row |
| 227 | + * You have the entre model of the row to work with |
| 228 | + */ |
| 229 | +public function setTableRowId($model) : ?string; |
| 230 | +
|
| 231 | +/** |
| 232 | + * Used to set any attribute on a table row |
| 233 | + * You have the entre model of the row to work with |
| 234 | + * ['name' => 'my-custom-name', 'data-key' => 'my-custom-key'] |
| 235 | + */ |
| 236 | +public function setTableRowAttributes($model) : array; |
| 237 | +
|
| 238 | +/** |
| 239 | + * Used to set the class of a table cell based on the column and the value of the cell |
| 240 | + */ |
| 241 | +public function setTableDataClass($attribute, $value) : ?string; |
| 242 | +
|
| 243 | +/** |
| 244 | + * Used to set the ID of a table cell based on the column and the value of the cell |
| 245 | + */ |
| 246 | +public function setTableDataId($attribute, $value) : ?string; |
| 247 | +
|
| 248 | +/** |
| 249 | + * Used to set any attributes of a table cell based on the column and the value of the cell |
| 250 | + * ['name' => 'my-custom-name', 'data-key' => 'my-custom-key'] |
| 251 | + */ |
| 252 | +public function setTableDataAttributes($attribute, $value) : array; |
| 253 | +``` |
| 254 | + |
| 255 | +## Inspiration From: |
| 256 | + |
| 257 | +- [https://github.com/kdion4891/laravel-livewire-tables](https://github.com/kdion4891/laravel-livewire-tables) |
| 258 | +- [https://github.com/yajra/laravel-datatables](https://github.com/yajra/laravel-datatables) |
| 259 | + |
| 260 | +## Changelog |
45 | 261 |
|
46 | 262 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
|
47 | 263 |
|
|
0 commit comments