2012-08-07 3 views
4

Q : 내 gridview에 대한 필터를 만드는 방법은 무엇입니까?CGridView 사용자 정의 열 필터

상태 : 고객 이름 = first_name. 이 내 그리드이다

LAST_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);    
    } 
} 

에서 VAR를 만들고) (규칙에서 '안전'과 같은 새로운 속성을 선언하는 모델의 방법을 잊지 마세요 또는 귀하의 의견은 고려되지 않습니다.

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

먼저 변화 :

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

에 : 모델의 search 방법에서

'name'=>'first_name', 

, 당신은 LIKE 조건을 추가해야합니다 :

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