Basic use of columns

Set columns to be sortable

  1. $grid->column('id')->sortable();

Form fields support sorting of relational table fields and json fields

Note that associative relationships only support hasOne and belongsTo types of field sorting, and do not support multiple levels of nesting!

  1. // Sorting fields in the association table
  2. $grid->column('profile.age')->sortable();
  3. // Specify the name of the field to be sorted
  4. $grid->column('my_age')->sortable('profile.age');
  5. // json field sorting
  6. $grid->column('options.price')->sortable('options->price');
  7. // Sort the json fields of the association table
  8. $grid->column('profile.options.price')->sortable('profile.options->price');

Support MySql order by cast(`{field}` as {type}) usage

  1. $grid->column('profile.age')->sortable(null, 'SIGNED');
  2. $grid->column('profile.options.price')->sortable('profile.options->price', 'SIGNED');

set default sort

  1. $grid->model()->orderBy('id', 'desc');

This function also supports sorting fields in the association table, note that only one to one and one to many associations are supported here

  1. $grid->model()->orderBy('profile.age');

Set the width of the column (width)

Set the column width, which can be used to limit the column width when the field is too long

  1. // px
  2. $grid->column('long_text')->width('300px');
  3. // percentage
  4. $grid->column('long_text')->width('15%');

fixColumns

Through fixColumns method, you can set fixed columns for the table, the first parameter is to fix the first three columns from the beginning, the second parameter is to fix two columns from the back to the front, (the second parameter can not be passed, the default is -1).

  1. $grid->fixColumns(2, -2);

result

Basic use of columns - 图1

Get the row number (index)

The number is counted from 0.

  1. // Use in the display callback
  2. $grid->column('number')->display(function () {
  3. return $this->_index + 1;
  4. });
  5. // used in a row action
  6. $grid->actions(function ($actions) {
  7. $index = $this->_index;
  8. ...
  9. });

Set td tag HTML attributes (setAttributes)

  1. $grid->column('email')->setAttributes(['name' => '...'])

Set Table Header HTML Attributes (setHeaderAttributes)

Set the html attribute of TITLE

  1. // Change Color
  2. $grid->column('name')->setHeaderAttributes(['style' => 'color:#5b69bc']);

Set the column selector (field show or hide showColumnSelector)

This feature is not enabled by default.

  1. // Enables field selector function
  2. $grid->showColumnSelector();
  3. // Set default hidden fields
  4. $grid->hideColumns(['field1', ...]);

Basic use of columns - 图2

</a

Storage driver (persistent)

In the configuration file config/admin.php you can configure the way to store the column selector state, the supported storage methods are as follows

-### Set column prompt message

  • Dcat\Admin\Grid\ColumnSelector\SessionStore Column selector state data is stored in session, valid only in login state
  • Dcat\Admin\Grid\ColumnSelector\CacheStore\ Column selector state data is stored in [Laravel Cache](https://laravel.com/docs/8.x/cache#driver-prerequisites) cache system for up to300days and can be configured withadmin.grid.column_selector.store_params.driverwhich defaults tofile`
  1. 'grid' => [
  2. ...
  3. 'column_selector' => [
  4. 'store' => Dcat\Admin\Grid\ColumnSelector\SessionStore::class,
  5. 'store_params' => [
  6. 'driver' => 'file',
  7. ],
  8. ],
  9. ],

Set column prompt message (help)

Grid\Column::help parameters.

  • $help string prompt content
  • $style string The background color of the prompt, supports green, blue, red, purple.
  • $placement string The position of the prompt, supports top, left, right, bottom

Basic use of columns - 图3

  1. $grid->column('id')->help('tip');

Set column search

The Grid\Column::filter method allows you to set a filter for a column, which makes it easy to filter the data table by that column. Refer to column filter for the detailed usage.

Basic use of columns - 图4

Extended column function

Column methods can be extended by the Grid\Column::macro method.

Add the following code to app/Admin/bootstrap.php:

  1. use Dcat\Admin\Grid;
  2. // $value is the value of the current field
  3. // $p1、$p2 are custom parameters
  4. Grid\Column::macro('myHeader', function ($value, $p1, $p2 = null) {
  5. // MyHeader needs to implement the Illuminate\Contracts\Support\Renderable interface
  6. // Of course, you can also pass a string here
  7. return $this->addHeader(new MyHeader($this, $p1, $p2));
  8. });

The MyHeader class

  1. use Dcat\Admin\Grid\Column;
  2. use Illuminate\Contracts\Support\Renderable;
  3. class MyHeader implements Renderable
  4. {
  5. public function __construct(Column $column, $p1, $p2)
  6. {
  7. ...
  8. }
  9. public function render()
  10. {
  11. ...
  12. }
  13. }

use

  1. $grid->column('user')->myHeader($p1, $p2);
  2. $grid->column('first_name')->myHeader($p1);