I had to partially render two types of views, depending on user selection. Wanted to display a GRID or a LIST style with an event that AJAX'ed new contents and update a layer with them.
The Grid View
The List View
If you see the buttons on the right of the box, you will identify those who change from Grid to List and viceversa.
Until then all was easy, the issue came when both views had to be processed to render the scripts that provide the dynamics of the grid or the list rendered. I couldn't do it on AJAX calls as I realized that every time a request was made and the partial views had its contents processed (review parameters of renderPartial), the scripts where downloaded again and again, making sometimes multiple function binding to the buttons.
The only way to do it was to register the required scripts of both partial views on the view that will render the buttons that perform AJAX requests. This is how I did it:
// workaround to register required scripts for grid and list // $view is a variable with selected or primary option // and _list and _grid where the two partial views to render $fake = $this->renderPartial(($view==='_grid'?'_list':'_grid'), array('model'=>$model), true, true); echo $this->renderPartial($view, array('model'=>$model), true, false);
As you can see, $fake is used to process the partial view required scripts and hold the contents that will not be rendered. After doing this, AJAX calls to the controller can return the contents of views without the need to process its output anymore:
public function actionIndex() { $model = new MyModel('search'); // style to display? $r = Yii::app()->getRequest(); $view = $r->getParam('style',false)?'_'.$r->getParam('style') : '_grid'; if($r->getIsAjaxRequest()) { // now we render the views without the need to process // output anymore $this->renderPartial($view,array('model'=>$model)); Yii::app()->end(); } $this->render('index', array('model'=>$model,'view'=>$view)); }
Total 4 comments
What you need to do is to render the grids normally on your first render and when you renderPartial the ajax requests you DO NOT process output (check fourth parameter of the function).
The strange repeats is due that the scripts are re-rendered thus re-binding the js events to the elements on the page.
Hi,
I don't really get your explanation, where does the code go? I don't get the connection between the first and the second part.
my problem: I have two grids in one page, each of them first rendered without ajax-call on page-request. After this, ajax request to replace the grids using partialRender.
When the link with the ajax request is clicked, it goes well, after this, i get strange repeats of the requests.
prchakal
This is a commercial theme design
Hi,
Can you give the layout (html/css/js) of the pages that is showed in your screenshot?
Thanks.
Leave a comment
Please login to leave your comment.