Yii Framework Forum: Show number list and set value in CGridView - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Show number list and set value in CGridView Rate Topic: -----

#1 User is offline   arsitek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 127
  • Joined: 10-October 08

Posted 15 March 2010 - 07:05 PM

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      |
.......
.......

0

#2 User is offline   Doug Swain 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 28-December 09

Posted 15 March 2010 - 08:01 PM

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',
        ),
),
)); ?>

1

#3 User is offline   arsitek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 127
  • Joined: 10-October 08

Posted 15 March 2010 - 08:30 PM

View PostDoug Swain, on 15 March 2010 - 08:01 PM, said:

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', //edited
            'value' => '$data->published ==0 ? "No" : "Yes"', //edited
        ),
        array(
                'class'=>'CButtonColumn',
        ),
),
)); ?>



Thanks you Doug Swain, It's work!

But how to add 'Number' column and get the number list?
0

#4 User is offline   Doug Swain 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 28-December 09

Posted 15 March 2010 - 08:40 PM

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!
0

#5 User is offline   arsitek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 127
  • Joined: 10-October 08

Posted 15 March 2010 - 08:56 PM

View PostDoug Swain, on 15 March 2010 - 08:40 PM, said:

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 "Undefined variable: pages" 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',
		),
	),
)); ?>

0

#6 User is offline   Doug Swain 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 28-December 09

Posted 15 March 2010 - 09:01 PM

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.
0

#7 User is offline   arsitek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 127
  • Joined: 10-October 08

Posted 15 March 2010 - 09:08 PM

View PostDoug Swain, on 15 March 2010 - 09:01 PM, said:

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,
		));

0

#8 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,338
  • Joined: 12-October 09
  • Location:Croatia

Posted 16 March 2010 - 04:48 AM

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

http://www.yiiframew...mn#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',
                ),
        ),
)); ?>

Find more about me.... btw. Do you know your WAN IP?
2

#9 User is offline   arsitek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 127
  • Joined: 10-October 08

Posted 16 March 2010 - 05:06 AM

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.
0

#10 User is offline   arsitek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 127
  • Joined: 10-October 08

Posted 17 March 2010 - 01:21 PM

Any have idea?
0

#11 User is offline   Artit P. 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 18-May 10

Posted 18 May 2010 - 10:27 PM

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',
                ),
        ),
)); ?>

0

#12 User is offline   bega 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 07-March 10

Posted 05 February 2011 - 06:26 PM

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

View PostArtit P., on 18 May 2010 - 10:27 PM, said:

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',
                ),
        ),
)); ?>


0

#13 User is offline   Twisted Whisper 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 21-June 13

Posted 06 July 2013 - 05:58 PM

View PostArtit P., on 18 May 2010 - 10:27 PM, said:

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',
                ),
        ),
)); ?>



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',
                ),
        ),
)); ?>

1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users