You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
Introduction ¶
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.
Views exposed ¶
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.
The issue ¶
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 solution ¶
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));
app()->end();
}
$this->render('index', array('model'=>$model,'view'=>$view));
}
Layout
Hi,
Can you give the layout (html/css/js) of the pages that is showed in your screenshot?
Thanks.
Re: Layout
prchakal
This is a commercial theme design
Code relation/place
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.
Re: Code relation/place
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.
Script
You have not explained full how use this method, how i can check if style or scripts exist, for send false from frontend.
@styopdev
This is an error that normally occurred when you (on the same controller's action) have a
render
andrenderPartial
withprocessOutput
parameter set totrue
.If you have previously rendered the main view with
render
all your assets are registered and if, on the same controller's action, you callrenderPartial
with process output, the same files are registered by CClientScript behind the scenes (as it happens with the main rendering).That means that the same asset files are re-registered, once on the main render and again with renderPartial with output process set to true.
Hope this helps to clarify
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.