Difference between #1 and #3 of
Serial Numbers (item counts) in GridView

Changes

Title changed

Serial Numbers (item counts) in GridView

Category unchanged

How-tos

Yii version unchanged

Tags unchanged

serial number, grid items count

Content changed

[...]
```php
Yii::import('zii.widgets.grid.CGridColumn');

class CounterColumn extends CGridColumn {
 
 


}
[...]
We need to set starter value for the counter so, we have


 
```php private $i = 0;     public function init()
 
{      $pager=$this->grid->dataProvider->pagination;     if($pager->currentPage==0) {      $this->i=0;     } else {     $this->i=$pager->currentPage*$pager->pageSize;     }
 
        
 
    
}
 
} ``` We need to use display the value, so we have
 
```php public function renderDataCellContent($row, $data) {   {     $this->i++;
 
   
  echo $this->i;     }
```
[...]
Yii::import('zii.widgets.grid.CGridColumn');

class CounterColumn extends CGridColumn
 {  { private $i = 0; public function init()
 
    
{ $pager=$this->grid->dataProvider->pagination; if($pager->currentPage==0) { $this->i=0; } else { $this->i=$pager->currentPage*$pager->pageSize; }         
 
} public function renderDataCellContent($row, $data) { // $row number is ignored     { $this->i++;  
 
echo $this->i; } } ``` **Using**  
```
 
[
php]
$this->widget('zii.widgets.grid.CGridView', array(     'id'=>'categories-grid',     'dataProvider'=>$model->search(),     'filter'=>$model,     'columns'=>array(         array(         'header'=>'Sr #',          'class'=>'CounterColumn' ),
 
),
 
'id',         'name', array(
 
    
array(
 
'header'=>'Actions',             'class'=>'CButtonColumn',         )
 
 
)); 
 
~~~
 
 
~~~
 
 
)
 
)); 
 
```
0 0
4 followers
Viewed: 18 492 times
Version: 1.1
Category: How-tos
Written by: PeRoChAk
Last updated by: Maurizio Domba Cerin
Created on: Oct 5, 2012
Last updated: 12 years ago
Update Article

Revisions

View all history