2013-05-31 20 views
1

我想在下拉列表中發佈組值。如何在價值欄中拼接id和組(type)?複合CHtml :: listData值

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

我曾嘗試:

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

但返回空值。

+1

檢查此問題http://stackoverflow.com/questions/12812062 – dInGd0nG

回答

6

因爲值字段接受匿名函數,你可以做這樣的:

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.1.13 **。對於之前的任何事情,您都可以使用模型的'afterFind()'將一個新變量設置爲'id'和'type'的組合 – topher