Reusing A Cgridview
Posted 16 February 2013 - 10:42 AM
Now I have created a second Controller by gii which handles garages. So here the "admin" page shows an overview of all garages in the database.
When I click on a garage the detail information of this garage entity is shown by the "view" page. Additionally I want this page to show all cars which are currently assigned to this garage. Cars can be parked in a garage so I modelled this relation in the Model files. On the "view" page of the Garage site should be shown the same CGridView as in the "admin" page of the Car site.
To achieve this I have wrapped the CGridView declaration in an own file where I show the CGridView by using beginWidget. I guess this CGridView is now a widget in terms of yii? I am showing this widget now in both pages which I have mentioned above by renderPartial. Here I am passing the model which should be shown. In case of the car/admin-page it contains the whole table and in case of garage/view-page it only contains the cars of the selected garage. Everything works fine to here.
But the filters are making problems. On car/admin the filters are working fine. But they doesn't on garage/view. When I enter a filter term and press enter the table gets refreshed but the filter term is gone. The table seems not to receive the parameters. How can I pass them? I already tried to modify the actionView method in the GarageController to set the URL-parameters in the model as it is done in the CarController. But this didn't work.
Can someone help me out? And: is this a good practise of reusing the CGridView? Or is there a better way? I am pretty new to yii but I am very impressed already.
Posted 16 February 2013 - 11:28 AM
You want your CarController to be called in a view rendered by GarageController. Be sure that the controller is explicitly set in your grid, like 'car/admin'.
Posted 16 February 2013 - 11:41 AM
Do I really want this? I tried setting the ajaxUrl to car/admin like you mentioned but now when I do filter I get redirected to car/admin. But I want to stay on garage/view and have the table-widget refreshed here. Curious: Sorting works on the table in garage/view, but filtering still doesn't.
Posted 16 February 2013 - 02:11 PM
Well, actually I try to avoid mixing responsibilities and have a component instead of a controller, that is why I thought that you wanted to call CarController method from the view rendered by GarageController, sorry if I pointed you in the wrong direction...
As for the filtering, the variables are supposed to get catched by the CGridView component if you follow the naming conventions... should work.
Posted 16 February 2013 - 03:37 PM
$carModell = Car::model(); $carModell ->attributes = $_GET['Car']; $this->renderPartial('application.views.car._table',array( 'model'=>$carModell, ...
Now the text in the filters remain for the "simple" database values. But the Car model contains some aggregated values like bookingCount. This is calculated by a subquery in the search method. The filters for those values are not kept. I have to set those values explicitly like the attributes:
$carModell -> bookingsCount = $_GET['Car']['bookingsCount'];
Well thanks clapas for your support. My problem is solved now. The solution is clear to me, and I like yii as much as before my problem.