Aladdin Mhemed
Aladdin Mhemed

Reputation: 3971

Yii cgridview ajaxUpdate property

I have one cgridview inside another cgridview: the outer one is:

$this->widget('application.modules.user.components.CsvGridView', array(
    'dataProvider'=>$model->mySearch(),
    'filter' => $model,
    'id'=>'users-grid',
...

the inner one is:

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'vacr-grid',

    'dataProvider'=>$model->searchu(),
   // 'pager'=>false,
   // 'enableSorting'=>false,
    'summaryText'=>'',
    'ajaxUpdate'=>'vacr-grid',

    'columns'=>array(

        'start',
        'end',
                array(
                        'name' => 'vac_type',
                        'value' =>
                        'isset($data->vacType->name)?$data->vacType->name:$data->vac_type'
                ),
    ),
));

The problem is that the inner one does not update properly (of course update with ajax).

when I click on "next" for example, in the inner cgridview, the requested URL is: Request URL:http://localhost/orm/vac/vac/adminu/user_id/1/Vac_page/2?ajax=users-grid
notice that users-grid is the id of the outer cgridview. and then after I click on "next" the inner and the outer cgridviews both disappears.

I try to use ajaxUpdate property but it does not affect anything. thank you.

Upvotes: 0

Views: 6769

Answers (2)

Piemol
Piemol

Reputation: 896

Two very important configuration options of CGridView when nesting them are the updateSelector and filterSelector ! On default, these selectors target all of the available paging/sorting links within a GridView. So clicking a sorting link in a nested gridview also causes all parent GridViews to update. So you'll have to adjust those selectors if that's a problem (it probably is) to only select direct descendants of the gridview and NOT ALSO those of the child gridview. Vice versa is not a problem if the child gridview has another ID.

So you're parent GridView (mostly needs) selectors like this (notice the use of the 'greater than' symbol):

$this->widget('CGridView',array(
    ...
    'updateSelector' => '#PARENTGRIDVIEW > .items.table > thead tr th a.sort-link, #PARENTGRIDVIEW > .pagination a',
    'filterSelector' => '#PARENTGRIDVIEW .filters input, #PatientSaleMedicalsIndex .filters select',

Now the child gridview can update independent from the parent. Note: The gridview id of the child has to reappear in the ajax request, else it's contents will not be placed within the page/gridview firing the request!

Upvotes: 0

111
111

Reputation: 1918

This is a similar issue to one I was having. The sort and paging urls are set by the dataProvider whereas the search url is set by ajaxUrl. You have to set all 3 to use CGridView out of context.

dataprovider set separately:

$dataProvider=new CActiveDataProvider('Modelname',array(
    'criteria'=>$criteria,
    'pagination'=>array(
        'route'=>'something/search'
    ),
    'sort'=>array(
        'route'=>'something/search'
    )
));




$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'vacr-grid',    
    'dataProvider'=>$dataProvider,
    'summaryText'=>'',
    'ajaxUpdate'=>'vacr-grid', // not necessary if same as id
    'ajaxUrl'=>Yii::app()->createUrl( 'Something/search' ),  // this takes care of the search  
    'columns'=>array(    
        'start',
        'end',
                array(
                        'name' => 'vac_type',
                        'value' =>
                        'isset($data->vacType->name)?$data->vacType->name:$data->vac_type'
                ),
    ),
));

Also my similar question...

How do I use the ajaxUrl parameter of CGridView in Yii?

Upvotes: 3

Related Questions