在我看来,我有一个 dropDownList
,它是从 clients
表中填充的,该表包含像 first_name
、last_name 这样的列
、id
等,现在我想显示 first_name
和 last_name
作为显示文本和 id
作为下拉列表中的值,我已完成使用 id
作为值和 first_name
作为显示文本,但在这里我想合并这些列 (first_name
和 last_name
) 并用作显示文本。
在模型中
function getClients()
{
$Clients = Client::model()->findAll();
$list = CHtml::listData($Clients , 'client_id', 'first_name');
return $list;
}
可见
echo $form->dropDownList($model,'client_id',$model->getClients());
最佳答案
还有一个方法 在模型中
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
虚拟属性,而且可以在任何需要全名的地方使用。
关于php - 在 Yii 框架中,如何组合列并在下拉列表中显示为显示字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12812062/