1.首先看一下我们的json数据:
[ {"goodsId":22,"goodsSn":"111","goodsName":"ppp", "simplyGoodsSizes":[ {"id":30,"newPrice":44,"sizeStr":"4*4"}, {"id":28,"newPrice":22,"sizeStr":"2*2"} ] }, {"goodsId":22,"goodsSn":"111","goodsName":"ppp", "simplyGoodsSizes":[ {"id":32,"newPrice":123,"sizeStr":"6*6"}, {"id":258,"newPrice":456,"sizeStr":"7*7"} ] } ]
2.模板文件:
<textarea id="goods_result_template" style="display: none"> <table> <tr> <td>产品id</td> <td>产品货号</td> <td>产品名称</td> <td>产品尺寸</td> <td></td> </tr> {#foreach $T as record} <tr> <td>{$T.record.goodsId}</td> <td>{$T.record.goodsSn}</td> <td>{$T.record.goodsName}</td> <td> {#foreach $T.record.simplyGoodsSizes as goodsSize} {$T.goodsSize.sizeStr} {#/for} </td> <td> <input type="button" value="将这个货加入订单" οnclick="selThisGoods(this);" /> </td> </tr> {#/for} </table> </textarea>
3.附件数据:
$("#goods_result_dialog").setTemplateElement("goods_result_template"); // 给模板加载数据 $("#goods_result_dialog").processTemplate(json);//
注意上面的json数据为服务器返回的json对象,格式为1中所示的数据格式.
注意上面的这一段的嵌套写法:
{#foreach $T.record.simplyGoodsSizes as goodsSize}
{$T.goodsSize.sizeStr}
{#/for}