数据库
Controller递归处理
public function getDeptTree() {
$deptdata = Db::table('dept')->field('dept_name,dept_no,p_id')->select();
$showdata = $this->getTree($deptdata, $p_id = 0, $depth = 0);
$data = json($showdata);
return $data;
}
// //部门树形数据组装
public function getTree($treedata, $p_id, $depth) {
$retnArr = array();
if (!empty($treedata)) {
foreach ($treedata as $key => $info) {
if ($info['p_id'] == $p_id) {
$info['depth'] = $depth;
$temp_info = $info;
foreach ($treedata as $subinfo) {
if ($subinfo['p_id'] == $info['dept_no']) {
$temp_info['sub'] = $this->getDeptTree($treedata, $info['dept_no'], $depth + 1);
}
}
$retnArr[] = $temp_info;
}
}
}
return $retnArr;
}
public function g