Tree组件的更新与查找

最近接到一个需求,需要根据树形结构的子节点状态来更新父节点的状态。特此记录下开发的过程。

需求:

需要根据目录树的结构,在某一节点上如果存在更新标记,那么父节点也必须打上更新标记,在该节点标记消失后,亦需要更新父节点更新标记

分析

如果是简单的树结构遍历使用迭代即可,项目需求需要由子节点状态的遍历来反过来更新父节点的状态。因此简单的遍历行不通,需要换一个思路。
  1. 首先需要遍历找出所有的拥有特殊节点的节点。(考虑扁平化树结构)
  2. 遍历循环找出他们的父节点,并打上标识。
  3. 输出新的变化后树节点

实现

1、扁平化数据

/**
 * @description 扁平化数组
 * @param {Array} arr 树结构数组
 */
function flatArr(arr) {
const result = []
function flat(arr) {
    if (Array.isArray(arr)) {
        arr.forEach(item => {
            if (item.children) {
                flat(item.children)
            }
            result.push(item)
        })
    }
}
flat(arr)
return result
}

2、根据当前的特殊需求,改变节点的状态

/**
 * @description 将扁平数组遍历更改当前的激活状态
 * @param {Array} arr 一维数组
 */
function getSpeacilIds(arr) {
    if (Array.isArray(arr)) {
        arr.forEach((item,index) => {
            // 如果节点有更新标记,抛出父节点。并且打上标记符
            const element = item;
            if (item.updata) {
                const parentIndex = arr.findIndex(i => {
                    return i.id === item.pid
                });
                if (parentIndex > -1) {   
                    // 修改父节点 更新属性
                    arr[parentIndex] = Object.assign({}, arr[parentIndex], {updata:true})
                }
            }
        })
    }
    return arr
}

3、根据新的数据状态,输出新的数据

function changeParentStatus(arr, flatArr) {
    function change(arr, flatArr, parent) {
        if (Array.isArray(arr)) {    
            arr.forEach((item, i) => {
                const itemElement = item
                const index = flatArr.findIndex(i => {
                    return i.id === item.id
                });
                // 找出 扁平数组元素
                const element = flatArr[index]
                if (index > -1) {
                    // 删除 扁平数据的children
                    element.children = null;
                };
                // 注入本级路径
                const path = `/${itemElement.name}`
                if (parent && parent.path) {
                    itemElement.path = `${parent.path}${path}`
                } else {
                    itemElement.path = `${path}`
                }
                // 合并
                arr[i] = Object.assign({}, element, itemElement)
                console.log('====================================');
                console.log(arr[i]);
                console.log('====================================');
                if (item.children) {
                    change(item.children,flatArr, arr[i])
                }
            })
        };
    };
    const newArr = JSON.parse(JSON.stringify(arr))
    const newFlatArr = JSON.parse(JSON.stringify(flatArr))
    change(newArr, newFlatArr)
    return newArr
}

4、完整代码如下

const data =  require('./data')

function changeTree(arr) {
    const flatA = flatArr(arr)
    const speaicl = getSpeacilIds(flatA)
    return JSON.stringify(changeParentStatus(data,speaicl) )
}

/**
 * @description 扁平化数组
 * @param {Array} arr 树结构数组
 */
function flatArr(arr) {
    const result = []
    function flat(arr) {
        if (Array.isArray(arr)) {
            arr.forEach(item => {
                if (item.children) {
                    flat(item.children)
                }
                result.push(item)
            })
        }
    }
    flat(arr)
    return result
}

/**
 * @description 将扁平数组遍历更改当前的激活状态
 * @param {Array} arr 一维数组
 */
function getSpeacilIds(arr) {
    if (Array.isArray(arr)) {
        arr.forEach((item,index) => {
            // 如果节点有更新标记,抛出父节点。并且打上标记符
            const element = item;
            if (item.updata) {
                const parentIndex = arr.findIndex(i => {
                    return i.id === item.pid
                });
                if (parentIndex > -1) {   
                    // 修改父节点 更新属性
                    arr[parentIndex] = Object.assign({}, arr[parentIndex], {updata:true})
                }
            }
            // const path = `/${item.name}`
        })
    }
    return arr
}

function changeParentStatus(arr, flatArr) {
    function change(arr, flatArr, parent) {
        if (Array.isArray(arr)) {    
            arr.forEach((item, i) => {
                const itemElement = item
                const index = flatArr.findIndex(i => {
                    return i.id === item.id
                });
                // 找出 扁平数组元素
                const element = flatArr[index]
                if (index > -1) {
                    // 删除 扁平数据的children
                    element.children = null;
                };
                // 注入本级路径
                const path = `/${itemElement.name}`
                if (parent && parent.path) {
                    itemElement.path = `${parent.path}${path}`
                } else {
                    itemElement.path = `${path}`
                }
                // 合并
                arr[i] = Object.assign({}, element, itemElement)
                console.log('====================================');
                console.log(arr[i]);
                console.log('====================================');
                if (item.children) {
                    change(item.children,flatArr, arr[i])
                }
            })
        };
    };
    const newArr = JSON.parse(JSON.stringify(arr))
    const newFlatArr = JSON.parse(JSON.stringify(flatArr))
    change(newArr, newFlatArr)
    return newArr
}

console.log('====================================');
console.log(changeTree(data));
console.log('====================================');

评论

暂无评论