Hi,
Has anyone coded a CGridView’s filter to dynamically modify another filter? Ex. My Project grid has a Category and SubCategory. I need to filter the sub categories by their parent category - ajax would be nice.
1856
Thanks!
Matt
Hi,
Has anyone coded a CGridView’s filter to dynamically modify another filter? Ex. My Project grid has a Category and SubCategory. I need to filter the sub categories by their parent category - ajax would be nice.
1856
Thanks!
Matt
You can simply set the filter based on the value of the other dropdown.
It works because each change in the filter call an ajax refresh for the grid.
Thanks!
I’ve implemented it like this and it works well. Looks rather messy though. Do you know of any other ways, in the widget or the Project->search method, that will work too?
$this->widget('zii.widgets.grid.CGridView', array(
'filterPosition' => 'header',
'id' => 'project-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'cssFile' => Yii::app()->baseUrl . '/css/gridViewStyle/gridView.css',
'htmlOptions' => array('class' => 'grid-view rounded'),
'ajaxUpdate' => 'adminPanel',
'template' => '{items}{pager}',
'enableSorting' => false,
'columns' => array(
array(
'name' => 'category_id',
'value' => '$data->category->name',
'filter' => CHtml::listData(Category::model()->findAll(), 'id', 'name'),
),
array(
'name' => 'sub_category_id',
'value' => '$data->subCategory->name',
'filter' =>
CHtml::listData(
isset($model->category_id) ? SubCategory::model()->findAll(new CDbCriteria(array(
'condition' => 'category_id = :parentCategoryId',
'params' => array(':parentCategoryId' => $model->category_id),
))) : SubCategory::model()->findAll(), 'id', 'name'),
),
'name',
'created_date',
array(
'class' => 'CButtonColumn',
'header' => 'View',
'template' => '{view}',
'viewButtonImageUrl' => Yii::app()->baseUrl . '/images/icons/milky/24/16.png',
),
)
));
Cheers,
Matt
Found a bug and changed isset to is_numeric. I think its because once the parent category filter has been set (by a drop down click), it isn’t unset by selecting the first, empty element in the list. Hence it is trying to find sub categories for an empty parent.
array(
'name' => 'sub_category_id',
'value' => '$data->subCategory->name',
'filter' =>
CHtml::listData(
is_numeric($model->category_id) ? SubCategory::model()->findAll(new CDbCriteria(array(
'condition' => 'category_id = :parentCategoryId',
'params' => array(':parentCategoryId' => $model->category_id),
))) : SubCategory::model()->findAll(), 'id', 'name'),
),
Matt
great is_numeric works great than isset…
Hi there I’ve a small problem. I created user CGridView which is relationship with company and department.
Error : company name is display in the grid view. but department name is not display in gridveiw. when I uncomment ‘value’=>…, “Trying to get property of non-object” show.
this model relation
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'ranks' => array(self::BELONGS_TO, 'Rank', 'rank_id'),
'companies' => array(self::BELONGS_TO, 'Company', 'company_id'),
'departments' => array(self::BELONGS_TO, 'Department', 'department_id'),
'departmentsdep' => array(self::BELONGS_TO, 'Department', 'department_id'),
'departmentssec' => array(self::BELONGS_TO, 'Department', 'section_id'),
'departmentstea' => array(self::BELONGS_TO, 'Department', 'team_id'),
);
}
this is gridview
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'login_name',
'first_name',
'last_name',
'email',
array(
'name' => 'company_id',
'value'=>'$data->companies->name',
'filter'=>CHtml::listData($companylist, 'id', 'name'),
),
array(
'name' => 'department_id',
//'value'=>'$data->departmentsdep->name',
'filter' =>
CHtml::listData(
is_numeric($model->company_id) ? Department::model()->findAll(new CDbCriteria(array(
'condition' => 'p_id = 0 AND company_id=:company_id',
'params' => array(':company_id' => $model->company_id),
))) : $department, 'id', 'name'),
),
array(
'name' => 'section_id',
//'value'=>'$data->departmentssec->name',
'filter' =>
CHtml::listData(
is_numeric($model->department_id) ? Department::model()->findAll(new CDbCriteria(array(
'condition' => 'p_id = :pid AND company_id=:company_id',
'params' => array(':company_id' => $model->company_id, ':pid'=>$model->department_id),
))) : $department, 'id', 'name'),
),
array(
'name' => 'team_id',
//'value'=>'$data->departmentstea->name',
'filter' =>
CHtml::listData(
is_numeric($model->department_id) ? Department::model()->findAll(new CDbCriteria(array(
'condition' => 'p_id = :pid AND company_id=:company_id',
'params' => array(':company_id' => $model->company_id, ':pid'=>$model->section_id),
))) : $department, 'id', 'name'),
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
How can I solve this error
thanks its help me