工具方法合集-utils.js-tree合集

时间:2024-02-24 12:05:14
// 根据指定字段获取符合的项
export function getChildren(val, tree, key = 'dbid') {
  let hasFound = false; // 表示是否有找到id值
  let result = null;
  const fn = (data) => {
    if (Array.isArray(data) && !hasFound) {
      // 判断是否是数组并且没有的情况下,
      data.forEach((item) => {
        if (item[key] === val) {
          // 数据循环每个子项,并且判断子项下边是否有id值
          result = item; // 返回的结果等于每一项
          hasFound = true; // 并且找到id值
        } else if (item.children) {
          fn(item.children); // 递归调用下边的子项
        }
      });
    }
  };
  fn(tree); // 调用一下
  return result;
}

//获取tree轨迹
export function getTrajectory(dataList) {
  let treePath = '';
  dataList.forEach(item => (treePath = `${treePath}/${item.nameZh}`));

  return treePath.substring(1);
}

//获取tree叶子节点
export function getAllLeafNode(tree, key = 'dbid') {
  const val = [];
  const mapTree = (node, innerVal, innerKey) => {
    node.forEach((item) => {
      if (!item.children) {
        innerVal.push(item[innerKey]);
      } else {
        mapTree(item.children, innerVal, innerKey);
      }
    });
  };
  mapTree(tree, val, key);
  return val;
}
//获取tree 的指定字段(所有层级)
export function getTreeAppointField(tree, key = 'dbid') {
  const val = [];
  const mapTree = (node, innerVal, innerKey) => {
    node.forEach((item) => {
      innerVal.push(item[innerKey]);
      if (item.children) {
        mapTree(item.children, innerVal, innerKey);
      }
    });
  };
  mapTree(tree, val, key);
  return val;
}

// 向tree中的每一项添加指定数据
/**
 * @param newData:向每一项填加的属性
 * @param uuid:是否要向每一项填加唯一标识
 * @param childrenField: 下级的字段名标识
 */
export function treeTraversalToAdd(treeList, newData, uuid = true, childrenField = 'children') {
  const addKey = (arr) => {
    if (uuid) {
      return arr?.map((item) => ({
        uuid: createUuid(),
        ...item,
        ...newData,
        [childrenField]: item[childrenField] ? addKey(item[childrenField]) : []
      }))
    }
    return arr?.map((item) => ({
      ...item,
      ...newData,
      [childrenField]: item[childrenField] ? addKey(item[childrenField]) : []
    }))
  }
  const newTree = addKey(deepClone(treeList))

  return newTree
}

/**
 * 向tree数据符合条件的子项前添加
 * @param newData:要添加的新数据
 * @param type:level添加平级、sub添加下级
 * @param first:是否是第一个元素
 */

export function addTreeChildren(val, tree, newData = {}, type = 'level', key = 'aid', first = false) {
  const newTree = deepClone(tree)
  let hasFound = false // 表示是否有找到值
  const traversal = data => {
    for (let i = 0; i < data.length; i++) {
      if (hasFound) return
      const info = data[i]
      if (lodashGet(info, key) !== val && info.children) {
        traversal(info.children)
      } else if (lodashGet(info, key) === val) {
        hasFound = true // 找到值
        const index = data.findIndex(item => val === lodashGet(item, key))
        if (type === 'level') {
          // 向平级添加
          if (first) {
            data.splice(index, 0, newData)
            return
          }
          data.splice(index + 1, 0, newData)
        } else {
          // 向下级添加(实则是替换当前项,向子级添加项)
          const dealData = { ...data[index], children: [newData, ...data[index]?.children] }
          data.splice(index, 1, dealData)
        }
      }
    }
  }
  traversal(newTree)
  return newTree
}

// 删除tree数据符合条件的子项
export function delTreeChildren(val, tree, key = 'aid') {
  const newTree = deepClone(tree)
  const traversal = data => {
    for (let i = 0; i < data.length; i++) {
      const info = data[i]
      if (lodashGet(info, key) !== val && info.children) {
        traversal(info.children)
      } else if (lodashGet(info, key) === val) {
        const index = data.findIndex(item => val === item[key])
        data.splice(index, 1)
        i--
      }
    }
  }
  traversal(newTree)
  return newTree
}