Yii Framework Forum: CHtml::activeDropDown Option Labels - Yii Framework Forum

Jump to content

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

CHtml::activeDropDown Option Labels How to concatenate 2 strings to be passed to CHtml::listData()? Rate Topic: ***** 1 Votes

#1 User is offline   tommytwoeyes 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 08-March 10

Posted 13 March 2010 - 09:44 PM

Hi,

I have a <select> list in a view that I'd like to populate with a list of employees from my DB. I'm using CHtml::activeDropDownList() and CHtml::listData() to accomplish this, and it looks like so:

<?php
      echo CHtml::activeDropDownList($model, deptHeadId, CHtml::listData(
        Employee::model()->findAll(), 'id', 'firstName'
      ));
?>


This works great, but I'd really like the employees' first and last names to be displayed in the list. Since CHtml::listData() only takes a single model attribute for its third parameter, what would be the best way to accomplish it?

Would it be better to use afterFind() in the model, or to extend the CHtml class and overwrite the listData() method? Or is there an even better solution?

TIA very much!

Tom
0

#2 User is offline   jerry2801 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 504
  • Joined: 18-November 09

Posted 13 March 2010 - 10:20 PM

<?php
      echo CHtml::activeDropDownList($model, deptHeadId, CHtml::listData(
        Employee::model()->findAll(), 'id', 'fullname'
      ));
?>



add follow function to your Employee model:
function getFullname()
{
    return $this->firstName, ' ', $this->lastName;
}

2

#3 User is offline   tommytwoeyes 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 08-March 10

Posted 13 March 2010 - 10:23 PM

View Postjerry2801, on 13 March 2010 - 10:20 PM, said:

<?php
      echo CHtml::activeDropDownList($model, deptHeadId, CHtml::listData(
        Employee::model()->findAll(), 'id', 'fullname'
      ));
?>



add follow function to your Employee model:
function getFullname()
{
    return $this->firstName, ' ', $this->lastName;
}



Oh, yeah. That's way better. Thanks!
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