[英]How do I specify an objects key with another variable in Handlebars
From an Ember template I'm trying to get an objects value using a variable for the key. 我正在尝试从Ember模板中使用变量作为键来获取对象值。 How would I do this? 我该怎么做?
I've Tried the Following: 我尝试了以下内容:
<table>
{{#each record in model}}
<tr>
{{#each col in table.cols}}
<td>{{record[col.field]}}</td>
{{/each}}
</tr>
{{/each}}
</table>
Variables: 变量:
var table = {
cols: [{
header: 'Date/Time',
field: 'date'
},{
header: 'Address',
field: 'address'
},{
header: 'Type',
field: 'type'
}]
};
var model = [{
id: 1,
date: '8/18/85',
address: '123 abc st',
type: 'Unique'
}, {
id: 2,
date: '9/8/95',
address: '123 abc st',
type:'Foreign'
}]
On build I'm getting the following error 在构建时出现以下错误
Parse error on line 25: ...} {{record[col.field]}} ----------------------^ Expecting 'STRING', 'NUMBER', 'ID', 'DATA', got 'INVALID' Error: Parse error on line 25: ...} {{record[col.field]}} ----------------------^ Expecting 'STRING', 'NUMBER', 'ID', 'DATA', got 'INVALID'
I don't know of a built in way, but you can create a template helper to do this for you. 我不知道内置的方式,但是您可以创建一个模板助手来为您完成此任务。
Something like this: 像这样:
Ember.Handlebars.helper('array-value', function(array, key, options) {
return array[key];
});
And in your template you would call: 在模板中,您将调用:
{{ array-value record col.field }}
Here is a jsbin example 这是一个jsbin示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.