// 多分类列表
public function cate()
{
$cate = doCate::all()->toArray();
$cate = $this->getTree($cate);
for ($i=0;$i<sizeof($cate);$i++){
$cate[$i]['title'] = str_repeat(' -----', $cate[$i]['level']).$cate[$i]['title'];
}
$this->assign('cate',$cate);
return $this->fetch();
}
// 递归分类
function getTree($array, $pid =0, $level = 0){
//声明静态数组,避免递归调用时,多次声明导致数组覆盖
static $list = [];
foreach ($array as $key => $value){
//第一次遍历,找到父节点为根节点的节点 也就是pid=0的节点
if ($value['pid'] == $pid){
//父节点为根节点的节点,级别为0,也就是第一级
$value['level'] = $level;
//把数组放到list中
$list[] = $value;
//把这个节点从数组中移除,减少后续递归消耗
unset($array[$key]);
//开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
$this->getTree($array, $value['id'], $level+1);
}
}
return $list;
}
{volist name="cate" id="v"}
<tr cate-id="{$v.id}" fid={$v.pid} >
<td>{$v.id}</td>
<td>
<i class="layui-icon x-show" status='true'></i>
<?php echo $v['title']?>
</td>
<td>
<button class="layui-btn layui-btn layui-btn-xs" onclick="disCate({$v.id},{$v.status})" n>{if $v.status == 1}正常{else/}已禁用{/if}</button>
</td>
<td class="td-manage">
<button class="layui-btn layui-btn layui-btn-xs" onclick="xadmin.open('编辑','editCate/?id={$v.id}',600,400)" >
<i class="layui-icon"></i>编辑
</button>
<button class="layui-btn layui-btn-warm layui-btn-xs" onclick="xadmin.open('编辑','addCate/?pid={$v.id}',600,400)" >
<i class="layui-icon"></i>添加子分类
</button>
</td>
</tr>
{/volist}
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!