改写el-tree实现角色权限设置

改写el-tree实现角色权限设置

  • 取消el-tree自带的父子关系,自己通过递归实现父子关系,这样在选中其中一个子级的时候,父级也会勾选,点击父级也可以全选和取消全选等 ,能够随心控制
<template>
 
      <el-tree
        ref="tree"
        :data="data"
        node-key="menuId"
        :check-strictly="true"
        :props="props"
        show-checkbox
        :default-expanded-keys="[-1]"
        @check="handleCheckChange"
      />
</template>

<script>

export default {
  name: 'TrialEthicsWebRoleMenu',
  data() {
    return {
      data: [
        {
          menuName: '全选',
          menuId: -1,
          children: []
        }
      ],
      props: {
        children: 'children',
        label: 'menuName'
      },
      postId: ''
    }
  },
  created() {
    this.getList()
  },
  mounted() {},

  methods: {
    handleCheckChange(node, flag) {
      node.flag = !node.flag
      if (node.flag) {
        if (node.children?.length) {
          this.recuision(node.children)
          return
        }
        if (node.parentId == 0) {
          return
        }
        this.a(node.parentId)
      } else {
        const arr = this.$refs.tree.getCheckedKeys()
        this.$refs.tree.setCheckedKeys(arr.filter((v) => v !== -1))
        this.data[0].flag = false
        if (node.children?.length) {
          this.recuisionClose(node.children)
        }
        if (node.parentId == 0) {
          return
        }
      }
    },
    // 从子级递归找父级 并选中
    a(parentId) {
      const find = this.findId(this.data, parentId)
      find.flag = true
      this.$refs.tree.setCheckedKeys([
        ...this.$refs.tree.getCheckedKeys(),
        parentId
      ])
      if (find.parentId !== 0) {
        this.a(find.parentId)
      }
    },
    // 查找Id节点
    findId(array, parentId) {
      var obj = {}
      for (let index = 0; index < array.length; index++) {
        const item = array[index]
        if (item.menuId === parentId) {
          obj = item
          break
        }
        if (item.children?.length) {
          if (JSON.stringify(this.findId(item.children, parentId)) != '{}') {
            obj = this.findId(item.children, parentId)
          }
        }
      }
      return obj
    },
    // 点父节点全选中
    recuision(array) {
      for (let index = 0; index < array.length; index++) {
        const item = array[index]
        item.flag = true
        this.$refs.tree.setCheckedKeys([
          ...this.$refs.tree.getCheckedKeys(),
          item.menuId
        ])
        if (item.children?.length) {
          this.recuision(item.children)
        }
      }
    },
    // 点父节点 取消
    recuisionClose(array) {
      for (let index = 0; index < array.length; index++) {
        const item = array[index]
        const arr = this.$refs.tree.getCheckedKeys()
        item.flag = false
        this.$refs.tree.setCheckedKeys(arr.filter((v) => v !== item.menuId))
        if (item.children?.length) {
          this.recuisionClose(item.children)
        }
      }
    },
    submit() {
      if (!this.postId) {
        this.$message.warning('请选择角色')
        return
      }
      if (!this.$refs.tree.getCheckedKeys().length) {
        this.$message.warning('请选择权限')
        return
      }
      const data = {
        belong: 'ethics',
        menuIds: this.$refs.tree.getCheckedKeys(),
        postId: this.postId
      }
      this.system('postPermission', data, (res) => {
        this.$message.success('保存成功')
      })
    },
    getCheckList(postId) {
      this.postId = postId
      const data = {
        postId: postId,
        belong: 'ethics'
      }
      this.system('menuRoleId', this.$qs.stringify(data), (res) => {
        this.getList(res.data)
      })
    },
    getList(arr) {
      const data = {
        belong: 'ethics'
      }
      this.system('menuListAll', data, (res) => {
        this.data[0].children = res.data
        this.resour(this.data)
      })
    },
    resour(array) {
      for (let index = 0; index < array.length; index++) {
        const item = array[index]
        item.flag = false
        if (item.children) {
          this.resour(item.children)
        }
      }
    }
  }
}
</script>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值