Yii嵌套设置为下拉菜单

时间:2012-10-28 22:38:10

标签: php drop-down-menu yii nested-sets

我正在使用 Yii嵌套设置行为,这有助于我保持我的类别嵌套在这里看到(没关系标题行,它们是俄语):enter image description here

我想做的就是使用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并且不知道如何将此功能应用于我的问题。感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

这是我用来格式化为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;

    }

答案 1 :(得分:1)

最后,我自己的递归解决方案(适用于多个根):

    public function getTreeRecursive() {
    $criteria = new CDbCriteria;
    $criteria->order = 'root, lft';
    $criteria->condition = 'level = 1';
    $categories = Category::model()->findAll($criteria);

    foreach($categories as $n => $category) {
        $category_r = array(
            'label'=>$category->title,
            'url'=>'#',
            'level'=>$category->level,
        );              
        $this->category_tree[$n] = $category_r;

        $children = $category->children()->findAll();
        if($children)
            $this->category_tree[$n]['items'] = $this->getChildren($children);
    }
    return $this->category_tree;
}
private function getChildren($children) {
    $result = array();
    foreach($children as $i => $child) {
        $category_r = array(
            'label'=>$child->title,
            'url'=>'#',
        );          
        $result[$i] = $category_r;
        $new_children = $child->children()->findAll();
        if($new_children) {
            $result[$i]['items'] = $this->getChildren($new_children);
        }           
    }
    return $result_items = $result;
}

答案 2 :(得分:0)

使用PHP&创建多级别类别系统Yii(MVC框架)不仅仅是一项简单的工作。 1.在模型中创建函数

`

function getRootCategory($cur_cat='') {
$sql='select id, course_name, parent_id from course where parent_id="0" and status=0'; 
$command=Yii::app()->db->createCommand($sql);
$return =$command->queryAll();
foreach($return as $rootCat){
if ($rootCat['id']==$cur_cat){
$test= 'selected=selected'; 
}else{
$test='';
 }
$id=$rootCat['id'];
echo "".$rootCat['course_name'].'';
$this->sub_cat($rootCat['id'] , '', $cur_cat ); 
}
 }

   function sub_cat($parentID=0, $space='',$cur_cat ) {
$sql="select id, course_name, parent_id from course where parent_id='$parentID' and status=0";
$command=Yii::app()->db->createCommand($sql);
$return =$command->queryAll();
$count=count($return);
if($parentID==0){ $space=''; }else{ $space .=" - "; }

if($count > 0){
foreach($return as $subcat){
if ($subcat['id']==$cur_cat){$test='selected=selected';}else{$test='';}
$ids=$subcat['id'];
echo "".$space.$subcat['course_name'].'';
$this->sub_cat($subcat['id'],$space, $cur_cat ); 
}
 } 
}

` 现在在view / file.php

中创建此代码
<?php
echo ‘<select id=”parent_id” class=”select” name=”Course[parent_id]” >’;
echo “<option value=’0′  >–Select Exam–</option>”;
echo Course::model()->getRootCategory($model->parent_id);
// ($model->parent_id) means selected text box
echo ‘</select>’; ?>

更多详情点击此网址访问http://it-expert.in/create-multi-level-category-using-recursive-function-in-yii/