在Yii框架中,如何合并列并在下拉列表中显示为显示字符串 [英] In Yii framework how can I Combine columns and show as display string in dropdownlist
问题描述
我的视图中有一个dropDownList
,它是从clients
表填充的,该表包含诸如first_name
,last_name
,id
等的列,现在我要显示first_name
和last_name
作为显示文本,而id
作为下拉列表中的值,我已经完成了id
作为值和first_name
作为显示文本的操作,但是在这里我要合并这些列(first_name
和last_name
)并用作显示文字.
I have a dropDownList
in my view, it is populating from clients
table, the table contains columns like first_name
, last_name
,id
etc., Now I want to show the first_name
and last_name
as display text and id
as value in drop down list, I'm done with id
as value and first_name
as display text, but here I want to combine those columns (first_name
and last_name
) and use as display text.
在模型中
function getClients()
{
$Clients = Client::model()->findAll();
$list = CHtml::listData($Clients , 'client_id', 'first_name');
return $list;
}
可见
echo $form->dropDownList($model,'client_id',$model->getClients());
推荐答案
此处提供了另一种方法 在模型中
Heres another method In model
function getFullName()
{
return $this->first_name.' '.$this->last_name;
}
和
function getClients()
{
$Clients = Client::model()->findAll();
$list = CHtml::listData($Clients , 'client_id', 'fullName');
return $list;
}
我认为这是一种可重用的方法,因为您不仅可以在下拉列表中使用fullName
虚拟属性,而且还可以在需要全名的地方使用该属性.
I think this is kinda reusable method since you can use the fullName
virtual attribute not only in dropdownlist but everywhere a full name is needed.
这篇关于在Yii框架中,如何合并列并在下拉列表中显示为显示字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!