zoukankan      html  css  js  c++  java
  • yii2 利用小部件生成后台左边菜单栏

        ************   模型层递归查询权限   ************

    /**
         * 递归方式查询权限
         */
        public function getPrivilege()
        {
            $connection = Yii::$app->db;
            $top=$command = $connection->createCommand('SELECT * FROM privilege')->queryAll();       
            return $this->digui($top, $parent_id=0);
        }
        
        public function digui($top, $parent_id)
        {   
            $child = array();
            foreach ($top as $key => $v)
            {
                if($v['parent_id'] == $parent_id)
                {
                    $child[] = $v;
                }
            }
            if(empty($child))
            {
                return  null;
            }
            foreach($child as $key => $v)
            {
                $second = $this->digui($top, $v['p_id']);
                if($second)
                {
                    $child[$key]['child'] = $second;
                }
            }
            return $child;
        }

          ************   控制器层组合数组   ************

    public function actionLeft(){
            
            //查询所有权限
            $pri = new Privilege();
            $privilege = $pri->getPrivilege();
            $child = array();
            foreach ($privilege as $key => $val) {
                if($val['child']){
                    foreach ($val['child'] as $k => $v) {
                        $child[$key][] = "<a href='index.php?r=".$v['controller'].'/'.$v['action']."' target='mainFrame' >".$v['privilege']."</a>";
                    }
                } else {
                    unset($privilege[$key]);
                }
            }
            //print_r($child);die;
            return $this->render('left',['privilege'=>$privilege,'child'=>$child]);
        }

    ************   视图层引用部件   ************

    <?php

    //折叠
    use yiiootstrapCollapse;

    ?>

    <style>
       #yii-debug-toolbar{display: none;}
    </style>

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>项目后台管理系统</title>
    </head>
    <body style="background:#4AA3D8">
    <?php foreach($privilege as $k => $v){?>
        <?php
        echo  Collapse::widget([
        'items'=>[
            [
                'label'=>$v['privilege'],
                'content'=>$child[$k],
            ]        
        ],
        'options'=>['style'=>['margin-bottom'=>'5px', 'left'=>'0']]
    ]);?>
    <?php }?>
    </body>
    </html>

  • 相关阅读:
    转:C++中Static作用和使用方法
    转:C/C++中,空数组、空类、类中空数组的解析及其作用
    转:c++类实例在内存中的分配
    转:union 联合体(共用体)
    转:内存对齐与补齐 字节对齐与结构体大小
    转:c++内存分配
    转:代码重构
    转:设计模式六大原则(3):依赖倒置原则
    读书
    转:Teach Yourself Programming in Ten Years——用十年教会自己编程
  • 原文地址:https://www.cnblogs.com/jhy-ocean/p/5396754.html
Copyright © 2011-2022 走看看