Yii Framework Forum: CGridView with two models - Filtering does not work! - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CGridView with two models - Filtering does not work! Rate Topic: -----

#1 User is offline   Abdallah 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 23-June 17

Posted 11 July 2017 - 06:01 AM

I have two models users and userstatus. I want to display information from the two models in acgridview and be able to filter on all the column.
so far, i have managed to only display the information. My filtering is not working.Can anyone help me? for now only filtering on the first column, i.e the UserName column

Relations:
Users Model - 'userstatus' => array(self::HAS_ONE, 'Userstatus', 'iUserId'),
Userstatus Model - 'users'=>array(self::BELONGS_TO,'Users','iUserId'),

Search Method in userstatus model:

public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.

$criteria=new CDbCriteria;

$criteria->compare('iUserStatusId',$this->iUserStatusId);
$criteria->compare('vcModel',$this->vcModel,true);
$criteria->compare('iUserStatus',$this->iUserStatus,true);
$criteria->with=array('users');
$criteria->addSearchCondition('users.vcUserName',$this->iUserId);
$criteria->compare('vcAppVersion',$this->vcAppVersion,true);
$criteria->compare('vcDataUsage',$this->vcDataUsage,true);

return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array('pageSize'=>200,),
//'order'=>field(iUserStatus,'Active','Inactive');
));
}


VIEW:

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'users-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array('header'=>'User','value' => '$data->vcUserName', 'filter' => CHtml::activeTextField($model, 'vcUserName'), ),
array('header'=>'Model','value'=>'$data->userstatus["vcModel"]'),
array('header'=>'Data Usage','value'=>'$data->userstatus["vcDataUsage"]', 'name'=>'userstatus.vcDataUsage', 'filter' => CHtml::activeTextField($model, 'vcDataUsage'), ),
array('header'=>'AppVersion','value'=>'$data->userstatus["vcAppVersion"]'),
array('header'=>'Status','value'=>function($data){
if($data->vcLoginStatus === "true"){
return "Active";
} else {
return "InActive";
}
}),

array(
'class'=>'CButtonColumn',
'template'=>'{view}',
),
),
));
0

#2 User is offline   alrazi 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,316
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 11 July 2017 - 09:19 AM

there is nice article that goes in detail

http://www.yiiframew...l-in-cgridview/
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users