数据库结构如下
控制器引入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();
}
编辑页面的代码
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('上级')}:</label>
<div class="col-xs-12 col-sm-8">
{:build_select('row[pid]', $ruledata, $row['pid'], ['class'=>'form-control', 'required'=>''])}
</div>
</div>
JS文件修改
{
field: 'name', title: __('Name'), align: 'left', formatter: function (value, row, index) {
return value.toString().replace(/(&|&)nbsp;/g, ' ');
}
},