0

I have following code in my controller:

public function actionAdmin()
{
    $model=new MForm('search');
    $model->unsetAttributes();  // clear any default values
    if(isset($_GET['ChManageForm']))
        $model->attributes=$_GET['ChManageForm'];

    $this->render('admin',array(
        'model'=>$model,
    ));
}

and

const member=1;
const  district=2;

My view(called admin)

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'ch-manage-form-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'id',
        'form_name',
        'region',
        'phone_number',
        'email',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

1 and 2 appears in the column called name(in my view file) and values of this column are saved in integer format. I need to show(in my view(admin)) member and district instead of numbers(e.g member instead of 1). How can I do it?

phpdev
  • 511
  • 4
  • 22

1 Answers1

0

You can also specify the grid view column like this (doesn't require a helper in your model):

array(
    'name'=>'form_name',
    'value'=>'$data->form_name ? \'Member\':\'District\'',
    'type'=>'text',
),

this works great if you only have 2 value in the variable.

Visit this URL to get more details.

http://www.yiiframework.com/forum/index.php/topic/14845-if-condition-inside-cgridview/

EDIT:

IF you have more than 2 values,

Model which you use to CGridView:

public function getValueText() {
        return $this->getValueTextOptions[$this->form_name];
}

public function getValueTextOptions() {
        return array(
                1 => 'Member',
                2 => 'District',
        );
}

View with CGridView:

array(
    'name'=>'form_name',
    'value'=>'$data->getValueText()',
)
Dharmesh Goswami
  • 1,155
  • 2
  • 13
  • 30