From 387e18059d0c65f962bb40809c9e7e437ec01039 Mon Sep 17 00:00:00 2001 From: jiutianzhiyu Date: Sun, 28 Mar 2021 01:36:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A9=E7=94=A8=E9=80=92=E5=BD=92=E6=8A=8A?= =?UTF-8?q?=E6=89=81=E5=B9=B3=E6=95=B0=E6=8D=AE=E8=BD=AC=E5=8C=96=E6=88=90?= =?UTF-8?q?=E6=A0=91=E5=BD=A2=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/list-to-treedata.js | 16 ++++++++++++++++ src/views/departments/index.vue | 12 ++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 src/utils/list-to-treedata.js diff --git a/src/utils/list-to-treedata.js b/src/utils/list-to-treedata.js new file mode 100644 index 0000000..a99bf6b --- /dev/null +++ b/src/utils/list-to-treedata.js @@ -0,0 +1,16 @@ +export function listToTreeData(list, currentPid) { + const res = [] + list.forEach(item => { + if (item.pid === currentPid) { + // 对于找出来的子节点, 都要再找它的下一级 + // 把当前的item.id 作为下一层的 pid + const children = listToTreeData(list, item.id) + // 什么时候停止, 找不到下一级的时候停止 + if (children.length > 0) { + item.children = children + } + res.push(item) + } + }) + return res +} diff --git a/src/views/departments/index.vue b/src/views/departments/index.vue index cea86d9..19a3ab4 100644 --- a/src/views/departments/index.vue +++ b/src/views/departments/index.vue @@ -18,6 +18,7 @@ -