Ember。为optionLabelPath选择条件值

时间:2015-02-06 18:33:38

标签: ember.js ember.select

我有一个“Candidato”列表,我想在下拉框中显示。以下是Candidato的一个例子:

{"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和confirmmado。因此,例如,Ivonne的选项将显示为“IvonneÁlvarezGarcia(*)”......因为她是“确认”。另一方面,Margarita的选择是:“Margarita Arellanes Cervantes”,因为她不是“确认”。

这样做的最简单和最简单的方法是什么?

谢谢!

1 个答案:

答案 0 :(得分: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属性将选择与候选者的ID绑定,而不是value属性。因此,您现在不选择候选人,而是选择候选人ID。