Wil*_*lis 1 javascript php yii
我想在下拉列表中发布组值.我如何在值字段中连接id和分组(type)?
CHtml::listData($eventLocationByUser, 'id', 'caption', 'type');
我试过了:
CHtml::listData($eventLocationByUser, 'id'.'type', 'caption', 'type');
但是返回空值.
由于值字段接受匿名函数,您可以这样做:
CHtml::listData(
$eventLocationByUser,
'id',
function($loc){ return $loc->id . " " . $loc->type; }
);
Run Code Online (Sandbox Code Playgroud)
另一种方法可能是在模型中添加另一个字段(例如,假设为$ idtype),每次创建或保存记录时,您也可以更新此字段(使用行为?)然后您可以使用:
CHtml::listData( $eventLocationByUser, 'id', 'idtype' });
Run Code Online (Sandbox Code Playgroud)
它可能会将业务逻辑从您的视图移动到您的模型,但只有您可以决定是否值得麻烦.