Yii custom update for GridView widget

Hooking into the ajax functions in Yii’s GridView and ListView widgets can be a bit tricky and the documentation isn’t always that clear. To do a simple custom filter, you can use something like this:

<?php echo CHtml::dropDownList('Holidays[date]', $model->date, 
  ), array('onchange'=>"$.fn.yiiGridView.update('holidays-grid', {data: $(this).serialize()});") ); ?>

Where “Holiday” is the name of the model used to populate the widget and “holidays-grid” is the html ID of the widget. (In real code I would fetch the drop-down values dynamically, but this shows how simple it can be.)

One somewhat undocumented issue is that you must have a particular URL manager rule set or you will get unpredictable results when combining this with sorting and paging:

see: www.yiiframework.com/…/19738-filter-after-sorting-problem-at-gridview/
According to Qiang, “for gridview to work properly in ajax mode, you have to add a URL rule like the following if you use “path” format:”

<controller:\w+>/<action:\w+>' => '<controller>/<action>

This will put all filter conditions into the query string part instead of the pathinfo part.

I spent a bit of time coming up with regex work-around until I found that :/

Leave a Reply