elementplus-tree权限树

<template>
  <div>
    <el-tree></el-tree>
    <el-tree ref="treeRefs" show-checkbox default-expand-all :data="treeData" :expand-on-click-node="false"
      style="max-width:600px;margin-top:10px;" :default-checked-keys="origincheckedkey" @check="handleNodecheck"
      node-key="id" :props="defauleProps" />
  </div>

</template>
<script setup lang='ts'>
import { reactive, ref, onMounted } from 'vue'
const treeRefs: any = ref(null);
const origincheckedkey: any = ref([])//初始选中数据
const treeData: any = ref([])
const realcheckedKey: any = ref([])

onMounted(() => {
  gitListData()
})
const gitListData = () => {
  const tree = {
    paperConfig: [
      {
        id: 3,
        parentId: 0,
        weight: 0,
        name: '成果调研',
        checked: true
      }, {
        id: 4,
        parentId: 0,
        weight: 0,
        name: '论文研读',
        checked: true,
        children: [
          {
            id: 6,
            parentId: 4,
            weight: 0,
            name: '多文档对比',
            checked: true,
          }, {
            id: 7,
            parentId: 4,
            weight: 0,
            name: '多文档问答',
            checked: true,
          }
        ]
      }, {
        id: 5,
        parentId: 0,
        weight: 0,
        name: '学术写作',
        checked: true,
        children: [
          {
            id: 8,
            parentId: 5,
            weight: 0,
            name: '学术翻译',
            checked: true,
          }, {
            id: 9,
            parentId: 5,
            weight: 0,
            name: '学术润色',
            checked: true,
          }, {
            id: 10,
            parentId: 5,
            weight: 0,
            name: '全文审校',
            checked: true,
          }
        ]
      },
    ],
    saasConfig: [{
      id: 2,
      parentId: 0,
      weight: 0,
      name: '租户管理',
      checked: true
    }, {
      id: 1,
      parentId: 0,
      weight: 1,
      name: '用户管理',
      checked: false
    }, {
      id: 11,
      parentId: 0,
      weight: 2,
      name: '角色管理',
      checked: true
    },],
    intelConfig:
      [{
        id: 12,
        parentId: 0,
        weight: 0,
        name: '主体监测分析',
        checked: false
      }, {
        id: 13,
        parentId: 0,
        weight: 0,
        name: '情报调研',
        checked: true
      }, {
        id: 14,
        parentId: 0,
        weight: 0,
        name: '资料研读',
        checked: true
      }, {
        id: 15,
        parentId: 0,
        weight: 0,
        name: '成果编报',
        checked: true
      },],
  }

  const checked = getcheckedItems(tree)
  origincheckedkey.value = checked
  console.warn('选中数据', checked)

  treeData.value = [
    {
      id: 'all',
      name: '所有功能',
      children: [],
    }
  ]
  if (tree.paperConfig && tree.paperConfig.length > 0) {
    treeData.value[0].children.push({
      name: '科研助手',
      id: 'kyzs',
      children: tree.paperConfig
    })
  }
  if (tree.saasConfig && tree.saasConfig.length > 0) {
    treeData.value[0].children.push({
      name: 'saas后台管理',
      id: 'saas',
      children: tree.saasConfig
    })
  }
  if (tree.intelConfig && tree.intelConfig.length > 0) {
    treeData.value[0].children.push({
      name: '情报管理',
      id: '情报',
      children: tree.intelConfig
    })
  }

  realcheckedKey.value = []
  realcheckedKey.value = getcheckedIds(treeData.value)
}

function getcheckedIds(data) {
  const checkedIds: any = []
  function traverse(items) {
    items.forEach((item: any) => {
      if (item.checked) {
        checkedIds.push(item.id)
      }
      if (item.children) {
        traverse(item.children)
      }
    })
  }
  traverse(data)
  return checkedIds
}

const getcheckedItems = (tree: any) => {
  const result: any = []
  function traverse(node) {
    if (!node?.children && node?.children?.length === 0) {
      if (node.checked) {
        result.push(node.id)
      }
      return node.checked
    }
    let allchildrenchecked = true
    node.children?.forEach((child: any) => {
      const childchecked = traverse(child)
      if (!childchecked) {
        allchildrenchecked = false
      }
    })
    if (node.checked && allchildrenchecked) {
      result.push(node.id)
    }
    return node.checked && allchildrenchecked
  }

  for (const key in tree) {
    tree[key]?.forEach((item: any) => traverse(item))
  }
  return result
}

const handleNodecheck = () => {
  const checkedNodes = treeRefs.value.getCheckedNodes(false, false)
  if (checkedNodes.length === 0) {
    realcheckedKey.value = []
  } else {
    realcheckedKey.value = []
    checkedNodes.forEach((item: any) => {
      realcheckedKey.value.push(item.id)
      if (item.parentId && item.parentId !== 0) {
        realcheckedKey.value.push(item.parentId)
      }
    })
  }
}

const defauleProps = {
  children: 'children',
  label: 'name'
}
</script>
<style lang="less" scoped></style>

后端返回的是一个对象,前端需对对象做处理重新组装为一棵树

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值