the following piece of code is working, except for one thing. The filter used for the departments should be dependent on the value selected in the filter for a region (a department belongs to one region). I’ve tried with the FindAllByAttributes() there that you see on the department filter, but it isn’t working. I can use FindAll() but then the two filters are not related. Any ideas on how to achieve this ?
thanks
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'mainGrid',
'dataProvider'=>$model->filterForDefaultSalesUserManagement(),
'filter'=>$model,
'columns'=>array(
array(
'class'=>'CCheckBoxColumn',
'id'=>'checkedColumn',
'selectableRows'=>2
),
array(
'name'=>'Fr_RegionId',
'header'=>'Region',
'value'=>'$data->frDepartment->frRegion->NameFR',
'filter'=>CHtml::listData(FrRegion::model()->findAll(),'Id','NameFR')
),
array(
'name'=>'Fr_DepartmentId',
'header'=>'Department',
'value'=>'$data->frDepartment->NameFR',
'filter'=>CHtml::listData(FrDepartment::model()->findAllByAttributes(
array(),
$condition = "fr_RegionId = :regionId",
$params = array(':regionId'=>$data->frDepartment->frRegion->Id)
),
'Id','NameFR')
),
'PostalCode',
'NameFR',
)
));
Change your params for the Department filtering to the following:
Assuming the filtering by
Regionalready works, then this should work as well.Your currently posted code does not work because you are trying to access the
$dataobject, which does not exist in the scope you are using it in. It is only used on a per-row basis, and refers to the objects returned via the dataProvider.You need to filter using
$model‘s attributes. Since you are using theFr_RegionIdattribute of whatever object$modelis for the Region column, then you can also use it to further filter your Department column.Note: You may have to adjust whether to even filter the Department at all, as if nothing is set in the
FR_RegionIdattribute, then it will attempt to match where the Department is blank. Or it will throw an error, depending on how you have your database set up.Try the above. It should let you filter Department if
Fr_RegionIdis selected from the Region dropdown box, and otherwise it won’t let you filter at all.Or, if you want to let every Department be available if no Region is selected, then replace
falsewith