我有以下JSON数据:
{"orders":[
{"id":16,"status":"completed","total":"45.00"},
{"id":17,"status":"completed","total":"55.00"}
]}如何使用json2html将这些数据转换为html?${orders.0.total}工作,但我希望它能够转换所有订单,而不仅仅是数组0。
我尝试过this answer的解决方案,但它行不通。
这就是我所拥有的:
<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>thx
发布于 2016-07-01 12:54:09
您的转换应该修改为:
var transform = {"tag":"div","children":function(){
return( json2html.transform(this.orders,orderTransform) );
}};请注意this更改为this.orders,因为根转换引用{orders: [...]}对象,因此在子函数中需要指定用作数据的新数组。
原始代码将this作为子元素的数据传递,这将使json2html尝试使用orderTransform重新修改{orders: [...]}对象,这是不正确的,因此显式指定该转换的数据在orders字段中很重要。
https://stackoverflow.com/questions/38145723
复制相似问题