How to Display Serial Number in ascending order in CGridview in Yii without taking a extra database field

StackOverflow https://stackoverflow.com/questions/19092335

  •  29-06-2022
  •  | 
  •  

Question

How to Display Serial Number in ascending order like

1 Record Abc
2 Record Def
3 Record Xyz

in CGridview in Yii without taking a extra database field ?

Was it helpful?

Solution 2

Hope this helps out:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'id'=>'accounts-grid',
    'columns'=>array(
        array(
            'header'=>'Serial Number',
            'value'=>'$row+1',
        ),
        'bank',
        'branch',
        'account',
        array(
            'class'=>'CButtonColumn',
            'template'=>'{update} {delete}',
            'htmlOptions'=>array('style'=>'width:40px;'),
            'updateButtonUrl'=>'Yii::app()->controller->createUrl("index", array("update"=>$data->uniq_id))',
            'headerHtmlOptions'=>array('style'=>'width:40px;'),
        ),
    ),
    'summaryText'=>''
)); ?>

The $row variable is a zero-based row number

OTHER TIPS

It will work with pagination as well

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

I found better answer with sort and filter.

$this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'status-grid',
        'dataProvider'=>$dataProvider,
        'columns'=>array(  
     array(
            array(
               'name'=>'id',
               'value'=>'$this->grid->dataProvider->getSort()->getDirection($this->name) != "desc" ? $this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1) : $this->grid->dataProvider->pagination->itemCount - $this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize - ($row)',
           ),
     //Other fields
    )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top