2016-07-01 7 views
0

/wの配列にJSONデータのインライン関数を作成します。json2html - 私は次のようなJSONデータを持っている複数の要素

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

どのように私はjson2htmlを使用してHTMLにこのデータを変換しますか? ${orders.0.total}作品が、私はそれがすべての注文を変換したいだけではなく、配列0

私はthis answerから解決策を試してみましたが、それは動作しません。

これは私が持っているものです:ルートが参照変換として、

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

this.ordersthisの変更:

<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

答えて

0

あなたの変換は、に変更する必要があります{orders: [...]}オブジェクトであるため、子関数では、データとして使用されている新しい配列を指定する必要があります。

元のコードが正しくありませんorderTransformを使用して{orders: [...]}オブジェクトを、再度レンダリングしてみjson2htmlになるだろう子供のためのデータとしてthisを渡され、その代わりに、明示的に変換するためのデータがであることを指定することが重要ですordersフィールド。

関連する問題