如何在CGridView Yii框架中添加下拉菜单?

时间:2016-07-09 21:29:22

标签: php mysql yii

我想将CGridView文本字段更改为下拉菜单,并将数字更改为类别名称。

category grid view

$model = new Products('search');
        $model->unsetAttributes();  
        if(isset($_GET['Products'])){
           $model->attributes=$_GET['Products'];
        }

在视图中。

<?php
       $this->widget('zii.widgets.grid.CGridView', array(
       'dataProvider'=>$model->search(),                               
       'columns'=>array(                                                                                                                                                       'code',
            'category_id',
            'quantity',
            ),
           'filter'=>$model,
        ));
    ?>

1 个答案:

答案 0 :(得分:1)

只需代替&quot; category_id&#39;,

array(
           // 'name' => 'category_id',
             'header'=>'Category Name',
            'value' => '$data->categoryname($data->category_id)',
            'filter' => $categoryArray
        ),


categoryname() should be written in your `Product Model`.  



  Product Model
public function categoryname($category_id){
//fetch your category name with this category id and return it

return categoryName;
}

$categoryArray应该是一个数组,其键为类别ID,值为category_name

$categoryArray = CHtml::listData(Category::model()->findAll() 'category_id', 'category_name')