2016-03-19 48 views
1

我想在我的網格視圖中顯示行號,但我找不到方法來執行此操作。Yii2在網格視圖中顯示行號

我發現這個問題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, 
     ], 
    ]); 

回答

5

你需要['class' => 'yii\grid\SerialColumn'],

<?= GridView::widget([ 
    'dataProvider' => $dataProvider, 
    'columns' => [ 
     ['class' => 'yii\grid\SerialColumn'], 
     'username', 
     'total', 
    ], 
    'summary'=>'', 
]); ?>