2012-08-07 56 views
4

問:如何爲我的gridview創建過濾器?CGridView自定義列過濾器

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', 
     ), 
    ), 
)); ?> 

回答

11

在視圖

<?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', 
      ), 
     ), 
    )); ?> 

在創建模型

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

一個變種,不要忘記在規則中聲明新的屬性爲「安全」()模型的方法,或您的輸入將不被視爲

public function rules() 
{ 
    return array(
    /* ... */ 
     array('customer_name', 'safe', 'on'=>'search'), 
    ); 
} 
3

第一個變化:

'name'=>'$data->first_name', 

到:

'name'=>'first_name', 

在模型search方法,你將不得不增加LIKE條件:

$model->addSearchCondition('CONCAT(first_name, " ", last_name)', $this->first_name);