虽然在GridView小部件中并显示我从自定义查询中获取的ActiveDataprovider结果,但我想在显示记录时获取下一行和上一行$ model-> id,我需要做的是显示指向上一个和下一个的链接在阅读模式下发布文章时,我的gridview已经生成了到文章视图的链接,但是似乎没有选择获取下一个数据库行的记录ID
我已经尝试在可用函数中使用默认值,但是它们似乎不保存任何相关数据
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
]);
},
],
],
....
)];
答案 0 :(得分:3)
尝试在$dataProvider
内部传递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 : '',
]);
},
],
],
....
)];
答案 1 :(得分:2)
由于$index
是模型的正确密钥,因此我们可以执行类似的操作。不需要在每次通话中都叫$models = $dataProvider->models;
来加快通话速度。
$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,
]);
},
],
],
....
)];