Admin.UI/src/utils/tree.js

(开头部分) 4KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

/**
* @description: 列表转树形列表
* @example
listToTree(_.cloneDeep(list))

listToTree(_.cloneDeep(list), {
  rootWhere: (parent, self) => {
    return self.parentId === 0
  },
  childsWhere: (parent, self) => {
    return parent.id === self.parentId
  },
  addChilds: (parent, childList) => {
    if (childList?.length > 0) {
      parent['children'] = childList
    }
  }
})
*/
export function listToTree(
  list = [],
  options = {},
  data
) {
  const { rootWhere, childsWhere, addChilds } = Object.assign({
    rootWhere: (parent, self) => {
      return self.parentId === 0
    },
    childsWhere: (parent, self) => {
      return parent.id === self.parentId
    },
    addChilds: (parent, childList) => {
      if (childList?.length > 0) {
        parent['children'] = childList
      }
    }
  }, options || {})
  let tree = []
  // 空列表
  if (!(list?.length > 0)) {
    return tree
  }

  // 顶级
  const rootList = list.filter(item => rootWhere && rootWhere(data, item))
  if (!(rootList?.length > 0)) {
    return tree
  }
  tree = tree.concat(rootList)

  // 子级
  tree.forEach(root => {
    const rootChildList = list.filter(item => childsWhere && childsWhere(root, item))
    if (!(rootChildList?.length > 0)) {
      return
    }
    rootChildList.forEach(item => {
      const childList = listToTree(list, { rootWhere: childsWhere, childsWhere, addChilds }, item)
      addChilds && addChilds(item, childList)
    })
后面还有 144 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。