Ajax update

Often happens to have the need of change a part of the page with ajax.

For example, having a code like:

<div id="forAjaxRefresh"></div>

<?php echo CHtml::ajaxLink('clickMe', array('ajax'), array('update'=>'#forAjaxRefresh'));?>

It will call the actionAjax in the controller and refresh the content of the div.

What about ClientScript?

And if the content we are going to replace has some script itself?

In this case the actionAjax should send even the client script needed, this can be done by setting the fourth parameter of renderpartial to true:

public function actionAjax()
{
   $this->renderPartial('ajaxView', array(), false, true);
}

The fourth will make so all client script will be processed and wrote at the end of the html widget.

Pay attention that the ID are authomatically generated by yii by incrementing a counter, when you do your renderPartial, there is the possibility that the newly generated id will confilct with the id of the other part of the page.

If you have some problem with the clientscripted object (link, jui widget and so on) after an ajax update, it is possible that you have to set the id to all this items, in order to avoid this conflicts.

16 2
14 followers
Viewed: 57 977 times
Version: 1.1
Category: Tips
Tags: AJAX
Written by: zaccaria
Last updated by: zaccaria
Created on: Oct 22, 2010
Last updated: 13 years ago
Update Article

Revisions

View all history

Related Articles