[英]Implementing loop of handlebars.js on nameless object or array
所有的Handlebars
示例都使用數組或對象名稱實現循環,但是我的json數組沒有任何名稱。
我的數據是這樣的:
[
{"$id":"1","SiteItemID":1,"Title":"Title 1","CreateDate":"2014-06-19T14:12:22.157"},
{"$id":"2","SiteItemID":2,"Title":"Title 2","CreateDate":"2014-06-19T14:12:22.157"},
{"$id":"2","SiteItemID":3,"Title":"Title 3","CreateDate":"2014-06-19T14:12:22.157"},
]
我想在表格行中使用它們,而我的html是這樣的:
{{#each object}}
<tr data-id="{{this.SiteItemID}}">
<td class="title col-xs-8">
{{this.Title}}
</td>
<td class="date text-right col-xs-2">
{{this.CreateDate}}
</td>
</tr>
{{else}}
No Content
{{/each}}
我將它們與以下JS代碼放在一起:
function getData() {
$.ajax({
url: 'http://192.168.101.223:81/api/siteitem'
, success: function(data) {
return data;
}
});
}
function showData() {
var data = getData();
var template = $("#itemlist-table").html();
var handlebarsTemplate = Handlebars.compile(template);
var output = handlebarsTemplate(data);
$("#mainbody").html(output);
}
showData();
而且我嘗試了{{#each object}}
和{{#each array}}
,但是它們都導致'No Content'。
難道我做錯了什么?
您需要使用{{#each []}}
{{#each []}}
<tr data-id="{{this.SiteItemID}}">
<td class="title col-xs-8">
{{this.Title}}
</td>
<td class="date text-right col-xs-2">
{{this.CreateDate}}
</td>
</tr>
{{else}}
No Content
{{/each}}
這是我的小提琴
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.