0

我试图用 HandleBars 循环通过以下 JSON;

{
"IndexValues": {
    "inv": {
        "1": {
            "live": {
                "INV": "1",
                "ITP": 719.166962
            },
            "day": 14,
            "week": 85,
            "month": 232
        },
        "2": {
            "live": {
                "INV": "2",
                "ITP": 719.166962
            },
            "day": 14,
            "week": 93,
            "month": 237
        },
        "dayK": 28,
        "week": 178,
        "montk": 469
    }
}
}

上面的 JSON 我尝试使用以下 HandleBar 循环:

{{#data.IndexValues.inv}}
    <!-- the following is showing data, but is not dynamic -->
    {{this.1.live.INV}}
    <br>
    {{this.2.live.INV}}

    <!-- the following doenst work -->
    {{#this}}
        {{this.live.INV}}
    {{/this}}

    <!-- the following is showing data, but is not dynamic -->
    {{#this}}
        {{this.1.live.INV}}
    {{/this}}
{{/data.IndexValues.inv}}

问题是“inv”是动态的,可能在 1-256 的范围内

4

1 回答 1

0

Handlebars 循环仅适用于数组元素( [ ] 表示法),而不适用于对象字段( { } 表示法)。您的示例 JSON 中没有数组。您要么 (a) 将 JSON 更改为具有列表,要么 (b) 创建一个新数组,然后将其传递给 Handlebars 模板。

(a) 更改 JSON(添加“inv.data”列表)

{
"IndexValues": {
    "inv": {
        data: [{
            "id" : 1,
            "live": {
                "INV": "1",
                "ITP": 719.166962
            },
            "day": 14,
            "week": 85,
            "month": 232
        },
        {
            "id" : 2,
            "live": {
                "INV": "2",
                "ITP": 719.166962
            },
            "day": 14,
            "week": 93,
            "month": 237
        }],
        "dayK": 28,
        "week": 178,
        "montk": 469
    }
}
}

您的车把模板将是:

{{#data.IndexValues.inv.data}}
    ...
{{/#data.IndexValues.inv.data}}
于 2012-12-20T04:38:40.163 回答