I’m very sorry, but I’ve never had a chance to write a complicated page that contains a pair of parent-child CGridViews. So I can’t help you a lot.
But I would write a code like the following just for a starting point …
// in controller (ParentController.php)
public function actionAdminEx()
{
// Parent model instance for parent grid search parameters
$parent_model = new Parent('search');
if(isset($_GET['Parent']))
$parent_model->attributes=$_GET['Parent'];
// Child model instance for child grid search parameters
$child_model = new Child('search');
if(isset($_GET['Child']))
$child_model->attributes=$_GET['Child'];
$this->render('admin_ex',array(
'parent_model'=>$parent_model,
'child_model'=>$child_model,
));
}
// in view (views/parent/admin_ex.php)
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'parent-grid',
'dataProvider'=>$parent_model->search(),
'filter'=>$parent_model,
'columns'=>array(
...
),
)); ?>
...
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'child-grid',
'dataProvider'=>$child_model->search(),
'filter'=>$child_model,
'columns'=>array(
...
array(
'class'=>'CButtonColumn',
'viewButtonUrl' => 'array("child/view", "id"=>$data->id)',
'updateButtonUrl' => 'array("child/update", "id"=>$data->id)',
'deleteButtonUrl' => 'array("child/delete", "id"=>$data->id)',
),
),
)); ?>
I have created a new aciton named ‘adminEx’ and a new view file named ‘admin_ex’, leaving the existing ‘admin’ action and ‘admin’ view untouched. But you may modify the existing action and view file instead.
Now the code above will display a page with 2 CGridViews, one for Parent and the other for Child.
They should work fine independently, including the ‘view’, ‘update’ and ‘delete’ buttons of the child grid.
But they are independent. Clicking a row on the parent grid will not update the child grid.
We need some extra coding for that.
[s]1) we have to prepare a search form for the child grid
-
retrieve the id of the parent model that has been clicked on the parent grid
-
set the id to ‘parent_id’ field in the search form
-
submit the search form to update the child grid[/s]
This may also need some extra javascript coding, I guess.
But, I have to say sorry again, I can’t go any further right now.
[EDIT]
We may make use of CGridView::selectionChanged to connect 2 grids.