How to get previous and next row model ID in yii2 gridview data row

早过忘川 提交于 2019-12-24 10:37:59

问题


While in GridView widget and displaying ActiveDataprovider results that i fetched from a customized query, i want to get next and previous row $model->id while displaying the records, what i need to do is to show links to previous and next posts while in reading mode, my gridview already generates link to the post view but there doesnt seem to be an option to get next DB row's record ID

i have already tried to use default values in the function available but they dont seem to hold any related data

GridView::widget([
    'dataProvider' => $dataProvider,
    ...
    'columns' => [
        ...
        [
           'label'=>'Post Name',
           'format' => 'raw',
           'value'=>function ($data, $key, $index, $obj) {
                // this is what i have right now
                return Html::a($data->name, ['view', 'id' => $data->id]);

                //ideally i would like something like this
                return Html::a($data->name, ['view', 
                     'id' => $data->id,
                     'prev' => $prev->id,
                     'next' => $next->id
                ]);
            },
        ],
    ],
    ....
)];

回答1:


Try passing $dataProvider inside value callback:

GridView::widget([
    'dataProvider' => $dataProvider,
    ...
    'columns' => [
        ...
        [
           'label'=>'Post Name',
           'format' => 'raw',
           'value'=>function ($data, $key, $index, $obj) use($dataProvider) {


                // Check prev and next object
                $models = $dataProvider->models;
                $prev = ($index>0) ? $models[$index-1] : null;
                $next = ($index < ((count($model)-1)) ? $models[$index+1] : null; 

                //ideally i would like something like this
                return Html::a($data->name, ['view', 
                     'id' => $data->id,
                     'prev' => ($prev != null) ? $prev->id : '',
                     'next' => ($news != null) ? $next->id : '',
                ]);
            },
        ],
    ],
    ....
)];



回答2:


As $index is correct key for models, We can do something like this. Not required to call $models = $dataProvider->models; in each call makes it faster.

$keys = $dataProvider->keys;

GridView::widget([
    'dataProvider' => $dataProvider,
    ...
    'columns' => [
        ...
        [
           'label'=>'Post Name',
           'format' => 'raw',
           'value'=>function ($data, $key, $index, $obj) use ($keys) {


                // Check prev and next object
                $prevId = ($key > 0) ? $keys[$index-1] : '';
                $nextId = ($key < ((count($keys)-1)) ? $keys[$index+1] : '';

                return Html::a($data->name, ['view',
                    'id' => $data->id,
                    'prev' => $prevId,
                    'next' => $nextId,
                ]);
            },
        ],
    ],
    ....
)];


来源:https://stackoverflow.com/questions/54399991/how-to-get-previous-and-next-row-model-id-in-yii2-gridview-data-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!