【问题标题】:Composite CHtml::listData value复合 CHtml::listData 值
【发布时间】:2013-05-31 00:37:43
【问题描述】:

我想在下拉列表中发布组值。如何在值字段中连接id 和分组 (type)?

CHtml::listData($eventLocationByUser, 'id', 'caption', 'type');

我试过了:

CHtml::listData($eventLocationByUser, 'id'.'type', 'caption', 'type');

但返回空值。

【问题讨论】:

标签: php javascript yii


【解决方案1】:

由于 value 字段接受匿名函数,因此您可以这样做:

CHtml::listData( 
    $eventLocationByUser, 
    'id', 
    function($loc){ return $loc->id . " " . $loc->type; }
);

And here is the example from the docs.


另一种方法是在模型中添加另一个字段(例如 $idtype),每次创建或保存记录时,您也会更新此字段(也许使用行为?)然后您可以使用:

CHtml::listData( $eventLocationByUser, 'id', 'idtype' });

它可能会将业务逻辑从您的视图转移到您的模型,但只有您可以决定是否值得麻烦。

【讨论】:

  • 匿名函数仅适用于版本 1.1.13。对于之前的任何事情,您可以使用模型的afterFind() 将新变量设置为idtype 的组合
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多