art.template 迴圈裡面分組。
阿新 • • 發佈:2018-12-21
後臺提供給我們一個數組,我們要用模版實現上面的格式輸出怎麼版呢?
下面就是解決方案:
<h2>迴圈4個一組</h2> <script type="text/html" id="temp1"> <b> {{each rows as item i}} {{if i%4==0}}<b>【{{/if}} {{item.userName}} {{if i>0 && (i-3)%4==0}}】</b>{{/if}} {{/each}}</ul> </script> <script> var data={ "rows":[ {"userName":"專案1"}, {"userName":"專案2"}, {"userName":"專案3"}, {"userName":"專案4"}, {"userName":"專案5"}, {"userName":"專案6"}, {"userName":"專案7"}, {"userName":"專案8"}, {"userName":"專案9"}, {"userName":"專案10"}, {"userName":"專案11"}, {"userName":"專案12"}, {"userName":"專案13"}, {"userName":"專案14"}, {"userName":"專案15"}, {"userName":"專案16"}, {"userName":"專案17"}, {"userName":"專案18"}, {"userName":"專案19"}, {"userName":"專案20"} ] } var html=template("temp1",data) document.write(html) </script>
如果這篇文章對您有幫助,您可以打賞我
技術交流QQ群:15129679