我想在网格视图中显示行号,但我无法找到实现此目的的方法。
我发现了这个问题Get the absolute row number in a grid,但似乎是针对Yii1。
这就是我的网格视图
<?php Pjax::begin(['id' => 'leaderboard-pjax']); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'username',
'total',
],
'summary'=>'',
]); ?>
<?php Pjax::end(); ?>
和我的数据提供者
$dataProvider = new ActiveDataProvider([
'query' => (new \yii\db\Query())
->select('user.username, score.total')
->from('user')
->leftJoin('score', 'score.user_id = user.id')
->where(['user.role' => User::ROLE_USER])
->orderBy('total DESC'),
'pagination' => [
'pageSize' => 10,
],
]);
答案 0 :(得分:6)
您需要['class' => 'yii\grid\SerialColumn'],
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'username',
'total',
],
'summary'=>'',
]); ?>