dongxuan1314
2014-03-05 16:27 阅读 60
已采纳

too long

How do I do a query to the case of multi-level menus in yii?

For example I have a table like this :

enter image description here

menu_name will follow parrent_id it has, so it will form a hierarchical menu

enter image description here

How do I use Yii on my layout so that the output as html above

My code :

<?php
Yii::import('zii.widgets.CMenu', true);

class ActiveMenu extends CMenu
{
    public function init(){

    $criteria = new CDbCriteria;
    $criteria->condition='published=:idpub AND menu_controller=:menu';
    $criteria->params=array(':idpub'=>1, ':menu'=>'#');

    $items = Menu::model()->findAll($criteria);

    echo "<ul id='yw1' class='nav'>";

        foreach ($items as $item)
        {
            echo "<li class='dropdown'>
                    <a href=".$item->menu_controller." data-toggle='dropdown' class='dropdown-toggle'>
                    <i class='icon-white icon-list'></i>".$item->menu_name."<b class='caret'></b></a>
                </li>";
        }

        echo "</ul>";

        parent::init();
    }
}

But in the above code I only get parents data. How do I want to loop to get the childs data include in every parents menu like this html code :

<ul id="yw1" class="nav">
    <li><a href="/app/home.html"><span class="icon-white icon-tasks"></span> Home</a></li>

    <li class="dropdown">
        <a href="#" data-toggle="dropdown" class="dropdown-toggle">
        <i class="icon-white icon-list"></i>Master<b class="caret"></b></a>
        <ul class="dropdown-menu">
            <li><a href="expedition"><span class="icon-bar icon-list"></span> Expedition</a></li>
        </ul>
        <ul class="dropdown-menu">
            <li><a href="partner"><span class="icon-bar icon-list"></span> Partner</a></li>
        </ul>
        <ul class="dropdown-menu">
            <li><a href="user"><span class="icon-bar icon-list"></span> User</a></li>
        </ul>
    </li>

    <li class="dropdown">
        <a href="#" data-toggle="dropdown" class="dropdown-toggle">
        <i class="icon-white icon-list"></i>Transaction<b class="caret"></b></a>
        <ul class="dropdown-menu">
            <li><a href="processin"><span class="icon-bar icon-list"></span> Process In</a></li>
        </ul>
        <ul class="dropdown-menu">
            <li><a href="processout"><span class="icon-bar icon-list"></span> Process Out</a></li>
        </ul>
        etc ....
        etc ....
        etc ....
    </li>
</ul>

Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dsvcqvp139098 dsvcqvp139098 2014-03-05 17:04

    You can create a recursive function for loop your childs like this

    <?php
        class ActiveMenu extends CMenu
        {
            public function init(){
                $this->renderChilds();
            }
    
            protected function getChilds($parent_id)
            {
                $criteria = new CDbCriteria;
                $criteria->condition='published=:idpub AND menu_controller=:menu AND parent_id = :parent';
                $criteria->params=array(':idpub'=>1, ':menu'=>'#', ':parent'=>$parent_id);
                $items = Menu::model()->findAll($criteria);
                return $items;
            }            
    
            public function renderChilds($parent_id=0, $class='nav')
            {
                 $items = $this->getChilds($parent_id);
                 if (!empty($items))
                 {
                     echo "<ul class='$class'>";
                     foreach ($items as $item)
                     {
                         echo "<li class='dropdown'>
                         <a href=".$item->menu_controller." data-toggle='dropdown' class='dropdown-toggle'>
                         <i class='icon-white icon-list'></i>".$item->menu_name."<b class='caret'></b></a>";
                         $this->renderChilds($item->id, 'dropdown-menu');                
                         echo "</li>";
                     }
                     echo "</ul>";
                 }
             }
    
             ...
         }    
    
    点赞 评论 复制链接分享

相关推荐