模型树
这个功能可以实现一个树状组件,可以用拖拽的方式实现数据的层级、排序等操作,下面是基本的用法。
表结构和模型
要使用model-tree,要遵守约定的表结构:
{tip}
parent_id字段一定要默认为0!!!
CREATE TABLE `demo_categories` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`parent_id` int(11) NOT NULL DEFAULT '0',`order` int(11) NOT NULL DEFAULT '0',`title` varchar(50) COLLATE utf8_unicode_ci NOT NULL,`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
上面的表格结构里面有三个必要的字段parent_id、order、title,其它字段没有要求。
对应的模型为app/Models/Category.php:
<?phpnamespace App\Models\Demo;use Dcat\Admin\Traits\ModelTree;use Illuminate\Database\Eloquent\Model;class Category extends Model{use ModelTree;protected $table = 'demo_categories';}
表结构中的三个字段parent_id、order、title的字段名也是可以修改的:
{tip} 为了便于阅读,这里不再展示
Repository代码。
<?phpnamespace App\Models\Demo;use Dcat\Admin\Traits\ModelTree;use Illuminate\Database\Eloquent\Model;class Category extends Model{use ModelTree;protected $table = 'demo_categories';// 父级ID字段名称,默认值为 parent_idprotected $parentColumn = 'pid';// 排序字段名称,默认值为 orderprotected $orderColumn = 'sort';// 标题字段名称,默认值为 titleprotected $titleColumn = 'name';}
使用方法
然后就是在页面中使用model-tree了:
<?phpnamespace App\Admin\Controllers\Demo;use App\Models\Category;use Dcat\Admin\Layout\Row;use Dcat\Admin\Layout\Content;use Dcat\Admin\Tree;use Dcat\Admin\Controllers\AdminController;class CategoryController extends AdminController{public function index(Content $content){return $content->header('树状模型')->body(function (Row $row) {$tree = new Tree(new Category);$row->column(12, $tree);});}}
可以通过下面的方式来修改行数据的显示:
$tree = new Tree(new Category);$tree->branch(function ($branch) {$src = config('admin.upload.host') . '/' . $branch['logo'] ;$logo = "<img src='$src' style='max-width:30px;max-height:30px' class='img'/>";return "{$branch['id']} - {$branch['title']} $logo";});
在回调函数中返回的字符串类型数据,就是在树状组件中的每一行的显示内容,$branch参数是当前行的数据数组。
如果要修改模型的查询,用下面的方式
$tree->query(function ($model) {return $model->where('type', 1);});
自定义工具栏按钮
请参考文档模型树动作
