Yii Cgridview how to render in view 2 or more values from the same table in 1 column

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

  •  26-06-2022
  •  | 
  •  

Question

I have a Cgridview that has multiple columns. I would like to merge and render the values of 3 columns in 1 column.

 <?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'policy-grid',
'dataProvider'=>$dataProvider,
'filter'=>$dataProvider->model,

'selectableRows'   => 1, // you can select only 1 row!!
'selectionChanged'=>'function(id){ var objectId = $.fn.yiiGridView.getSelection(id); 
 if (isNaN(objectId) || objectId == ""){return;} location.href = 
"'.$this->createUrl('policy/view').'&id="+$.fn.yiiGridView.getSelection(id);}',
'htmlOptions'=>array('class'=>'grid-view grid_pointer',),

 'columns'=>array(

  array(
       'name'        => 'compliance',
     'type'        => 'html',
     'value'       => '$data->status == "ACTIVE" ? (CHtml::image($data->compliance == 
    "SUFFICIENT" ? "images/policy_sufficient.png" : "images/policy_insufficient.png")) 
    : "N/A" ',
     'filter'      => VFFormUtil::getFilter(VFFormUtil::POLICY_COMPLIANCE),
     'htmlOptions' => array('style'=>'text-align: center'),
  ),

  array(
       'name'        => 'status',
     'filter'      => VFFormUtil::getFilter(VFFormUtil::ACTIVE_INACTIVE),
     'htmlOptions' => array('style'=>'text-align: center'),
  ),

  array(          

      'name'        => 'coverage_left',
      'htmlOptions' => array('style'=>'text-align: center'),
  ),

  array(
      'name' => 'model_year', 
      'htmlOptions' => array('style'=>'text-align: center'), 
  ),
  'make',
  'model',

How would I combine 'model_year','make' and 'model', into 1 column in the views?

Was it helpful?

Solution

The correct way would be to create a new property variable in the model, and join the values afterFind().

See How to have multiple fields (D/M/Y) for single date property in Yii?, but you have to swap the find and save functions, as that user wants to do the opposite (split single column into three fields instead).

OTHER TIPS

You can create a getter in the model:

public function getModelLabel()
{
    return $this->model_year.' '.$this->make.' '.$this->model;
}

Then you can use modelLabel in your GridView as if it were a real attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top