Tree table

The tree table supports pagination and click-to-load functionality and is particularly suitable for displaying large amounts of data in a multi-level structure.

Tree table - 图1

Table structures and models

To use a tree table, follow the agreed table structure:

{tip} This table structure and model are fully compatible with model-tree .

  1. CREATE TABLE `demo_categories` (
  2. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  3. `parent_id` int(11) NOT NULL DEFAULT '0',
  4. `order` int(11) NOT NULL DEFAULT '0', // order 字段不是必须的,不设置也可以
  5. `title` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  6. `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  7. `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  8. PRIMARY KEY (`id`)
  9. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

The above table structure has three required fields inside parent_id, title, the other fields are not required.

The corresponding model is app/Models/Category.php:

{tip} For ease of reading, the Repository code is no longer shown here.

  1. <?php
  2. namespace App\Models\Demo;
  3. use Dcat\Admin\Traits\ModelTree;
  4. use Illuminate\Database\Eloquent\Model;
  5. class Category extends Model
  6. {
  7. use ModelTree;
  8. protected $table = 'demo_categories';
  9. }

The names of the three fields parent_id, order and title in the table structure can also be modified:

  1. <?php
  2. namespace App\Models\Demo;
  3. use Dcat\Admin\Traits\ModelTree;
  4. use Illuminate\Database\Eloquent\Model;
  5. class Category extends Model
  6. {
  7. use ModelTree;
  8. protected $table = 'demo_categories';
  9. protected $titleColumn = 'name';
  10. protected $orderColumn = 'sort';
  11. protected $parentColumn = 'pid';
  12. }

If your data table does not require the order field to be sorted, add the following code to the model

  1. <?php
  2. namespace App\Models\Demo;
  3. use Dcat\Admin\Traits\ModelTree;
  4. use Illuminate\Database\Eloquent\Model;
  5. class Category extends Model
  6. {
  7. use ModelTree;
  8. protected $table = 'demo_categories';
  9. // Disable the order field by returning a null value.
  10. public function getOrderColumn()
  11. {
  12. return null;
  13. }
  14. }

Use

The tree table function can be enabled by calling Grid\Column::tree method, after that, only the data of the top node will be queried by default.

  1. <?php
  2. namespace App\Admin\Controllers\Demo;
  3. use App\Models\Category;
  4. use Dcat\Admin\Grid;
  5. use Dcat\Admin\Controllers\AdminController;
  6. class CategoryController extends AdminController
  7. {
  8. protected function grid()
  9. {
  10. return Grid::make(new Category(), function (Grid $grid) {
  11. $grid->id('ID')->bold()->sortable();
  12. $grid->title->tree(); // Enable the tree table feature
  13. $grid->order;
  14. $grid->created_at;
  15. $grid->updated_at->sortable();
  16. $grid->filter(function (Grid\Filter $filter) {
  17. $filter->like('slug');
  18. $filter->like('name');
  19. $filter->like('http_path');
  20. });
  21. });
  22. }
  23. }

The above code executes sql as follows (by default, only data with parent = 0 is queried):

  1. select count(*) as aggregate from `demo_categories` where `parent_id` = 0
  2. select * from `demo_categories` where `parent_id` = 0 order by `order` asc, `id` asc limit 20 offset 0

Grid\Column::tree Method parameters

  • bool $showAll false Whether to display all the nodes of the next level at once, the default pagination display
  • bool $sortable true Whether to enable the sorting function
  1. // Disable paging and load all next level nodes at once.
  2. $grid->title->tree(true);
  3. // No need to sort the order field, pass false for the second argument
  4. $grid->title->tree(false, false);

orderable排序

The orderable sorting function relies on the spatie/eloquent-sortable component, which requires modifying the model:

  1. use Spatie\EloquentSortable\Sortable;
  2. class Category extends Model implements Sortable
  3. {
  4. use ModelTree;
  5. // Set the sort field, default order
  6. protected $orderColumn = 'sort';
  7. }

Here’s how to use Example

  1. class CategoryController extends AdminController
  2. {
  3. protected function grid()
  4. {
  5. return Grid::make(new Category(), function (Grid $grid) {
  6. $grid->id('ID')->bold()->sortable();
  7. $grid->title->tree(); // Enable the tree table feature
  8. $grid->order->orderable(); // Enable sorting function
  9. ...;
  10. });
  11. }
  12. }

About Data Search

If the search function is used in the tree table (Grid::filter, Grid\Column::filter, Grid::quickSearch), it cancels the operation of searching only the top-level data in order to allow the user to search for the desired data.

{tip} Using search functions such as query filters, column filters, quick-search all cancel the operation of searching only the top-level data. with the exception of features such as filter and scope query scope.

For example, the following code enables quick search

  1. class CategoryController extends AdminController
  2. {
  3. protected function grid()
  4. {
  5. return Grid::make(new Category(), function (Grid $grid) {
  6. $grid->id('ID')->bold()->sortable();
  7. $grid->title->tree(); // Enable the tree table feature
  8. $grid->order->orderable(); // Enable sorting function
  9. $grid->quickSearch(['id', 'title']);
  10. ...;
  11. });
  12. }
  13. }

and the user uses a quick search in the browser, then sql is generated as follows

  1. select count(*) as aggregate from `demo_categories` where `id` like "%xxx%" or `title` like "%xxx%"
  2. select * from `demo_categories` where `id` like "%xxx%" or `title` like "%xxx%" order by `order` asc, `id` asc limit 20 offset 0

Difference from model tree function

The model-tree can also be used to display multi-level structured data, and support drag-and-drop data hierarchy, sorting, and other operations, but does not support paging and click to load the function, can only load all the data at once. Therefore, model-tree is not suitable for displaying data with large amounts of data.

The tree table supports pagination and click to load, suitable for displaying large amounts of data multi-level structured data, but does not support the use of drag and drop to achieve the data hierarchy, sorting operations.