有时候我们不得不使用table写一些东西,但是不知道数据的深度,就无法确定数据每一行有几列,就无法进行colspan和rowspan。
那我们使用什么去获取每一行的深度呢? 使用递归!写的时候要格外注意不要写成死循环!否则电脑很容易卡机。
需要前后端都对数据进行一些处理,前端需要处理成如下格式的数据:
let getData = [
{
"position": 1,
"name": "1",
"depth": 0,
"child": [{
"position": 2,
"name": "11",
"depth": 1,
"child": [{
"position": 3,
"name": "111",
"depth": 2, },
{
"position": 4,
"name": "112",
"depth": 2
}
]
},
{
"position": 5,
"name": "12",
"depth": 1
}
]
},
{
"position": 6,
"name": "2",
"depth": 0,
"child": [
]
}
];
做出来的表格是这样子的:
js代码如下
涉及版权,如需html、剩余js代码请私信