dact-admin数据表单->树形选择器 (tree) 可以修改成单选吗

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 4

同求这个需求实现,在做“选择上级分类”的时候想要能单选某个条目。

1年前 评论

可以的,查看源码,得知 dcat admin 采用的是 jstree.js, 它的API里有这个配置项 multiple。通过继承 Dcat\Admin\FormDcat\Admin\Form\Field\Tree的方式,重写 Dcat\Admin\Form\Field\Tree 中的 options 属性,加入 jstree.js 的配置项即可实现:

<?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,
            ],
        ],
    ];
}
9个月前 评论

刚刚又发现一个好方法,不用上边那么麻烦,Dcat\Admin\Form\Field 有个 mergeOptions 方法,可以在controller 中直接修改 jstree 的配置

$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();
9个月前 评论
SoryeTong 5天前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!