Data table events
Initialization
The Grid::resolving
method allows you to listen for table initialization events.
The developer can change some of the settings or behavior of Grid
in both events, for example, to disable certain actions, by adding the following code to app/Admin/bootstrap.php
.
use Dcat\Admin\Grid;
Grid::resolving(function (Grid $grid) {
$grid->disableActions();
$grid->disablePagination();
$grid->disableCreateButton();
$grid->disableFilter();
$grid->disableRowSelector();
$grid->disableTools();
$grid->disableExport();
});
// Only need to listen once.
Grid::resolving(function (Grid $grid) {
...
}, true);
This way you don't have to set it in the code of each controller.
If, after the global setting, you want to turn on the setting in one of the tables, for example to turn on the display action column, call $grid->disableActions(false);
on the corresponding instance.
Building
The Grid::composing
method allows you to listen for events when the table is called.
Grid::composing(function (Grid $grid) {
...
});
// You only need to listen in once.
Grid::composing(function (Grid $grid) {
...
}, true);
Fetching
Listen for the event before the form gets data, which is triggered after the composing
event.
$grid->listen(Grid\Events\Fetching::class, function ($grid) {
});
// can be used in the composing event.
Grid::composing(function (Grid $grid) {
$grid->listen(Grid\Events\Fetching::class, function ($grid) {
...
});
});
Fetched
Listen to events after the table has been retrieved. By listening to this event, you can modify data in bulk, see the following example
$grid->listen(Grid\Events\Fetched::class, function ($grid, Collection $rows) {
// $collection This is the current model collection of table data, which you can read or modify according to your needs.
$rows->transform(function ($row) {
// Change row data
$row['name'] = $row['first_name'].' '.$row['last_name'];
return $row;
});
});
ApplyFilter
listens for form filter search events, which are triggered only when the filter has search criteria
$grid->listen(Grid\Events\ApplyFilter::class, function ($grid, array $conditions) {
// $conditions Array of search criteria generated by the current filter
dd('Form filter', $conditions);
});
ApplyQuickSearch
listens for the form quick search event, which is triggered only when there is a value in the quick search input box
$grid->listen(Grid\Events\ApplyQuickSearch::class, function ($grid, $input) {
// $input Search keywords
dd('Quick Search', $input);
});
ApplySelector
listens for form specification filter events, which are triggered only when the specification filter selects an option
$grid->listen(Grid\Events\ApplySelector::class, function ($grid, array $input) {
// $input Array of options selected by the filter
dd('Form specification filters', $input);
});
rows callbacks
The Grid::rows
method allows you to listen for events after the table gets data.
use Dcat\Admin\Grid\Row;
use Illuminate\Support\Collection;
$grid->rows(function (Collection $rows) {
/**
* Get the first line of data
*
* @var Row $firstRow
*/
$firstRow = $rows->first();
// Set tr html attribute.
$firstRow->setAttributes(['name' => '....']);
if ($firstRow) {
// Get the id of the first line
$id = $firstRow->id;
// to arrays
$row = $firstRow->toArray();
}
});