Yii Framework Forum: Display more values in a table cell - Yii Framework Forum

Jump to content

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

Display more values in a table cell Rate Topic: -----

#1 User is offline   zitter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 140
  • Joined: 14-July 09

Posted 06 May 2010 - 09:41 AM

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
0

#2 User is offline   Jaz Manister 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 109
  • Joined: 27-October 09
  • Location:Rural Western New York, USA

Posted 06 May 2010 - 10:58 AM

View Postzitter, on 06 May 2010 - 09:41 AM, said:

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()',

0

#3 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 07 May 2010 - 01:41 AM

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.c...CLinkColumn.php .
0

#4 User is offline   zitter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 140
  • Joined: 14-July 09

Posted 08 May 2010 - 10:52 AM

@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 :-)
0

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