在CGridView中过滤自定义列(在模型中具有自定义方法的值)


filter custom columns(having values from custom methods in model) in CGridView

下面是我在view.php文件中的代码

$this->widget('zii.widgets.grid.CGridView', array(
        'id' => 'myGrid',
        'dataProvider' => $model->search(),
        'filter' => $model,
        'enableSorting' => true,
        'columns' => array(
            array(
                'name' => 'id',
                'header' => 'ID',
                'value' => $data->id,                
            ),
            array(
                'header' => 'Value', 
                'value' => '$data->getValue($data->id)', //getValue($id) is a custom method in the model.php file which returns a value after some calculations                    
                'filter' => $model->listOfFilterValues(), //listOfFilterValues() is a custom method in the model.php file which returns a CHtml::listData
            ),            
        ),
       )
    );

正如您所看到的,我在model.php文件中使用自定义方法来获取Value列(因为我无法从查询中获得它)。出现gridView,并出现下拉列表。

问题是使用下拉过滤(在值列)不起作用。(因为它不是查询输出中的一列)也排序上的值列(当我点击列标题)不工作。

有办法完成这件事吗?非常感谢你的帮助。谢谢你。

试试这个:

在模型:

class Plans extends BasePlans
{
 ...
public $planTypeSearch;
 ...
public function search() {    
...     
$criteria->compare('plan.plan_type', $this->planTypeSearch, true);
...
),
return new CActiveDataProvider($this, array(
    'criteria' => $criteria,
    'sort'=>array(
        'attributes'=>array(
            'planTypeSearch'=>array(
                 'asc'=>'plan.plan_type',
                 'desc'=>'plan.plan_type DESC',
             ),
         ),
     ),
 );

在你看来:

array(
    'name'=>'planTypeSearch',
    'header'=> 'Plan Type',
    'value'=>'(isset($data->plan)) ? $data->plan->plan_type: null',
    'filter'=>isset($plans) ? CHtml::listData($plans, 'plan_type','plan_type') : NULL,
),