2

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
                ]);
            },
        ],
    ],
    ....
)];
SAQIBZAFAR
  • 89
  • 2
  • 10

2 Answers2

5

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 : '',
                ]);
            },
        ],
    ],
    ....
)];
Fabrizio Caldarelli
  • 2,982
  • 11
  • 14
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,
                ]);
            },
        ],
    ],
    ....
)];
Insane Skull
  • 9,220
  • 9
  • 44
  • 63