nicosierra
nicosierra

Reputation: 166

$fn.yiiGridView.update calls for multiple grids

In a view, I display two grids with data. The user is able to submit data in the following way:

  1. The data is sent using a form displayed in the same page
  2. On submit, the form data is sent through an ajax call to a controller
  3. The ajax call has a callback on success that executes

    $.fn.yiiGridView.update("grid1");
    $.fn.yiiGridView.update("grid2");
    

    as both grids will have their data changed by the submitted form.

The $.fn.yiiGridView.update method will update the grids by retrieving the same page and extracting what is required.

In this particular situation, it will happen twice, which raises the need to find a way to avoid this extra call and reduce unnecessary traffic.

Is there a way to modify the yiiGridView callbacks to update both grids with a single call? I've tried to hack the methods, but with no success.

Upvotes: 2

Views: 2382

Answers (1)

nicosierra
nicosierra

Reputation: 166

Ok, after some struggle I have found a solution.

I realise that there's a field called ajaxUpdate that in the yiiGridView documentation (jquery.gridview.js line: 39) says :

ajaxUpdate: array, IDs of the containers whose content may be updated by ajax response

This value is extracted from the CGridView parameters (http://www.yiiframework.com/doc/api/1.1/CGridView#ajaxUpdate-detail)

So I defined

$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'grid1', //or could be grid2
    'dataProvider' => $model->search(),
    'ajaxUpdate'=>'grid1,grid2', //string separated with commas
    ......
));

and now just by calling

$.fn.yiiGridView.update("grid1"); //or grid2 if you set ajaxUpdate in grid2

both grids will get updated.

I wonder if there's a way to override the value using the options parameter for the gridview function (using the $('#grid1').yiiGridView('update',options) syntax)

Upvotes: 3

Related Questions