2015-02-06 39 views
0

我有一個「Candidato」列表,我想在下拉框中顯示。這裏有一個Candidato的例子:Ember.Select條件值optionLabelPath

{"id": 3, "nombre": "Ivonne Álvarez Garcia", "sexo": 0, "confirmado": true} 

這裏的另一個問題:

{"id": 1, "nombre": "Margarita Arellanes Cervantes", "sexo": 0, "confirmado": false}, 

在我的模板我有這樣的:

{{view "select" content=model.candidatosPAN optionValuePath="content.id" optionLabelPath="content.nombre" selection=newParams.candidatoPAN}}<br/> 

的事情是我想要的「選項標籤」來可變,取決於nombre和confirmado。因此,例如,Ivonne的選項將顯示爲「IvonneÁlvarezGarcia(*)」...因爲她是「confirmmado」。另一方面,瑪格麗塔的選擇是:「Margarita Arellanes Cervantes」,因爲她不是「confirmmado」。

什麼是最簡單和最簡單的方法來做到這一點?

謝謝!

回答

1

一個解決辦法是在某處包裹在列表屬性的內容在你的控制器定義的標籤,因此,例如:

// in your controller 
candidatos: Ember.computed.map('candidatosPAN', function(candidato) { 
    var suffix = candidato.get('confirmado') ? ' (*)' : ''; 
    return { 
     id: candidato.get('id'), 
     label: candidato.get('nombre') + suffix 
    }; 
}); 

// in your template 
{{view "select" content=candidatos optionValuePath="content.id" optionLabelPath="content.label"}} 

取而代之的是selection的屬性,你可以使用value屬性綁定的選擇與候選人的身份證。因此,現在不用選擇候選人,而是選擇候選人身份。

+0

嗨,我也考慮過這種方法。然而,由於所選選項被綁定的方式(它直接與底層模型綁定),我不得不使用從路由模型中提取的原始候選選項。 有沒有涉及定義Ember.Select的簡單擴展的方法? – 2015-02-06 20:45:33

+0

如果您想直接更新模型,可以將觀察者添加到控制器,觀察選定的ID。在觀察員中,您必須找到正確的候選人並與候選人更新模型。 – jcbvm 2015-02-06 20:50:49

+0

啊!對。感謝您指出了這一點。 – 2015-02-06 20:52:16