ThinkPHP3.0+dwz1.4.3 无限级左边栏菜单

转自:hongping626的专栏

表结构及测试数据如下:

在common.php共用函数里面加入:

//将数组转化为树形数组
 function arrToTree($data,$pid){
        $tree = array();
        foreach($data as $k => $v){
            if($v['pid'] == $pid){
                $v['pid'] = arrToTree($data,$v['id']);
                $tree[] = $v;
            }
        }        
        return $tree;
 }
 //左边菜单栏输出
 function outMenu($group,$tree){
    $html = '';
    foreach($tree as $t){
      if($t['group_id']==$group){
        if(empty($t['pid'])){
            $html .= '<li><a href="'.__APP__.'/'.$t['name'].'/index/" target="navTab" rel="'.$t['name'].'">'.$t['title'].'</a></li>';
        }else{
            $html .='<li><a>'.$t['title'].'</a><ul>';
            $html .=outMenu($group,$t['pid']);
            $html  = $html.'</ul></li>';
        }
      }
    } 
    return $html;
 }

indexAction.class.php:

public function index() {
		if (isset ( $_SESSION [C ( 'USER_AUTH_KEY' )] )) {
			//显示菜单项
			$menu = array ();
			
			//读取数据库模块列表生成菜单项
			$node = M ( "Node" );
			$where ['status'] = 1;
            $where ['group_id'] = array(array('exp','is not null'),array('neq',0));
			$list = $node->where ( $where )->field ( 'ID,PID,NAME,GROUP_ID,TITLE' )->order ( 'group_id,level_id,sort' )->select ();
            $gList = $node->where($where)->getField('id,group_id');
			$accessList = $_SESSION ['_ACCESS_LIST'];
			foreach ( $list as $key => $module ) {
				if (isset ( $accessList [strtoupper ( APP_NAME )] [strtoupper ( $module ['name'] )] ) || $_SESSION ['administrator']) {
					//设置模块访问权限
					$module ['access'] = 1;
					$menu [$key] = $module;
				}
			}                       
            $groupM = D('Group');
            $groupMap['status'] = 1;
            $groupMap['id'] = array('in',array_flip(array_flip($gList)));
            $groupList = $groupM->where($groupMap)->order('sort asc')->select();
                        
            $menu = arrToTree($menu,0);

			if (! empty ( $_GET ['tag'] )) {
				$this->assign ( 'menuTag', $_GET ['tag'] );
			}
			//dump($menu);
            $this->assign ( 'groupList', $groupList );
			$this->assign ( 'menu', $menu );
		}
		C ( 'SHOW_RUN_TIME', false ); // 运行时间显示
		C ( 'SHOW_PAGE_TRACE', false );
		$this->display ();
	}

模板menu.html:

<div class="accordion" fillSpace="sideBar">
  <volist id="v1" name="groupList" >
    <div class="accordionHeader">
        <h2><span>Folder</span>{$v1.title}</h2>
    </div>
    <div class="accordionContent">    
        <ul class="tree treeFolder">
        {$v1['id']|outMenu=$menu}
        </ul>    
    </div>
  </volist>
</div>

最终效果:

(本文为作者原创,如需转载及引用请标明作者、出处)


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值