tableOptions
A “Select columns” button with a side modal where the user shows, hides and reorders the columns of an index table. The choice is submitted to your endpoint, stored per user and applied to the table in the browser.
Signature
Section titled “Signature”Lte3::tableOptions(array $columns, array $options = [], array $attrs = [])$columns— the column definitions.$options— the saved choice of the current user for this table.
How it fits together
Section titled “How it fits together”- Describe the columns once and pass them, with the saved options, both to
Lte3::tableOptions()and to the<table>asdata-columns/data-options. - Mark every
<th>and<td>of a column with the classjs-table-options-{key}. - On page load the package JS hides and reorders the cells according to the options.
- The modal form submits the new choice to
action; your endpoint stores it and redirects back.
Basic example
Section titled “Basic example”@php $columns = [ ['key' => 'id', 'name' => 'ID'], ['key' => 'name', 'name' => 'Name'], ['key' => 'email', 'name' => 'Email'], ['key' => 'created_at', 'name' => 'Created', 'default' => false], ['key' => 'actions', 'name' => 'Actions', 'hidden' => true], ]; $tableOptions = auth()->user()->table_options['users'] ?? [];@endphp
<div class="card"> <div class="card-body table-responsive p-0"> <table class="table table-hover" data-columns='@json($columns)' data-options='@json($tableOptions)'> <thead> <tr> <th class="js-table-options-id">ID</th> <th class="js-table-options-name">Name</th> <th class="js-table-options-email">Email</th> <th class="js-table-options-created_at">Created</th> <th class="js-table-options-actions"></th> </tr> </thead> <tbody> @foreach($users as $user) <tr> <td class="js-table-options-id">{{ $user->id }}</td> <td class="js-table-options-name">{{ $user->name }}</td> <td class="js-table-options-email">{{ $user->email }}</td> <td class="js-table-options-created_at">{{ $user->created_at }}</td> <td class="js-table-options-actions">...</td> </tr> @endforeach </tbody> </table> </div>
{!! Lte3::tableOptions($columns, $tableOptions, [ 'action' => route('admin.table-options', 'users'), 'table' => 'users', 'name' => 'options', 'btn_modal_title' => 'Columns', 'btn_save_title' => 'Save', 'btn_reset_title' => 'Reset', ]) !!}</div>Columns
Section titled “Columns”| Key | Type | Default | Description |
|---|---|---|---|
key | string | - | Column key, used in the cell class js-table-options-{key} and in the submitted names. |
name | string | - | Title in the modal. |
default | bool | true | Visibility when the user has no saved options for this column. |
hidden | bool | false | The column is always hidden and not listed in the modal. |
Options
Section titled “Options”The saved choice, keyed by column key:
[ 'name' => ['active' => '1', 'weight' => '0'], 'id' => ['active' => '1', 'weight' => '1'], 'email' => ['active' => '0', 'weight' => '2'],]active—"1"shown,"0"hidden. The browser compares with the string"0", so keep the values as strings as they come from the request (anarray/jsoncast does that); an integer0does not hide the column.weight— position; columns are sorted by it in the modal and in the table.- Columns missing from the options are shown or hidden by their
default; options for unknown orhiddencolumns are ignored.
| Attr | Type | Default | Description |
|---|---|---|---|
action | string | # | URL the modal form is submitted to. |
method | string | POST | GET (uppercase) submits by GET, any other value by POST. |
table | string | '' | Table key, the second level of the submitted names. Set it: an empty key breaks the names. |
name | string | options | Root name of the submitted array. |
btn_modal_title | string | Select columns | Text of the open button and the modal title (HTML). |
btn_save_title | string | Submit | Text of the save button. |
btn_reset_title | string | Reset | Text of the reset button. |
preloader | bool | true | Renders an .overlay with a spinner that hides the table until the columns are applied. |
preloader_id | string | table-preloader | id of the overlay. The package JS fades out only #table-preloader. |
What is submitted
Section titled “What is submitted”Save button:
options[users][name][active]=1options[users][name][weight]=0options[users][id][active]=1options[users][id][weight]=1options[users][email][active]=0options[users][email][weight]=2activecomes from a switch (checkbox with a hidden0), so it is always0or1.weightis updated when the user drags the rows in the modal.- Columns with
hiddenare not submitted.
Reset button: submits options=[], i.e. the string "[]" instead of the array. Treat a non-array value as “forget the saved options”.
public function tableOptions(Request $request, string $table){ $user = $request->user(); $all = $user->table_options ?? []; $options = $request->input('options');
if (is_array($options)) { $all[$table] = $options[$table] ?? []; } else { unset($all[$table]); }
$user->update(['table_options' => $all]);
return back();}Placement
Section titled “Placement”- The component renders its own
<form>(throughLte3::formOpen()), so put it outside other forms;formClose()also ends the model binding of an outerformOpen(). - With
preloader, put it inside the same.cardas the table: the AdminLTE.overlaycovers its closest positioned parent. - The modal id is fixed (
#table__options-modal) and the JS works with the first.tableelement on the page, so use one configured table per page and make it the first.table.
JS behaviour
Section titled “JS behaviour”- On page load the package JS reads
data-columnsanddata-optionsof the first.table, moves thejs-table-options-{key}cells of every row into the saved order, addsd-noneto inactive andhiddencolumns, then fades out#table-preloader. - In the modal, rows are sortable (
.sortable-ywithdata-input-weight-class="js-input-weight"): after a drag theweightinputs are renumbered, seeinitSortableYin JavaScript API.
Related
Section titled “Related”- checkbox — the switch used for each column.
- Request options — other per-user list settings (
per_page).