«

遍历显示(可用递归调用)暂无时间修改

emer 发布于 2017-12-6 11:12   2268 次阅读     


var jsonDate=[
    {
        "id": 1,
        "name": "电子",
        "editTime": "2010-01-01 12:12:12",
        "child": [
            {
                "id": "11",
                "name": "电子产品",
                "editTime": "2010-01-01 12:12:12",
                "child": [
                    {
                        "id": "111",
                        "name": "电脑",
                        "editTime": "2010-01-01 12:12:12"
                    },
                    {
                        "id": "112",
                        "name": "相机",
                        "editTime": "2010-01-01 12:12:12"
                    }
                ]
            },
            {
                "id": "12",
                "name": "电子的",
                 "editTime": "2010-01-01 12:12:12",
                "child": [
                    {
                        "id": "121",
                        "name": "电脑",
                        "editTime": "2010-01-01 12:12:12"
                    },
                    {
                        "id": "122",
                        "name": "相机",
                        "editTime": "2010-01-01 12:12:12"
                    }
                ]
            }
        ]
    }

];

function rendetTable(arr){
   if(arr.length>0){
     var str=""
     var rowNum=0;
     var child1=null;
     var child2=null;
     for(var i=0;i<arr.length;i++){
        rowNum++;

       str+="<tr data-id='"+arr[i].id+"'><td>"+rowNum+"</td><td>"+arr[i].name+"</td><td>一类层级</td><td> </td><td>"+arr[i].editTime+"</td><td>编辑</td>";
        if(arr[i].child.length>0){
            child1=arr[i].child;
            for(var j=0;j<child1.length;j++){
                rowNum++;
                str+="<tr data-id='"+child1[j].id+"'><td>"+rowNum+"</td><td>"+child1[j].name+"</td><td>二类层级</td><td>"+arr[i].name+"</td><td>"+child1[j].editTime+"</td><td>编辑</td>";
                 if(child1[j].child.length>0){
                    child2=child1[j].child;
                     for(var k=0;k<child2.length;k++){
                        rowNum++;
                        str+="<tr data-id='"+child2[k].id+"'><td>"+rowNum+"</td><td>"+child2[k].name+"</td><td>三类层级</td><td>"+child1[j].name+"</td><td>"+child2[k].editTime+"</td><td>编辑</td>";
                     }
                 }
            }
        }else{
            continue;
        }

     }
    }else{
        console.log("暂无数据");
    }
 $("#table-body").html(str);
}
rendetTable(jsonDate);