Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the absolute row number in a grid

Tags:

gridview

yii

In yii's CGridView I can get the current row number by using $row. But this returns the row index within the current page only. What I really need is to get the absolute row number among all pages.

I am using yii, so my dreams should come true "easily", so I expect the answer should not guide me to add a special field to the data provider or access the pager and get the current page number and then multiply numbers bla bla bla.

Thanks

like image 368
mmonem Avatar asked Apr 29 '11 16:04

mmonem


1 Answers

Have you solved this? If not, I offer you the following solution

$this->widget(
  'zii.widgets.grid.CGridView',
  array(
    'columns'=>array(
      array(
        'header'=>'No.',
        'value'=>'$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
      ),
    ),
  ));

basically you can access the currentpage and pagesize variables from the dataprovider and you use it to calculate the 'row number' for the whole data. Why the $row + 1? because $row starts from 0. Hope this helps :D

like image 190
ZaQ Avatar answered Sep 24 '22 22:09

ZaQ