我有json
[{
"_id": {
"$oid": "57d23f407d033458a7cf4229"
},
"employeeCode": "E1",
"dateFrom": "12/08/2016",
"dateTo": "30/08/2016",
"level": "1",
"partyCode": "P1",
"partyEmployeeId": "57d23f407d033458a7cf4229"
}, {
"_id": {
"$oid": "57d23f407d033458a7cf422a"
},
"employeeCode": "E2",
"dateFrom": "07/09/2016",
"dateTo": "",
"level": "1",
"partyCode": "P1",
"partyEmployeeId": "57d23f407d033458a7cf422a"
}]
我通过
将这个json传递给我的模板var compiledTemplate = _.template( myTemplate,{party:JSON.stringify(party)});
this.$el.html( compiledTemplate );
我的模板代码是
<% _.each(party,function(employee,index,list){ %>
<tr class="employees">
<td>
<span>
<input type="text" value="" class="employeeCode searchValuesOfTable" ><%= employee.employeeCode %></input>
<img style="height:13px;" src="images/select.png" class="search">
</span>
</td>
<td>
<input type="text" value="" class="dateFrom searchValuesOfTable datePickers" placeholder="dd/mm/yyyy"><%= employee.dateFrom %></input>
</td>
<td>
<input type="text" value="" class="dateTo searchValuesOfTable datePickers" placeholder="dd/mm/yyyy"><%= employee.dateTo %></input>
</td>
<% }) %>
但它创建了很多表格行并且不打印该值。请帮我弄清楚错误
答案 0 :(得分:0)
不应该执行stringify,它会将JSON转换为我们无法通过键访问元素的字符串。
此外,您正在使用“partyEmployee”访问模板中的“派对”引用将json数据传递给模板。
只需更改js代码即可。
var template = _.template(myTemplate);
template({partyEmployee : party});