doT中嵌套for循环的使用

简介:

1.数据结构

var goods = [
        {
            "id": "1",
            "name": "衣服",
            "goods": [
                {
                    "goods_id": "1",
                    "name": "衣服1",
                    "price": "100",
                    "logoimg": "http://image.diandodo.com/zhudianbao/Uploads/User/u1250000222/20160723/201607231342158687.png@250h_250w_1e_1c"
                },
                {
                    "goods_id": "2",
                    "name": "衣服2",
                    "price": "200",
                    "logoimg": "http://image.diandodo.com/zhudianbao/Uploads/User/u1250000222/20160723/201607231342158687.png@250h_250w_1e_1c"
                }
            ]
        },
        {
            "id": "2",
            "name": "鞋子",
            "goods": [
                {
                    "goods_id": "3",
                    "name": "鞋子1",
                    "price": "100",
                    "logoimg": "http://image.diandodo.com/zhudianbao/Uploads/User/u1250000222/20160723/201607231342158687.png@250h_250w_1e_1c"
                },
                {
                    "goods_id": "4",
                    "name": "鞋子2",
                    "price": "200",
                    "logoimg": "http://image.diandodo.com/zhudianbao/Uploads/User/u1250000222/20160723/201607231342158687.png@250h_250w_1e_1c"
                }
            ]
        }
    ];

2.区域与页面构建

    <div id="category-goods"></div>
    <!-- 必须加上type="text/x-dot-template" -->
    <script id="category-goods-tmpl" type="text/x-dot-template">
        {{ for(var i=0, catlen=it.length; i<catlen; i++) { }}
            <div class="rxsp">
                <div class="aui-pull-left"><h3> {{= it[i].name }} </h3></div>
                <a class="aui-pull-right ">
                    查看更多
                    <span class="aui-iconfont aui-icon-right"></span>
                </a>
            </div>

            <div class="aui-list-item-inner">
                <div class="aui-row aui-row-padded">
                    {{ for(var j=0, goodslen=it[i]['goods'].length; j<goodslen; j++) { }}    
                    <div class="aui-col-xs-6">
                        <img src="{{= it[i]['goods'][j].logoimg }}" onclick="openWin('goods_detail')">
                        <p class="tit">{{= it[i]['goods'][j].name }} </p>
                        <div class="aui-info" style="padding-top:0">
                            <div class="aui-info-item">
                                <span class="red">{{= it[i]['goods'][j].price }}<!-- <span class="jifen"> 积分1000</span></span> -->
                            </div>
                        </div>
                    </div>
                    {{ } }}
                </div>
            </div>
        {{ } }}
    </script>

3.调用数据

    var goods = [
        {
            "id": "1",
            "name": "衣服",
            "goods": [
                {
                    "goods_id": "1",
                    "name": "衣服1",
                    "price": "100",
                    "logoimg": "http://image.diandodo.com/zhudianbao/Uploads/User/u1250000222/20160723/201607231342158687.png@250h_250w_1e_1c"
                },
                {
                    "goods_id": "2",
                    "name": "衣服2",
                    "price": "200",
                    "logoimg": "http://image.diandodo.com/zhudianbao/Uploads/User/u1250000222/20160723/201607231342158687.png@250h_250w_1e_1c"
                }
            ]
        },
        {
            "id": "2",
            "name": "鞋子",
            "goods": [
                {
                    "goods_id": "3",
                    "name": "鞋子1",
                    "price": "100",
                    "logoimg": "http://image.diandodo.com/zhudianbao/Uploads/User/u1250000222/20160723/201607231342158687.png@250h_250w_1e_1c"
                },
                {
                    "goods_id": "4",
                    "name": "鞋子2",
                    "price": "200",
                    "logoimg": "http://image.diandodo.com/zhudianbao/Uploads/User/u1250000222/20160723/201607231342158687.png@250h_250w_1e_1c"
                }
            ]
        }
    ];

    var evalText = doT.template($("#category-goods-tmpl").text());
    $("#category-goods").html(evalText(goods));

4.结果展示

调整结构

var goods_list = {
        "list": [
            {
                "id": "1",
                "name": "衣服",
                "goods": [
                    {
                        "goods_id": "1",
                        "name": "衣服1",
                        "price": "100",
                        "logoimg": "http://image.diandodo.com/zhudianbao/Uploads/User/u1250000222/20160723/201607231342158687.png@250h_250w_1e_1c"
                    },
                    {
                        "goods_id": "2",
                        "name": "衣服2",
                        "price": "200",
                        "logoimg": "http://image.diandodo.com/zhudianbao/Uploads/User/u1250000222/20160723/201607231342158687.png@250h_250w_1e_1c"
                    }
                ]
            },
            {
                "id": "2",
                "name": "鞋子",
                "goods": [
                    {
                        "goods_id": "3",
                        "name": "鞋子1",
                        "price": "100",
                        "logoimg": "http://image.diandodo.com/zhudianbao/Uploads/User/u1250000222/20160723/201607231342158687.png@250h_250w_1e_1c"
                    },
                    {
                        "goods_id": "4",
                        "name": "鞋子2",
                        "price": "200",
                        "logoimg": "http://image.diandodo.com/zhudianbao/Uploads/User/u1250000222/20160723/201607231342158687.png@250h_250w_1e_1c"
                    }
                ]
            }
        ]
    };

这种结构适用于arttemplate,便于统一结构。
doT中相应调整,

    var evalText = doT.template($("#category-goods-tmpl").text());
    $("#category-goods").html(evalText(goods_list.list));



本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/5946161.html,如需转载请自行联系原作者
相关文章
|
6月前
|
Python
循环的嵌套
在编程中,循环结构是一种非常重要的控制结构,用于重复执行一段代码。而循环的嵌套则是指在一个循环内部又包含了另一个或多个循环,使得代码的执行更加复杂和灵活。本文将介绍循环嵌套的概念、使用场景以及示例代码。
99 1
|
6月前
|
算法 前端开发
2649. 嵌套数组生成器
2649. 嵌套数组生成器
31 0
|
1月前
数组去重for循环和for循环嵌套
数组去重for循环和for循环嵌套
24 0
|
2月前
|
算法 索引 Python
for循环的用法
for循环的用法
|
4月前
|
语音技术 Python
语音识别,range语句,range语句=生成数字序列,for循环临时变量作用域,for循环临时变量作用域,for循环的变量作用域只能在for内部用,for循环的嵌套使用,for循环打印九九乘法表
语音识别,range语句,range语句=生成数字序列,for循环临时变量作用域,for循环临时变量作用域,for循环的变量作用域只能在for内部用,for循环的嵌套使用,for循环打印九九乘法表
|
5月前
for循环嵌套for循环与递归的区别
for循环嵌套for循环与递归的区别
|
6月前
|
存储 机器学习/深度学习 人工智能
嵌套for循环的九九乘法表——四个方向打印
嵌套for循环的九九乘法表——四个方向打印
133 0
|
Python
python之while循环,无限循环,while循环的嵌套的定义和使用。
python之while循环,无限循环,while循环的嵌套的定义和使用。
|
Python
python之sort,reverse的定义和使用,列表元素最大最小值的求解,列表嵌套的基本使用。
python之sort,reverse的定义和使用,列表元素最大最小值的求解,列表嵌套的基本使用。
|
Python
巧用list与for循环求和
巧用list与for循环求和
130 0
下一篇
无影云桌面