fastadmin树形表格

数据库结构如下

image.png

控制器引入Tree类

use fast\Tree;

分配数据给添加、编辑页面

$ruleList = collection($this->model->order('id', 'asc')->select())->toArray();

Tree::instance()->init($ruleList);
$this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
$ruledata = [0 => __('无')];
foreach ($this->rulelist as $k => &$v) {
    $ruledata[$v['id']] = $v['name'];
}
unset($v);
$this->view->assign('ruledata', $ruledata);

改造index方法 不需要分页

public function index()
{
    if ($this->request->isAjax()) {
        $list = $this->rulelist;
        $total = count($this->rulelist);
        $result = array("total" => $total, "rows" => $list);
        return json($result);
    }
    return $this->view->fetch();
}

编辑页面的代码

{:build_select('row[pid]', $ruledata, $row['pid'], ['class'=>'form-control', 'required'=>''])}

JS文件修改

 {
    field: 'name', title: __('Name'), align: 'left', formatter: function (value, row, index) {
        return value.toString().replace(/(&|&)nbsp;/g, ' ');
    }
},

最终效果

image.png

image.png

image.png

你可能感兴趣的:(fastadmin树形表格)