9

我的视图中有一个dropDownList,它是从表中填充的clients,该表包含诸如first_name,等列last_nameid现在我想将first_nameandlast_name显示为显示文本和id下拉列表中的值,我完成了idas value 和first_nameas显示文本,但在这里我想组合这些列(first_namelast_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());
4

4 回答 4

20

这是模型中的另一种方法

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不仅可以在下拉列表中使用虚拟属性,还可以在需要全名的任何地方使用该属性。

于 2012-10-10T07:31:47.957 回答
7

第一种变体(简单):

$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;
}
于 2012-10-10T04:51:48.587 回答
0

使用 PHP 的“匿名函数”功能尝试这种紧凑模式:

    function getClients()
    {
        return CHtml::listData(Client::model()->findAll(), 'id', function($client) { return $client->first_name . ' ' . $client->last_name; });
    }
于 2015-02-03T12:26:44.257 回答
0

先前带有匿名功能的“紧凑模式”的评论有错误!正确的代码是:

 function getClients()
 {
    $clients = Client::model()->findAll();
    return CHtml::listData($clients, 'id', function($clients) { return $client->first_name . ' ' . $client->last_name; });
 }
于 2015-12-17T10:03:41.603 回答