4

我正在使用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
)); 

我不明白如何完成这项工作,顺便说一句,我阅读了这个主题,只是不知道如何将此功能实际应用于我的问题。感谢任何帮助。

4

3 回答 3

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;
}
于 2012-10-30T22:08:08.007 回答
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;

    }
于 2012-10-29T18:58:05.280 回答
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 ‘&lt;select id=”parent_id” class=”select” name=”Course[parent_id]” >’;
echo “&lt;option value=’0′  >–Select Exam–&lt;/option>”;
echo Course::model()->getRootCategory($model->parent_id);
// ($model->parent_id) means selected text box
echo ‘&lt;/select>’; ?>

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

于 2018-03-31T07:50:02.420 回答