Show number list and set value in CGridView

In Controller:




public function actionAdmin()

{

	$dataProvider=new CActiveDataProvider('sections');

	$this->render('admin',array(

		'dataProvider'=>$dataProvider,

	));	

}



In view:




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

'dataProvider'=>$dataProvider,

'columns'=>array(

	'id',

	'name',

	'published',

	array(

		'class'=>'CButtonColumn',

	),

),

)); ?>



In default view will show like this:


| Id | Name      | Publised |

+----+-----------+----------+

| 1  | Section 1 | 0        |

| 3  | Section 2 | 1        |

.......

.......



How if I want show table like this?




| No| Id | Name      | Publised |

+---+----+-----------+----------+

| 1 | 1  | Section 1 | No       |

| 2 | 3  | Section 2 | Yes      |

.......

.......

There is a really nice way to do this with CGridView. With CGridView, you can pass in an expression in each columns "value" field. In addition, you can make use of the data being processed by the CGridView via the "$data" variable within CGridView. In your case:




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

'dataProvider'=>$dataProvider,

'columns'=>array(

        'id',

        'name',

        array(

            name => 'published',

            value => '$data->published == 0 ? "No" : "Yes"',

        ),

        array(

                'class'=>'CButtonColumn',

        ),

),

)); ?>



Thanks you Doug Swain, It’s work!

But how to add ‘Number’ column and get the number list?

I’m not sure about this and I haven’t tried it myself and I’m not exactly sure I understand what you’re looking for exactly, but I would try this - Tell CGridView to create a new column with a header of “Number” and then tell CGridView to make the value of the “Number” column the substring of the “Name” column.




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

'dataProvider'=>$dataProvider,

'columns'=>array(

        'id',

        array(

            'header' => 'Number',

            'value' => 'substr( $data->name, -1, 1 )',

        ),

        'name',

        array(

            'name' => 'published',

            'value' => '$data->published == 0 ? "No" : "Yes"',

        ),

        array(

                'class'=>'CButtonColumn',

        ),

),

)); ?>



Again, I haven’t tested this so it may very well not work, but that would be my first guess. Good luck!

The new header column is work, but not the value.

In normal way the number count by


$pages->pageSize*$pages->currentPage;

but error of [color="#FF0000"]"Undefined variable: pages"[/color] is show up.

Last codes:




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

	'dataProvider'=>$dataProvider,

	'columns'=>array(

		array(

            'header' => 'Number',

            'value' => '$pages->pageSize*$pages->currentPage',

        ),

		'id',

		'name',

		'sections.name',

		array(

            'name' => 'published',

            'value' => '$data->published == 0 ? "No" : "Yes"',

        ),

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



Right. The reason you see that “Undefined variable” error is because the string you put as the value is actually evaluated by CGridView. While $pages exists in the view, it does not exist in that instance of CGridView. You’ll have to use whatever information you have within $data to perform what you’re trying to do.

I just put ‘pagination’ class in controller, but it’s not working. Now, I dont’have idea to solve this:

In controller:




$dataProvider=new CActiveDataProvider('categories',array(

		'pagination'=>array(

			'pageSize'=>self::PAGE_SIZE,

	)));

$this->render('admin',array(

			'dataProvider'=>$dataProvider,

		));



If you need just to display a column with row numbers you can use the $row variable for the value property

http://www.yiiframework.com/doc/api/CDataColumn#value-detail

eg.




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

        'dataProvider'=>$dataProvider,

        'columns'=>array(

                array(

                'header'=>'Number',

                'value'=>'$row+1',       //  row is zero based

        ),

                'id',

                'name',

                'sections.name',

                array(

            'name' => 'published',

            'value' => '$data->published == 0 ? "No" : "Yes"',

        ),

                array(

                        'class'=>'CButtonColumn',

                ),

        ),

)); ?>

Thanks mdomba… :)

Your code working just in the first page, in the second page the number begining in 1 again. If we have 10 list for each page, in second page should be begin in number 11.

Any have idea?

I use following code to handle row number in pagination




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

        'dataProvider'=>$dataProvider,

        'columns'=>array(

                array(

                'header'=>'Number',

                'value'=>'$this->grid->dataProvider->pagination->currentPage*$this->grid->dataProvider->pagination->pageSize + $row+1',       //  row is zero based

        ),

                'id',

                'name',

                'sections.name',

                array(

            'name' => 'published',

            'value' => '$data->published == 0 ? "No" : "Yes"',

        ),

                array(

                        'class'=>'CButtonColumn',

                ),

        ),

)); ?>



@Artit P : It’s work, thank you very much

A simpler method:




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

        'dataProvider'=>$dataProvider,

        'columns'=>array(

                array(

                'header'=>'Number',

                'value'=>'$this->grid->dataProvider->pagination->offset + $row+1',       //  row is zero based

        ),

                'id',

                'name',

                'sections.name',

                array(

            'name' => 'published',

            'value' => '$data->published == 0 ? "No" : "Yes"',

        ),

                array(

                        'class'=>'CButtonColumn',

                ),

        ),

)); ?>