Yii2 DataGrid为第一行和最后一行设置的不同按钮

时间:2018-03-12 12:39:49

标签: php datagrid yii2

我需要重新排序DataGrid中的行,所以我为每行'Up'和'Down'添加了两个按钮:

        <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            [
                'headerOptions' => ['style' => 'width:56px'],
                'class' => 'yii\grid\ActionColumn',
                'template' => '{Up} {Down}',
                'buttons' => [
                    'Up' => function($url, $model, $key) {
                        return Html::a('',['/admin/content/up', 'id' => $key], ['class' => 'glyphicon glyphicon-chevron-up']);
                    },
                    'Down' => function($url, $model, $key) {
                        return Html::a('',['/admin/content/down', 'id' => $key], ['class' => 'glyphicon glyphicon-chevron-down']);
                    },
                ]
            ],
            'content'
        ]
    ]);
    ?>

没有分页,所有数据都适合一页。现在看起来像:

Current look

我想让它看起来更整洁:

What I want look

所以我需要分别为第一行和最后一行隐藏“向上”和“向下”按钮。无法获得如何制作它甚至如何从匿名按钮功能获取行号。

1 个答案:

答案 0 :(得分:0)

试试这个

$count = $dataProvider->getTotalCount(); 

之后,在您的操作列

中添加此属性
'visibleButtons' => [
    'Up' => function ($model, $key, $index) {
         return $index == 0 ? false : true; 
     }, 
    'Down' => function ($model, $key, $index) use ($count) { 
         return $index == ($count - 1) ? false : true; 
     }
]