|
@@ -85,44 +85,57 @@
|
|
|
let listArr = reactive<any[]>([]);
|
|
|
|
|
|
//递归遍历左侧菜单数据
|
|
|
- let list2tree = (list) => {
|
|
|
- list.forEach((child) => {
|
|
|
- const pid = child.parentId;
|
|
|
- if (pid) {
|
|
|
- list.forEach((parent) => {
|
|
|
- if (parent.id === pid) {
|
|
|
- parent.children = parent.children || [];
|
|
|
- parent.isFolder = true;
|
|
|
- parent.title = parent.fileName;
|
|
|
- parent.pid = parent.parentId;
|
|
|
- parent.children.push({ id: child.id, isFolder: true, title: child.fileName, pid: child.parentId });
|
|
|
- }
|
|
|
- });
|
|
|
+ // let list2tree = (list) => {
|
|
|
+ // list.forEach((child) => {
|
|
|
+ // const pid = child.parentId;
|
|
|
+ // if (pid) {
|
|
|
+ // list.forEach((parent) => {
|
|
|
+ // if (parent.id === pid) {
|
|
|
+ // parent.children = parent.children || [];
|
|
|
+ // parent.isFolder = true;
|
|
|
+ // parent.title = parent.fileName;
|
|
|
+ // parent.pid = parent.parentId;
|
|
|
+ // parent.children.push({ id: child.id, isFolder: true, title: child.fileName, pid: child.parentId });
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+ // console.log(list, '999999999');
|
|
|
+ // return list.filter((n) => n.parentId == 'root');
|
|
|
+ // };
|
|
|
+ let list2trees = (data) => {
|
|
|
+ // 删除 所有 children,以防止多次调用
|
|
|
+ data.forEach(function (item) {
|
|
|
+ delete item.children;
|
|
|
+ });
|
|
|
+ // 将数据存储为 以 id 为 KEY 的 map 索引数据列
|
|
|
+ let map = {};
|
|
|
+ data.forEach(function (item) {
|
|
|
+ map[item.id] = item;
|
|
|
+ });
|
|
|
+ var val = [];
|
|
|
+ data.forEach(function (item) {
|
|
|
+ item.isFolder = true;
|
|
|
+ item.title = item.fileName;
|
|
|
+ item.pid = item.parentId;
|
|
|
+ // 以当前遍历项,的pid,去map对象中找到索引的id
|
|
|
+ var parent = map[item.pid];
|
|
|
+ // 好绕啊,如果找到索引,那么说明此项不在顶级当中,那么需要把此项添加到,他对应的父级中
|
|
|
+ if (parent) {
|
|
|
+ (parent.children || (parent.children = [])).push(item);
|
|
|
+ } else {
|
|
|
+ //如果没有在map中找到对应的索引ID,那么直接把 当前的item添加到 val结果集中,作为顶级
|
|
|
+ val.push(item);
|
|
|
}
|
|
|
});
|
|
|
- console.log(list, '999999999');
|
|
|
- return list.filter((n) => n.parentId == 'root');
|
|
|
- };
|
|
|
- let list2trees = () => {
|
|
|
- let arr = [
|
|
|
- { id: 0, name: 1, parentId: 'root' },
|
|
|
- { id: 1, name: 1, parentId: 0 },
|
|
|
- { id: 2, name: 1, parentId: 0 },
|
|
|
- { id: 3, name: 1, parentId: 2 },
|
|
|
- { id: 4, name: 1, parentId: 2 },
|
|
|
- { id: 5, name: 1, parentId: 3 },
|
|
|
- { id: 6, name: 1, parentId: 3 },
|
|
|
- { id: 7, name: 1, parentId: 5 },
|
|
|
- { id: 8, name: 1, parentId: 5 },
|
|
|
- { id: 9, name: 1, parentId: 7 },
|
|
|
- { id: 10, name: 1, parentId: 7 },
|
|
|
- ];
|
|
|
+ return val;
|
|
|
};
|
|
|
+
|
|
|
//获取左侧菜单树数据
|
|
|
let getTreeList = async () => {
|
|
|
listArr.length = 0;
|
|
|
let data = await getTree({ parentId: '' });
|
|
|
- let list = list2tree(data.records);
|
|
|
+ let list = list2trees(data.records);
|
|
|
console.log(list, 'list');
|
|
|
listArr.push(...list);
|
|
|
console.log(listArr, '树节点数据');
|
|
@@ -208,7 +221,6 @@
|
|
|
|
|
|
onMounted(() => {
|
|
|
getTreeList();
|
|
|
- list2trees();
|
|
|
});
|
|
|
</script>
|
|
|
|