aru*_*run 9 php yii virtual-attribute html.dropdownlistfor yii-cmodel
我有一个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;
}
Run Code Online (Sandbox Code Playgroud)
在视野中
echo $form->dropDownList($model,'client_id',$model->getClients());
Run Code Online (Sandbox Code Playgroud)
dIn*_*0nG 20
继承人另一种方法在模型中
function getFullName()
{
return $this->first_name.' '.$this->last_name;
}
Run Code Online (Sandbox Code Playgroud)
和
function getClients()
{
$Clients = Client::model()->findAll();
$list = CHtml::listData($Clients , 'client_id', 'fullName');
return $list;
}
Run Code Online (Sandbox Code Playgroud)
我认为这是一种可重用的方法,因为您fullName不仅可以在下拉列表中使用虚拟属性,而且还需要使用全名.
第一个变种(简单):
$list = array();
foreach ($Clients as $c) {
$list[$c->id] = $c->first_name . ' ' . $c->last_name;
}
Run Code Online (Sandbox Code Playgroud)
第二个变种(通用):
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;
}
Run Code Online (Sandbox Code Playgroud)
第3个变种(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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11222 次 |
| 最近记录: |