我有一个dropDownList
在我看来,它是由填充clients
表,表中包含了诸如列first_name
, last_name
, id
等,现在我想显示first_name
和last_name
作为显示文字和id
在下拉列表中的值,我“M用做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
不仅在下拉列表却处处需要一个全名虚拟属性。
第一个变种(简易):
$list = array();
foreach ($Clients as $c) {
$list[$c->id] = $c->first_name . ' ' . $c->last_name;
}
第二个变种(通用):
class ExtHtml extends CHtml {
public static function listData($models,$valueField,$textField,$groupField='')
{
$listData=array();
if($groupField==='')
{
foreach($models as $model)
{
$value=self::value($model,$valueField);
if (is_array($textField)) {
$t = array();
foreach ($textField as $field) {
$t[]=self::value($model,$field,$field);
}
$text=implode(' ', $t);
} else {
$text=self::value($model,$textField, null);
if ($text == null) {
if (is_callable($textField)) $text=call_user_func($textField, $model);
else $text = $textField;
}
}
$listData[$value]=$text;
}
}
else
{
foreach($models as $model)
{
$group=self::value($model,$groupField);
$value=self::value($model,$valueField);
if (is_array($textField)) {
$t = array();
foreach ($textField as $field) {
$t[]=self::value($model,$field,$field);
}
$text=implode(' ', $t);
} else {
$text=self::value($model,$textField, null);
if ($text == null) {
if (is_callable($textField)) $text=call_user_func($textField, $model);
else $text = $textField;
}
}
$listData[$group][$value]=$text;
}
}
return $listData;
}
public static function value($model,$attribute,$defaultValue=null)
{
foreach(explode('.',$attribute) as $name)
{
if(is_object($model) && ($model->hasAttribute($name) || isset($model->{$name})))
$model=$model->$name;
else if(is_array($model) && isset($model[$name]))
$model=$model[$name];
else
return $defaultValue;
}
return $model;
}
}
// in model
function getClients()
{
$Clients = Client::model()->findAll();
$list = ExtHtml::listData($Clients , 'client_id', array('first_name', 'last_name'));
return $list;
}
第三变型(MySQL的)
function getClients()
{
$Clients = Client::model()->findAll(array('select' => 'concat(first_name, " ", last_name) as first_name'));
$list = CHtml::listData($Clients , 'client_id', 'first_name');
return $list;
}
使用PHP的“匿名函数”功能试试这个紧凑模式:
function getClients()
{
return CHtml::listData(Client::model()->findAll(), 'id', function($client) { return $client->first_name . ' ' . $client->last_name; });
}
以“精简模式”之前的评论匿名函数有错误! 正确的代码是:
function getClients()
{
$clients = Client::model()->findAll();
return CHtml::listData($clients, 'id', function($clients) { return $client->first_name . ' ' . $client->last_name; });
}
文章来源: In Yii framework how can I Combine columns and show as display string in dropdownlist