Search code examples
phpjqueryyiipaginationcgridview

Yii CGridview Pagination renderpartial


I'm currently rendering 3 CGridviews in my view, all using "renderPartial" and the same php file. This works fine, except for when using pagination, and the user selects another page of results, it moves all the CGridviews to that page (if they can). I'm not sure why this is happening or how to fix it.

I tried this approach post on Yii forms, and it does fix the pagination of the CGridViews, but breaks my ajax functions and css styles. See below for an image of this occurring.

Here is the CGridView Template

<div class="row btn-row">
<h2 class="admin-h1"><?php echo $title; ?></h2>
<div class="btn-block-div">
    <button class="btn addOrgBtn" id="<?php echo $title; ?>">Add</button>
</div>
</div>
<?php 
$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>$title .'-table',
    'dataProvider'=>$dataProvider,
    'selectableRows'=>1,
    'hideHeader'=>true,
    'selectionChanged'=>'viewRoles',
    'columns'=>array(
        array(
            'header'=>'Name',
            'name'=>'Organization',
        ),
        'Roles',
    ),
));
?>

Here are the "RenderPartial" statements inside my view:

<div class="col-md-12 col-sm-12 col-xs-12" style="margin-bottom:60px;">
<div class="col-md-4 col-sm-4 col-xs-12" id="School-grid">
    <?php echo $this->renderPartial('_roleTable', array('title'=>'School', 'model'=>$model, 'dataProvider'=>$schoolItems)); ?>
</div>
<div class="col-md-4 col-sm-4 col-xs-12" id="Classroom-grid">
    <?php echo $this->renderPartial('_roleTable', array('title'=>'Classroom', 'model'=>$model, 'dataProvider'=>$classroomItems)); ?>
</div>
<div class="col-md-4 col-sm-4 col-xs-12" id="Shared-grid">
    <?php echo $this->renderPartial('_roleTable', array('title'=>'Shared', 'model'=>$model, 'dataProvider'=>$sharedItems)); ?>
</div>
</div>

This is what is currently happening: enter image description here

And this is what happens when I add "false, true" to my "RenderPartial" statements like the Yii answer: enter image description here

Thanks in advance for the help, I'm really lost on this issue.

UPDATE For reference, I also asked this question here on the Yii forms, as I find SO has much better response time, but the Yii forms have the concentrated community.


Solution

  • The problem was a combination of things.

    First off, I needed to set the "updateSelector" attribute of CGridView to a custom page selector for each table.

    Secondly, I needed to set the "pageVar" in my "dataProvider" to match the selector.

    Here is a static example of this to make it as clear as possible.

    DataProvider:

    new CArrayDataProvider($data, array(
            'sort'=>array(
                'attributes'=>array(
                    'Organization',
                    'Roles',
                ),
            ),
            'pagination'=>array(
                'pageSize'=>10,
                'pageVar'=>'custom-page-selector', //page selector
            ),
        ));
    

    CGridView:

    <?php 
    $this->widget('zii.widgets.grid.CGridView', array(
        'id'=>$title .'-grid',
        'dataProvider'=>$dataProvider,
        'selectableRows'=>1,
        'updateSelector'=>'custom-page-selector', //update selector
        'columns'=>array(
            array(
                'header'=>'Name',
                'name'=>'Organization',
            ),
            'Roles',
        ),
    ));
    ?>
    

    Hope this helps someone in the future!