Model tree

This feature implements a tree component that can be dragged and dropped to achieve data hierarchy, sorting, and other operations, and here is the basic Usage.

Model tree - 图1

Table structures and models

To use model-tree, follow the agreed table structure:

{tip} The parent_id field must default to 0!!!!

  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',
  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, order, title, the other fields are not required.

The corresponding models areapp/Models/Category.php:

  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:

{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. // Parent ID field name, default value is parent_id
  10. protected $parentColumn = 'pid';
  11. // Sort by field name, default value is order
  12. protected $orderColumn = 'sort';
  13. // TITLE field name, default value is title
  14. protected $titleColumn = 'name';
  15. // Since v2.1.6-beta,Defining the depthColumn property will save the current row's hierarchy in the data table
  16. protected $depthColumn = 'depth';
  17. }

Methods of use

Then it’s time to use model-tree on the page:

  1. <?php
  2. namespace App\Admin\Controllers\Demo;
  3. use App\Models\Category;
  4. use Dcat\Admin\Layout\Row;
  5. use Dcat\Admin\Layout\Content;
  6. use Dcat\Admin\Tree;
  7. use Dcat\Admin\Controllers\AdminController;
  8. class CategoryController extends AdminController
  9. {
  10. public function index(Content $content)
  11. {
  12. return $content->header('树状模型')
  13. ->body(function (Row $row) {
  14. $tree = new Tree(new Category);
  15. $row->column(12, $tree);
  16. });
  17. }
  18. }

The display of row data can be modified in the following ways:

  1. $tree = new Tree(new Category);
  2. $tree->branch(function ($branch) {
  3. $src = config('admin.upload.host') . '/' . $branch['logo'] ;
  4. $logo = "<img src='$src' style='max-width:30px;max-height:30px' class='img'/>";
  5. return "{$branch['id']} - {$branch['title']} $logo";
  6. });

The string type data returned in the callback function is what is displayed for each row in the tree component, and the $branch parameter is an array of data for the current row.

Modify Model Lookup Criteria

If you want to modify the model’s query, use the following

  1. $tree->query(function ($model) {
  2. return $model->where('type', 1);
  3. });

Limit the maximum number of levels

Default 5

  1. $tree->maxDepth(3);

Custom line operations

  1. use Dcat\Admin\Tree;
  2. $tree->actions(function (Tree\Actions $actions) {
  3. if ($actions->row->id > 5) {
  4. $actions->disableDelete(); // Disable the delete button.
  5. }
  6. // Add a new action
  7. $actions->append(...);
  8. });
  9. // Batch add action
  10. $tree->actions([
  11. new Action1(),
  12. "<div>...</div>",
  13. ...
  14. ]);

Customize complex actions, refer to model-tree action.

Customize toolbar buttons

Please refer to the document model-tree action.