yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

Grid pagination with nested Pjax problem #9336

Closed re1naldo closed 9 years ago

re1naldo commented 9 years ago

I have a grid that displays certain data. Each row of that grid can be expanded to display child grids (products, goods, etc.) which are related to that grid: alt text

All grids use Pjax and most features (sorting, updating, deleting) usually works fine after I set linkSelector of Pjax. However, pagination of child grids seemed to be broken. If I clicked different page, it refreshed the whole page and added pagination to the parent grid.

Parent grid (using Grid from kartik):

GridView::widget([
    'id' => 'arrangement-gridview',        
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'pjax' => true,
    'pjaxSettings' => [
        'options' => [
            'id' => 'arrangement-grid', 
            'timeout' => Yii::$app->params['timeout'], 
            'linkSelector' => '#arrangement-grid a:not([rel=fancybox], .item-link)',
            'enablePushState' => false
        ]
    ],
    'columns' => [           
        [
            'class' => 'kartik\grid\ExpandRowColumn',
            'expandOneOnly' => true,
            'value' => function ($model, $key, $index, $column) {
                return GridView::ROW_COLLAPSED;
            },
            'detail' => function ($model, $key, $index, $column) {
            // render child grids                                        
                return Yii::$app->controller->renderpartial('/detail/index', [
                    ...
                ]);
            }
        ],
        (other columns)                        
    ],
]);

Child grid (using default GridView):

Pjax::begin([
    'id' => $productId,
    'timeout' => Yii::$app->params['timeout'], 
    'enablePushState' => false,
]);    
    echo GridView::widget([
         'id' => $productGrid,
         'dataProvider' => $productData,
         'pager' => [
             'class' => 'yii\widgets\LinkPager',
             'linkOptions' => ['class' => 'item-link']
        ],
        'columns' => [
             [
                 'class' => 'yii\grid\DataColumn',
                 'attribute' => 'price',
                 'sortLinkOptions' => ['class' => 'item-link'],
            ],
            (other columns)
       ],
   ]);   
Pjax::end();

Did I do anything wrong? Was the pagination broken because of nested Pjax? I tried to use renderAjax instead of renderPartial when rendering child grids, but it broke the whole page.

dynasource commented 9 years ago

make sure the different pagination widgets dont conflict. You can use the pageParam http://www.yiiframework.com/doc-2.0/yii-data-pagination.html#$pageParam-detail

If that doesnt work, please use 2 Yii2 grids . If that does work, its beyond the scope of this issuetracker and perhabs ask @kartik-v at his forum.

re1naldo commented 9 years ago

Solved by setting route and param of pagination:

 $dataProvider = new ActiveDataProvider([
     'query' => $query,
     'pagination' => [
        'route' => '...',
        'params' => $params
    ]
]);

When there are multiple Pjax in 1 page, it seems the generated pagination URL follows the current URL.

@dynasource Thanks for your suggestion!