一开始的数据是这样的
为了使table能显示子节点 所以要重新数据处理
处理算法如下
// import Menu from "@/services/menu"
import { map } from "lodash"
function getMenuData(data) {
console.log(data);
if (data[0].level === undefined) {
const rows = map(data, (item, index) => ({
key: "key" + index,
item
}))
return rows
}
var result = [], len = 0
data.forEach(item => {
if (item.id > len)
len = item.id
return len
})
const rows = map(data, (item, index) => ({
key: "row" + item.name,
item,
children: []
}))
for (let i = 0; i < len; i++) {
for (let j = 0; j < len; j++) {
if (rows[i] && rows[j] && rows[i].item.parent_id && rows[i].item.parent_id === rows[j].item.id) {
rows[j].children.push(rows[i])
}
}
}
rows.forEach(e=>{
if(e.children.length===0){
delete e.children
}
})
rows.forEach(elem => {
if (elem.item.parent_id === null) {
result.push(elem)
}
})
return result
}
export default getMenuData