编程语言
首页 > 编程语言> > php – Yii嵌套设置为下拉菜单

php – Yii嵌套设置为下拉菜单

作者:互联网

我正在使用Yii嵌套集行为,这有助于我保持我的类别嵌套在这里看到(无所谓的标题行,它们是俄语):

我想要做的就是使用Bootstrap嵌套菜单,它应该是这样的:

    $criteria = new CDbCriteria;
    $criteria->order = 'root, lft';
    $categories = Category::model()->findAll($criteria);
    foreach($categories as $i => $category) {
      $items[$i]['label'] = $category->title;
      $items[$i]['url'] = $category->url;
      $items[$i]['active'] = false;
      $items[$i]['items'] = array(
        array('label'=>'123', 'url'=>'#'),
        array('label'=>'123', 'url'=>'#'),
        array('label'=>'123', 'url'=>'#', 'items'=>array(
            array('label'=>'1234', 'url'=>'#'),
            array('label'=>'1234', 'url'=>'#'),
            array('label'=>'1234', 'url'=>'#', 'items'=>array(
                array('label'=>'1234', 'url'=>'#'),
                array('label'=>'1234', 'url'=>'#'),
                array('label'=>'1234', 'url'=>'#'),
            )),
        )),
    ); 
}
$this->widget('bootstrap.widgets.TbMenu', array(
    'type'=>'pills',
    'stacked'=>false, // whether this is a stacked menu
    'items'=>$items
)); 

我不明白如何完成这个,顺便说一下我读了this topic,只是不知道如何将这个功能应用于我的问题.感谢任何帮助.

解决方法:

这是我用来格式化为json对象的函数,你可以修改它来生成一个php数组.

protected function formatJstree(){
        $categories = $this->descendants()->findAll();
        $level=0;
        $parent = 0;
        $data = array();
        foreach( $categories as $n => $category )
        {
            $node = array(
                'data'=> "{$category->title}",
                'attr'=>array('id'=>"category_id_{$category->category_id}")
            );
            if($category->level == $level){
                $data[$parent]["children"][] = $node;
            }
            else if($level != 0 && $category->level > $level){
                if(!isset($data[$n]["children"])){
                    $data[$n]["children"] = array();
                }
                $data[$parent]["children"][] = $node;
            }
            else
            {
                $data[] = $node;
                $parent = $n;
            }
            $level=$category->level;

        }
        return $data;

    }

标签:php,drop-down-menu,yii,nested-sets
来源: https://codeday.me/bug/20190704/1373401.html