0

我有以下 JSON 数据:

{"orders":[
{"id":16,"status":"completed","total":"45.00"},
{"id":17,"status":"completed","total":"55.00"}
]}

如何使用 json2html 将此数据转换为 html? ${orders.0.total}有效,但我希望它转换所有订单,而不仅仅是数组 0。

我已经尝试了这个答案的解决方案,但它不起作用。

这就是我所拥有的:

<body>
    <ul id="list"></ul>
</body>

<script type="text/javascript">
    //List items
    var myjson = [{"orders":[
                 {"id":16,"status":"completed","total":"45.00"},
                 {"id":17,"status":"completed","total":"55.00"}
                 ]}];

    //List item transform
    var orderTransform = {"tag":"div","html":"${total}"}

    var transform = {"tag":"div","children":function(){
        return( json2html.transform(this,orderTransform) );
    }};

    $(function(){
        //Create the list
        $('#list').json2html(myjson,transform);
    });
</script>

谢谢

4

1 回答 1

0

您的转换应修改为:

var transform = {"tag":"div","children":function(){
        return( json2html.transform(this.orders,orderTransform) );
}};

注意thisto的变化this.orders,因为根变换引用了{orders: [...]}对象,所以在 children 函数中需要指定用作数据的新数组。

原始代码this作为孩子的数据传递,这将使 json2html 尝试{orders: [...]}使用 orderTransform 重新呈现对象,这是不正确的,因此明确指定该转换的数据在orders字段中很重要。

于 2016-07-01T12:54:09.183 回答