基本描述
/**节点选中规则现有一颗完整的树形结构,如下 tree现要求各层级节点右侧单向独立,选中节点时其子级节点也被选中,取消节点时其子级节点也被取消,但是子节点选中与取消操作不影响其父级节点若选中 A,则生成节点,返回选中项 [1]若选中 B,则生成节点,返回选中项 [1, 2]若选中 C,则生成节点,返回选中项 [1, 2, 3]若选中 A、B,则生成节点,返回选中项 [1]、[1, 2]若选中 A、B、C,则生成节点,返回选中项 [1]、[1, 2]、[1, 2, 3]若选中 A、C,则生成节点,返回选中项 [1]、[1, 2, 3]若选中 B、C,则生成节点,返回选中项 [1, 2]、[1, 2, 3]综上,即选中单个节点时,选中返回的数据最后一个元素为选中节点 id;选中 n 个节点即会产生 n 个数组 */const tree = [ { id: 1, name: 'A', parentId: 0, children: [ { id: 2, name: 'B', parentId: 1, children: [ { id: 3, name: 'C', parentId: 2 } ] } ] }]/**情景描述先要求如果选中节点 A、C,由于 C 的父级节点 B 没有选中,则将其提升到父级节点 B 的位置,组件树如下所示 tree1 */const tree1 = [ { id: 1, name: 'A', parentId: 0, children: [ { id: 3, name: 'C', parentId: 2 } ] }]/** * 若只选中 C,则将 C 提到 A 的位置,即 C 变为顶级节点,组件树如下 tree2 */const tree2 = [ { id: 3, name: 'C', parentId: 2 }]/** * 最终,假设选中 A、C,我们会得到平铺的选中项 checkedItemArr,现要求将 checkedItemArr 组合成树状结构即 tree1 形式,如何实现? * 注意:我们可以拿到完整的原始树形结构,即 tree */const checkedItemArr = [ { id: 1, name: 'A', parentId: 0 }, { id: 3, name: 'C', parentId: 2 }]/**实现方案如下:1. 递归原始树 tree,对其进行处理,如果某个节点没有子级,将其 children 设为 null,这是为了避免 UI 展示时该级显示展开下级按钮(下级为空),处理后得到 newTree2. 递归获取所有节点的 id 链,即由该节点所在的各级菜单的值所组成的数组,形如 A:[1]、C: [1, 2, 3],得到 groupIdList [[1]、[1, 2]、[1, 2, 3]]3. 遍历给到的 checkedItemArr,获取其中每一项的 id 链,得到 id 链数组 checkedChainIds4. 遍历 checkedChainIds,删减掉未选中的父级 id,如选中 A、C,则 checkedChainIds 为 [[1], [1, 2, 3]],删减之后变为 [[1], [1, 3]],即 rootArr5. 对得到的 rootArr 进行去重,过滤出相同链表中最长项,如同时存在 [1]、[1, 2]、[1, 2, 3],最终只保留 [1, 2, 3] 即可,因为 [1, 2, 3] 已经包含了 1 和 2,得到 newRootArr6. 组织树形结构数据:1)以 id 为 key 复制出来一份 checkedItemArr 为 temp,并准备一个空数组 tempArr;2)两层遍历,第一层遍历 newRootArr,第二层遍历 newRootArr 中的每一项 id 链- 过滤 id 链中的每一项,如果其索引为 0 且在 tempArr 中不存在,则将其 push 进 tempArr;否则将其将其 push 进前一项的 children */// 第一步const setEmptyChildrenToNull = (arr) => { function deepSearch(tree) { for (var i = 0; i < tree.length; i++) { const item = tree[i] if (item.children && item.children.length > 0) { deepSearch(item.children) } else { item.children = null } } } deepSearch(arr) return arr}const newTree = setEmptyChildrenToNull(tree)// 第二步const groupIdList = []const recursionItem = (originIds, arr) => { arr.forEach(item => { const idChain = [...originIds, item.id] groupIdList.push(idChain) if (item.children) recursionItem(idChain, item.children) })}recursionItem([], newTree)// 第三步const checkedChainIds = checkedItemArr.map(item => { return groupIdList.filter(it => it[it.length - 1] === item.id)[0]})// 第四步const rootArr = []const judgeIdInManageTeamList = (id) => { const temp = checkedItemArr.filter(it => it.id === id) return !!temp.length}checkedChainIds.forEach(itemIds => { const arr = [] for (let i = itemIds.length - 1; i >= 0; i--) { const id = itemIds[i] if (judgeIdInManageTeamList(id)) { arr.unshift(id) } } rootArr.push(arr)})// 第五步const lenArr = []rootArr.forEach(item => { const len = item.length if (lenArr.indexOf(len) < 0) lenArr.push(len)})// 去重,过滤出相同链表中最长项const newRootArr = []for (let i = 0, iLen = lenArr.length; i < iLen; i++) { const arr = rootArr.filter(item => item.length === lenArr[i]) const brr = rootArr.filter(item => item.length > lenArr[i]) if (brr.length) { for (let j = 0, jLen = arr.length; j < jLen; j++) { const a = arr[j][arr[j].length - 1] const crr = brr.filter(item => item[arr[j].length - 1] === a) if (!crr.length) { newRootArr.push(arr[j]) } } } else { newRootArr.push(...arr) }}// 第六步const temp = {}const tempArr = []checkedItemArr.forEach(item => { temp[item.id] = Object.assign({}, item)})const recursionFind = (tree, id) => { var isGet = false var retNode = null function deepSearch(tree, id) { for (var i = 0; i < tree.length; i++) { if (id === tree[i].id && !isGet) { retNode = tree[i] isGet = true break } else if (tree[i].children && tree[i].children.length > 0) { deepSearch(tree[i].children, id) } } } deepSearch(tree, id) return retNode}newRootArr.forEach(item => { for (let i = 0, iLen = item.length; i < iLen; i++) { const it = item[i] const hasNot = !((tempArr.filter(one => one.id === it)).length) if (i === 0 && hasNot) { tempArr.push(temp[it]) } else { const prevItem = recursionFind(tempArr, item[i - 1]) const currItem = recursionFind(tempArr, item[i]) if (prevItem && !currItem) { prevItem.children ? prevItem.children.push(temp[it]) : prevItem.children = [temp[it]] } } }})// 这是最终经过层级提升后生成的新树const afterAdjustTree = tempArr
方案一
const tree = [ { id: 1, name: '好未来', parentId: 0, children: [ { id: 2, name: '培优', parentId: 1, children: [ { id: 4, name: '培优增长', parentId: 2, children: [ { id: 8, name: '培优增长技术部', parentId: 4 } ] }, { id: 5, name: '培优平台', parentId: 2 } ] }, { id: 3, name: '网校', parentId: 1, children: [ { id: 6, name: '网校增长', parentId: 3, children: [ { id: 9, name: '网校增长测试', parentId: 6 } ] }, { id: 7, name: '网校平台', parentId: 3 } ] } ] }]const checkedItemArr = [ { id: 1, name: '好未来', parentId: 0 }, { id: 4, name: '培优增长', parentId: 2 }, { id: 6, name: '网校增长', parentId: 3 }, { id: 9, name: '网校增长测试', parentId: 6 }]// 第一步const setEmptyChildrenToNull = (arr) => { function deepSearch(tree) { for (var i = 0; i < tree.length; i++) { const item = tree[i] if (item.children && item.children.length > 0) { deepSearch(item.children) } else { item.children = null } } } deepSearch(arr) return arr}const newTree = setEmptyChildrenToNull(tree)// 第二步const groupIdList = []const recursionItem = (originIds, arr) => { arr.forEach(item => { const idChain = [...originIds, item.id] groupIdList.push(idChain) if (item.children) recursionItem(idChain, item.children) })}recursionItem([], newTree)// 第三步const checkedChainIds = checkedItemArr.map(item => { return groupIdList.filter(it => it[it.length - 1] === item.id)[0]})// 第四步const rootArr = []const judgeIdInManageTeamList = (id) => { const temp = checkedItemArr.filter(it => it.id === id) return !!temp.length}checkedChainIds.forEach(itemIds => { const arr = [] for (let i = itemIds.length - 1; i >= 0; i--) { const id = itemIds[i] if (judgeIdInManageTeamList(id)) { arr.unshift(id) } } rootArr.push(arr)})// 第五步const lenArr = []rootArr.forEach(item => { const len = item.length if (lenArr.indexOf(len) < 0) lenArr.push(len)})// 去重,过滤出相同链表中最长项const newRootArr = []for (let i = 0, iLen = lenArr.length; i < iLen; i++) { const arr = rootArr.filter(item => item.length === lenArr[i]) const brr = rootArr.filter(item => item.length > lenArr[i]) if (brr.length) { for (let j = 0, jLen = arr.length; j < jLen; j++) { const a = arr[j][arr[j].length - 1] const crr = brr.filter(item => item[arr[j].length - 1] === a) if (!crr.length) { newRootArr.push(arr[j]) } } } else { newRootArr.push(...arr) }}// 第六步const temp = {}const tempArr = []checkedItemArr.forEach(item => { temp[item.id] = Object.assign({}, item)})const recursionFind = (tree, id) => { var isGet = false var retNode = null function deepSearch(tree, id) { for (var i = 0; i < tree.length; i++) { if (id === tree[i].id && !isGet) { retNode = tree[i] isGet = true break } else if (tree[i].children && tree[i].children.length > 0) { deepSearch(tree[i].children, id) } } } deepSearch(tree, id) return retNode}newRootArr.forEach(item => { for (let i = 0, iLen = item.length; i < iLen; i++) { const it = item[i] const hasNot = !((tempArr.filter(one => one.id === it)).length) if (i === 0 && hasNot) { tempArr.push(temp[it]) } else { const prevItem = recursionFind(tempArr, item[i - 1]) const currItem = recursionFind(tempArr, item[i]) if (prevItem && !currItem) { prevItem.children ? prevItem.children.push(temp[it]) : prevItem.children = [temp[it]] } } }})// 这是最终经过层级提升后生成的新树const afterAdjustTree = tempArrconsole.log('===> result')console.log(afterAdjustTree)
方案二
const tree = [ { id: 1, name: '好未来', parentId: 0, children: [ { id: 2, name: '培优', parentId: 1, children: [ { id: 4, name: '培优增长', parentId: 2, children: [ { id: 8, name: '培优增长技术部', parentId: 4 } ] }, { id: 5, name: '培优平台', parentId: 2 } ] }, { id: 3, name: '网校', parentId: 1, children: [ { id: 6, name: '网校增长', parentId: 3, children: [ { id: 9, name: '网校增长测试', parentId: 6 } ] }, { id: 7, name: '网校平台', parentId: 3 } ] } ] }]const checkedItemArr = [ { id: 1, name: '好未来', parentId: 0 }, { id: 4, name: '培优增长', parentId: 2 }, { id: 6, name: '网校增长', parentId: 3 }, { id: 9, name: '网校增长测试', parentId: 6 }]const checkedIdArr = checkedItemArr.map(v => v.id);// 第一步const setEmptyChildrenToNull = (arr) => { function deepSearch(tree) { for (var i = 0; i < tree.length; i++) { const item = tree[i] if (item.children && item.children.length > 0) { deepSearch(item.children) } else { item.children = null } } } deepSearch(arr) return arr}const newTree = setEmptyChildrenToNull(tree)// 第二步const groupIdList = []const recursionItem = (originIds, arr) => { arr.forEach(item => { const idChain = [...originIds, item.id] /** * 如果有子级,将 id 链往下传 * 如果没有子级,放入 groupIdList * 即找到同一路径上的最长 id 链 */ if (item.children) { recursionItem(idChain, item.children) }else{ groupIdList.push(idChain); } })}recursionItem([], newTree)console.log('叶子结点ID链',groupIdList)/** * 这里取到了同一路径上的最长 id 链 groupIdList */// 第三步let filterIdGroup = [];let isEqual = function (arr1 = [], arr2 = []){ if(arr1.length !== arr2.length) return false return arr1.every((v, k) => v == arr2[k])}groupIdList.forEach(groupIdListItem => { let temp = []; groupIdListItem.forEach(id => { checkedIdArr.indexOf(id) != -1 && temp.push(id) }) temp.length && !filterIdGroup.some(v=>isEqual(v,temp)) && filterIdGroup.push(temp);})console.log('过滤之后',filterIdGroup)// 第六步const temp = {}const tempArr = []checkedItemArr.forEach(item => { temp[item.id] = Object.assign({}, item)})const recursionFind = (tree, id) => { var isGet = false var retNode = null function deepSearch(tree, id) { for (var i = 0; i < tree.length; i++) { if (id === tree[i].id && !isGet) { retNode = tree[i] isGet = true break } else if (tree[i].children && tree[i].children.length > 0) { deepSearch(tree[i].children, id) } } } deepSearch(tree, id) return retNode}filterIdGroup.forEach(item => { for (let i = 0, iLen = item.length; i < iLen; i++) { const it = item[i] const hasNot = !((tempArr.filter(one => one.id === it)).length) if (i === 0 && hasNot) { tempArr.push(temp[it]) } else { const prevItem = recursionFind(tempArr, item[i - 1]) const currItem = recursionFind(tempArr, item[i]) if (prevItem && !currItem) { prevItem.children ? prevItem.children.push(temp[it]) : prevItem.children = [temp[it]] } } }})// 这是最终经过层级提升后生成的新树const afterAdjustTree = tempArrconsole.log('新的树结构')console.log(afterAdjustTree)