Generate Serial numbers in CGridView

I need to display Serial numbers starting from 1,2… in CGrideView.

Any idea??

Thanks :)

If you need a serial number, save it db, you can use an autoincrement field.

If you want just to display the number of row, in the property value of CDataColumn you can use the variable $row

Thanks Zaccaria.

I did





array('header'=>'SN.',

	  'value'=>'++$row',

 ),



It works fine.

And I couldn’t understand about the first option: ‘[color=#1C2837][size=2]If you need a serial number, save it db, you can use an autoincrement field.[/size][/color]’

It means that you can use just the AUTOINCREMENT FIELD on the database as your serial number. So every element has its own and unique serial number, because, what would happen if you change the order of the elements on the CGridView?

Cheers

Thank, Antonio for your explanation. I’d like to add that a real serial number should change even if a previous record is deleted, so the only solution is to save in db.

[s]


'value'=>'++$row'

[/s]


 'value'=>'++$id'

In this case SN will not change

Of corse there is a typo, I mean, should NOT change

I got the serial no 1–10 but i dont get next page 11–20 , once again i get 1–10

  1. Create IndexColumn.php into components folder



<?php


Yii::import('zii.widgets.grid.CGridColumn');


class IndexColumn extends CGridColumn {


	public $sortable = false;


	public function init()

	{

		parent::init();

	}


	protected function renderDataCellContent($row,$data)

	{

		$pagination = $this->grid->dataProvider->getPagination();

		$index = $pagination->pageSize * $pagination->currentPage + $row + 1;

		echo $index;

	}

	

}



  1. Add IndexColumn to your grid view:



$this->widget('zii.widgets.grid.CGridView', array(

    ...

    'columns'=>array(

	array(

            'class'=>'IndexColumn',

        ),

        // other columns here

        ...

    ),

));



Hope it would help you.

thanks Weavora Team :D

Thank You Sir , It is Very help me .

Thank You Sir It works Great…

Sir If i am adding name in the column then it prompts me error.

Any Suggestion for adding Heading of IndexColumn

Thanks in Adavance

Cheeeeer,it’s working, amazing.

You just have to add one Line in view\admin.php :)




'columns' => array(

    array(

      'header'=>'No.',

      'value'=>'$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',

   ),

...

)

Thanks!

Weavora Team, how to use the same in widget "ext.EExcelView.EExcelView" as i m getting error( Property "IndexColumn.value" is not defined. ) in excel view mode.

Excelente es la mejor solucion que he encontrado. Saludos desde Colombia