Ember.Select optionLabelPath 的条件值
Ember.Select conditional value for optionLabelPath
我有一个 "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/>
问题是我希望 "option label" 可变,具体取决于 nombre 和 confirmado。因此,例如,Ivonne 的选项将显示为 "Ivonne Álvarez Garcia (*)" ... 因为她是 "confirmado"。另一方面,玛格丽特的选项是:"Margarita Arellanes Cervantes" 因为她不是 "confirmado".
最简单且 ember 左右的方法是什么?
谢谢!
一个解决方案是将内容包装在一个列表中 属性 在控制器的某处定义标签,例如:
// 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"}}
您可以使用 value
属性来将 selection 与候选人的 ID 绑定,而不是 selection
属性。因此,您现在 select 不是 select 一个候选人,而是一个候选人 ID。
我有一个 "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/>
问题是我希望 "option label" 可变,具体取决于 nombre 和 confirmado。因此,例如,Ivonne 的选项将显示为 "Ivonne Álvarez Garcia (*)" ... 因为她是 "confirmado"。另一方面,玛格丽特的选项是:"Margarita Arellanes Cervantes" 因为她不是 "confirmado".
最简单且 ember 左右的方法是什么?
谢谢!
一个解决方案是将内容包装在一个列表中 属性 在控制器的某处定义标签,例如:
// 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"}}
您可以使用 value
属性来将 selection 与候选人的 ID 绑定,而不是 selection
属性。因此,您现在 select 不是 select 一个候选人,而是一个候选人 ID。