估计到这来的都挺急的把,分享两种算法(推荐使用第二种,有时间再研究第一种),直接上代码,拿去直接粘走:
第一种
export function transListToTreeData(list, rootValue) {
// 思路:先找到所有的根节点,然后再再去添加children,第二个参数是根节点的pid值
var arr = [];
list.forEach((item) => {
if (item.pid === rootValue) {
const children = transListToTreeData(list, item.id);
if (children.length) {
// 如果children的长度大于0就说明找到了子节点
item.children = children;
}
arr.push(item);
}
});
return arr;
}
解析:如果不会用的话,我来说一下怎么用:
当后端传给我们的数据没有按照树形控件的规则排好的时候,可以使用这个方法组装数组:
如:后端给我们的数据是:
然后我们调用这个方法之后(第二个参数是根节点的pid,传一个空值就可以了,因为根节点没有pid),结果就会变成:
第二种
这一种是
万能的 万能的 万能的
function getListData(data, config) {
var id = config.id || "id"
var pid = config.pid || "pid"
var children = config.children || "children"
var idMap = {}
var jsonTree = []
data.forEach(function (v) {
idMap[v[id]] = v
})
data.forEach(function (v) {
var parent = idMap[v[pid]]
if (parent) {
!parent[children] && (parent[children] = [])
parent[children].push(v)
} else {
jsonTree.push(v)
}
})
return jsonTree
}
使用方法:
第一个参数是传递的数据,第二个参数是一个对象,传递的id的值是一个字符串,是后端返回给我们的数据中的每一条数据的id的key,pid是每一条数据的pid的key,children的值我吗可以随便指定。
最终数据的结果如下:
附录
测试用的数据
var aaa = [
{
id: "1175310929766055936",
pid: "",
companyId: "1",
name: "总裁办",
code: "ZCB",
managerId: null,
manager: "孙财",
introduce: "总裁办公室",
createTime: null,
},
{
id: "1175311213774962688",
pid: "",
companyId: "1",
name: "行政部",
code: "XZB",
managerId: null,
manager: "罗晓晓",
introduce: "1111",
createTime: null,
},
{
id: "1175311267684352000",
pid: "",
companyId: "1",
name: "人事部",
code: "RSB",
managerId: "1071632760222810112",
manager: "孙财",
introduce: "规划法国恢复规划",
createTime: null,
},
{
id: "1175311325720936448",
pid: "",
companyId: "1",
name: "财务部",
code: "",
managerId: null,
manager: null,
introduce: null,
createTime: null,
},
{
id: "1175311373083017216",
pid: "",
companyId: "1",
name: "技术部",
code: "JSB",
managerId: null,
manager: null,
introduce: null,
createTime: null,
},
{
id: "1175311418004013056",
pid: "",
companyId: "1",
name: "运营部",
code: "YYB",
managerId: null,
manager: null,
introduce: null,
createTime: null,
},
{
id: "1175311466846683136",
pid: "",
companyId: "1",
name: "市场部",
code: "SCB",
managerId: "1063705989926227968",
manager: "武高丽",
introduce: null,
createTime: null,
},
{
id: "1235395178363559936",
pid: "1175311325720936448",
companyId: "1",
name: "财务核算部",
code: null,
managerId: null,
manager: null,
introduce: null,
createTime: null,
},
{
id: "1235398264104624128",
pid: "1175311325720936448",
companyId: "1",
name: "税务管理部",
code: null,
managerId: null,
manager: null,
introduce: null,
createTime: null,
},
{
id: "1235398536969265152",
pid: "1175311325720936448",
companyId: "1",
name: "薪资管理部",
code: null,
managerId: null,
manager: null,
introduce: null,
createTime: null,
},
{
id: "1235398608847052800",
pid: "1175311373083017216",
companyId: "1",
name: "Java研发部",
code: "tryutyty",
managerId: null,
manager: "孙悟空",
introduce: "1213245",
createTime: null,
},
{
id: "1235398661355544576",
pid: "1175311373083017216",
companyId: "1",
name: "Python研发部",
code: null,
managerId: null,
manager: null,
introduce: null,
createTime: null,
},
{
id: "1235398708763762688",
pid: "1175311373083017216",
companyId: "1",
name: "Php研发部",
code: null,
managerId: null,
manager: null,
introduce: null,
createTime: null,
},
{
id: "1235398917619130368",
pid: "1175311466846683136",
companyId: "1",
name: "北京事业部",
code: null,
managerId: null,
manager: null,
introduce: null,
createTime: null,
},
{
id: "1235399011458293760",
pid: "1175311466846683136",
companyId: "1",
name: "上海事业部",
code: null,
managerId: null,
manager: null,
introduce: null,
createTime: null,
},
{
id: "1542388304374833152",
pid: "1175311373083017216",
companyId: "1",
name: "前端技术部",
code: "QD",
managerId: null,
manager: "高小山",
introduce: "前端的小可爱们",
createTime: null,
},
]