繁体   English   中英

在无名对象或数组上实现handlebars.js的循环

[英]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}}

这是我的小提琴

http://jsfiddle.net/ckross01/K49xy/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM