Display more values in a table cell

Is it possible to display more values in a table cell, using CGridView? For example something like that, in HTML:




<strong>Name Surname</strong>

<br />

<strong>Groups: </strong> Group1, Group2, Group3, etc.



Where ‘name’, ‘surname’ and ‘groupN’ are data taken from model; in this example could be $User->name, $User->surname, $User-groups (joined with N:M relationship).

TIA

Danilo

Something like this should work for singular properties of the model:




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

  'dataProvider'=>$dataProvider,

  'columns'=>array(

    array(

      'name'=>'column_name',

      'type'=>'raw',//this line is vital to have html not show up as text

      'value'=>'"<strong>".$data->name . " " . $data.surname . "</strong>"',

    ),

  ),

));



I’m not sure if this would be the best solution, but to get the group names into a list you could create a function in the model. Here’s a rough example.




//model class

public function getGroupNames(){

  $groups = '';

  foreach($this->groups as $group)

    $groups .= $group->name . ' ';

  return $groups;

}


//CGridView

'type'=>'raw',

'value'=>'"<strong>" . $data->name . " " . $data->surname . "</strong><br/><strong>Groups:</strong> " . $data->getGroupNames()',



If you have to render complex layout in a column or want to reuse that column in another project you could create your own column class. It’s very easy: Extend it from CGridColumn and override renderDataCellContent.

Check the source of existing columns for examples. E.g. http://code.google.com/p/zii/source/browse/tags/1.1.2/widgets/grid/CLinkColumn.php .

@jaz: it works perfectly, thanks! :)

@mike: yes, API is fundamental, but I prefer to see more tutorials for noobie like me. For example, CGridView looks like a very huge and cool class, but it’s too difficult for me to look inside the code to “understand” how it works. I’m trying to do that, because I really love this framework, but more tuts make Yii more usable for people like me.

My 2 cents :slight_smile: