CGridView自定义列过滤器

Thu*_* Ra 5 php filter yii cgridview

问:如何为gridview创建过滤器?

status:customer name = first_name.姓

这是我的网格视图

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'customer-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        array(
              'header'=>'Customer Name',
              'name'=>'$data->first_name',
              'value'=>'$data->first_name.\' \'.$data->last_name',
              ),        
        'company_name',
        'country',
        'state',
        'city',     
        'address1',         
        'phone1',       
        'email',        
        array('name' => 'company_id',
               'value'=>'$data->companies->name',
               'filter'=>CHtml::listData($records, 'id', 'name'),
        ),
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>
Run Code Online (Sandbox Code Playgroud)

Thu*_* Ra 12

在模型中创建一个var

class Customer extends CActiveRecord
{
    public $customer_name;
    public function search()
    {            
        $criteria->compare('CONCAT(first_name, \' \', last_name)',$this->customer_name,true);            
    }
}
Run Code Online (Sandbox Code Playgroud)

在视图

<?php $this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'customer-grid',
        'dataProvider'=>$model->search(),
        'filter'=>$model,
        'columns'=>array(
            array(           
                'name'=>'customer_name',
                'value'=>'ucwords($data->first_name.\' \'.$data->last_name)',
                  ),        
            'company_name',
            'country',
            'state',
            'city', 
            'address1',
            'phone1',
            'email',
            array(
                'class'=>'CButtonColumn',
            ),
        ),
    )); ?>
Run Code Online (Sandbox Code Playgroud)

并且不要忘记在模型的rules()方法中将新属性声明为"安全",否则您的输入将不会被考虑

public function rules()
{
    return array(
    /* ... */
        array('customer_name', 'safe', 'on'=>'search'),
    );
}
Run Code Online (Sandbox Code Playgroud)