thinkphp5带分组功能的树形结构的无限级分类节点展示

--
-- 表的结构 `h_group`
--


CREATE TABLE IF NOT EXISTS `h_group` (
`id` smallint(3) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(25) NOT NULL,
`title` varchar(50) NOT NULL,
`create_time` int(11) unsigned NOT NULL,
`update_time` int(11) unsigned NOT NULL DEFAULT '0',
`status` tinyint(1) unsigned NOT NULL DEFAULT '0',
`sort` smallint(3) unsigned NOT NULL DEFAULT '0',
`show` tinyint(1) unsigned NOT NULL DEFAULT '0',
`group_menu` char(15) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAMDEFAULT CHARSET=utf8 AUTO_INCREMENT=43 ;



--
-- 表的结构 `h_node`
--


CREATE TABLE IF NOT EXISTS `h_node` (
`id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
`route` varchar(50) NOT NULL,
`title` varchar(50) DEFAULT NULL,
`status` tinyint(1) DEFAULT '0',
`remark` varchar(255) DEFAULT NULL,
`sort` smallint(6) unsigned DEFAULT NULL,
`pid` smallint(6) unsigned NOT NULL,
`level` tinyint(1) unsigned NOT NULL,
`type` tinyint(1) NOT NULL DEFAULT '0',
`group_id` tinyint(3) unsigned DEFAULT '0',
PRIMARY KEY (`id`),
KEY `level` (`level`),
KEY `pid` (`pid`),
KEY `status` (`status`),
KEY `name` (`route`)
) ENGINE=MyISAMDEFAULT CHARSET=utf8 AUTO_INCREMENT=30 ;





node模型中的方法
/**
* 获取节点
* @date: 2017年9月6日 下午3:38:14
* @param: $level指定的级别
*$gid分组ID
* @return:
*/
public function getNodes($level=0,$gid=0){
$nodes=array();
$map = array('n.status' => 1);
if($gid){
$map['n.group_id'] = $gid;
}
if($level){
$map['n.level'] = $level;
}
$nodes = $this
->alias('n')
->join('__GROUP__ g','g.id = n.group_id')
->where($map)

->field('n.id,n.id as nId,n.route,n.title,n.title as text,n.level,n.pid,n.sort,n.group_id,g.title as tags')
->order('n.sort', 'desc')
->select();
//将查询的结果对象转换为数组
$nodes = collection($nodes)->toArray();
return $nodes;
}

/**
* 递归节点形成左侧的菜单
* @date: 2017年9月6日 下午3:39:47
* @param: $data所有节点
*$pId父节点
* @return:
*/
public function buildTrees($data, $pId)
{
$treenodes = array();
foreach($data as $k => $v)
{
if($v['pid'] == $pId)
{
$v['child'] = $this->buildTrees($data, $v['id']);
$treenodes[] = $v;
}
}
return $treenodes;
}



node控制器中的方法


public function createMenu(){
/*获取左侧菜单 */
$leftmenu = array();

$h_configs = Config::get('hplus');
$menu_cache_name = $h_configs['menu_cache_name'];
//将菜单加入session
if(Session::has($menu_cache_name)){
$leftmenu = Session::get($menu_cache_name);
}else{
/* 获取分组 */
$group_model = new GroupM();
$leftmenu['groups']= $group_model->getGroups();
$leftmenu['grouplist']= $leftmenu['groups'];
/* 获取节点 */
$leftmenu['node'] = array();
if(!empty($leftmenu['groups'])){
foreach ($leftmenu['groups'] as $k=>$v){
$tmp =array();
//判断是否是admin用户
if($this->is_admin){
$tmp = $this->node_model->getNodes(0,$v['id']);
}
else{
$tmp = $this->node_model->getNodesLeft(0,$v['id'],Session::get($this->user_auth_key));
}
if(!empty($tmp)){
$tmp = $this->node_model->buildTrees($tmp,0);
$leftmenu['node'][$v['id']] = $tmp;
// array_walk($tmp,'walkfuc');
// $nodes_route[$v['id']] = $tmp;
}else{
unset($leftmenu['groups'][$k]);
}

}
}
//用于判断左侧菜单是否高亮
$leftmenu['level_two_childs'] = array();
$leftmenu['level_two_childs'] = $this->node_model->getChildsByLevel();
Session::set($menu_cache_name,$leftmenu);
}

//$treenode = $node_model->buildTrees($nodes,0);
// $treenode = empty($treenode)?array():$treenode;
$this->assign('level_two_childs',isset($leftmenu['level_two_childs'])?$leftmenu['level_two_childs']:array());
$this->assign('menugroup',isset($leftmenu['groups'])?$leftmenu['groups']:array());
$this->assign('grouplist',isset($leftmenu['grouplist'])?$leftmenu['grouplist']:array());
$this->assign('treenode',isset($leftmenu['node'])?$leftmenu['node']:array());
}

实现效果
【thinkphp5带分组功能的树形结构的无限级分类节点展示】thinkphp5带分组功能的树形结构的无限级分类节点展示
文章图片


    推荐阅读