Yii 1.1: Ajax update

14 followers

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.

Total 2 comments

#15168 report it
trond at 2013/10/14 08:21am
Conflicting IDs

I think it it generally a good idea to specify custom IDs with the 'htmlOtions' parameter when using renderPartial to update the page.

#2884 report it
diggy at 2011/02/21 03:46pm
If you don't want to include jquery in returned html

If you do not want to include jquery script code into returnned html code you can do in view:

Yii::app()->clientscript->scriptMap['jquery.js'] = false;

Leave a comment

Please to leave your comment.

Write new article
  • Written by: zaccaria
  • Category: Tips
  • Yii Version: 1.1
  • Votes: +21 / -2
  • Viewed: 39,502 times
  • Created on: Oct 22, 2010
  • Last updated: never
  • Tags: AJAX