问答 / 1 / 4 / 创建于 2年前
同求这个需求实现,在做“选择上级分类”的时候想要能单选某个条目。
可以的,查看源码,得知 dcat admin 采用的是 jstree.js, 它的API里有这个配置项 multiple。通过继承 Dcat\Admin\Form 和 Dcat\Admin\Form\Field\Tree的方式,重写 Dcat\Admin\Form\Field\Tree 中的 options 属性,加入 jstree.js 的配置项即可实现:
Dcat\Admin\Form
Dcat\Admin\Form\Field\Tree
options
<?php namespace App\Admin\Form\Field; use Dcat\Admin\Form\Field\Tree as FieldTree; /** * dcat admin 使用的是 jstree.js */ class Tree extends FieldTree { protected $options = [ 'plugins' => ['checkbox', 'types'], 'core' => [ 'check_callback' => true, 'themes' => [ 'name' => 'proton', 'responsive' => true, ], 'multiple' => false, // 采用单选 ], 'checkbox' => [ 'keep_selected_style' => false, 'three_state' => true, ], 'types' => [ 'default' => [ 'icon' => false, ], ], ]; }
刚刚又发现一个好方法,不用上边那么麻烦,Dcat\Admin\Form\Field 有个 mergeOptions 方法,可以在controller 中直接修改 jstree 的配置
Dcat\Admin\Form\Field
mergeOptions
$form->tree('category_id', '所属分类') ->nodes(ProductCategory::get()->toArray()) ->saving(fn($value) => $value[0] ?? 0) // dcat admin 这里使用的jstree, value 是个数组 ->expand(false) ->mergeOptions([ 'core' => [ 'check_callback' => true, 'themes' => [ 'name' => 'proton', 'responsive' => true, ], 'multiple' => false, // 采用单选 ], ]) ->required();
我要举报该,理由是:
同求这个需求实现,在做“选择上级分类”的时候想要能单选某个条目。
可以的,查看源码,得知 dcat admin 采用的是 jstree.js, 它的API里有这个配置项 multiple。通过继承
Dcat\Admin\Form
和Dcat\Admin\Form\Field\Tree
的方式,重写Dcat\Admin\Form\Field\Tree
中的options
属性,加入 jstree.js 的配置项即可实现:刚刚又发现一个好方法,不用上边那么麻烦,
Dcat\Admin\Form\Field
有个mergeOptions
方法,可以在controller 中直接修改 jstree 的配置