Skip to content


Dynamic update of Yii CGridView

I’ve been exploring the Yii framework for about a week now and I have been very pleased with how extensible the framework and supporting libraries are. My most recent task was to add dynamic AJAX update functionality to a table of information. In Yii, a tabular arrangement of data is constructed using zii.widgets.grid.CGridView. For example:

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'id' => 'item-grid',
    'columns'=>array(
        'title',
        'category.name',
        'content:html',
        array(           
            'name'=>'create_time',
            'value'=>'date("M j, Y", $data->create_time)',
        ),
        array(
            'name'=>'authorName',
            'value'=>'$data->author->username',
        ),
        array(
            'class'=>'CButtonColumn',
        ),
    ),
));

Yii supports dynamic AJAX updates using the yiiGridView api.

$('#item-grid a.submi').live('click',function(){
  $.fn.yiiGridView.update('item-grid', {
           type:'POST',
           url:$(this).attr('href'),
           data: {update:"now"},
           success:function() {
             $.fn.yiiGridView.update('item-grid');
           }
  });
  return false;
})

The update() call issues a new server request passing the extra data variables. The returned results are then parsed and the table item-grid is updated without too much fuss.

The interesting thing about this request, is there are actually two update() calls. The outer call uses POST, and then the inner update() call uses an implicit GET. After monitoring my server logs, it turns out this AJAX call is actually hitting my server with a total of three calls. A POST request, then two GET request, and one POST. After I noticed the duplication, I reduced this to the following:

$('#item-grid a.submi').live('click',function(){
  $.fn.yiiGridView.update('item-grid', {
           type:'POST',
           url:$(this).attr('href'),
           data: {update:"now"},
  });
  return false;
})

I posted a message here on the Yii forums regarding this issue. Time to wait and see if there is a rhyme or reason behind the seemingly duplicate calls..

Posted in PHP. Tagged with .

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.