Question

does somebody know a possibilty to set a default function in CGridView, which will run on all grids on the page after every AJAX update? I use the CGridView on many pages, and I don't want to specify this function for each grid separatly. I need this because I use jQuery selectmenu for my filter dropdowns, and after the AJAX reload, they need to be inicialized again.

'afterAjaxUpdate' => "function(id,data){$('select').selectmenu()}";
Was it helpful?

Solution

See the format of value http://www.yiiframework.com/doc/api/1.1/CGridView#afterAjaxUpdate-detail

a javascript function that will be invoked after a successful AJAX response is received. The function signature is function(id, data) where 'id' refers to the ID of the grid view, 'data' the received ajax response data.

You need set

'afterAjaxUpdate' => "function(id,data){$('select').selectmenu()}";

UPDATED:

I don't see another way than create child of CGridView and set value of $afterAjaxUpdate. Here is the code:

class GridView extends CGridView{
    public $afterAjaxUpdate = "function(id,data){$('select').selectmenu()}";
}

UPDATED:

I look at the source code of widget and the property afterAjaxUpdate used only in the method registerClientScript. That's why I propose anothe two solution. First - you may change value of afterAjaxUpdate in init of inherited class:

public function init(){
    parent::init(); // after setting all values reset value to desire
    $this->afterAjaxUpdate = "function(id,data){$('select').selectmenu()}";
  }

Second - you can rechange it propery before calling method registerClientScript:

public function registerClientScript(){
    $this->afterAjaxUpdate = "function(id,data){$('select').selectmenu()}";
    parent::registerClientScript();
  }

OTHER TIPS

After more research I found, that it really is not possible to set a default afterAjaxUpdate function, which would be fired on all grid views automatically.

There however are 2 possiblities, which one can use:

  1. See answer from CreatoR - set the default event in an extended GridView object. This works as long as noone manualy sets an afterAjaxUpdate in a specific grid.

  2. You could change (but this is not a real nice "extension" way) the jquery.yiigridview.js file to this specific needs, by adding a new option=function, sth like '***afterAjaxUpdateDefault*'**, and place it before or after the 'afterAjaxUpdate' is called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top