VUE(9) : vue-element-admin[9] : 菜单树展示及回显

 菜单数据

this.routes = [
        {"path":1,"title":"权限测试页","children":[{"path":11,"title":"页面权限"},{"path":12,"title":"指令权限"},{"path":13,"title":"角色权限"}]},
        {"path":2,"title":"权限测试页2","children":[{"path":21,"title":"页面权限2"},{"path":22,"title":"指令权限2"},{"path":23,"title":"角色权限2"}]},
      ]

 选中

const xuan_zhong = [{"path":11},{"path":21}]

注 : 选中不用传递上级菜单的path

 完整代码

<template>
  <div class="app-container">
    <el-button type="primary" @click="handleAddRole">
      {{ $t('permission.addRole') }}
    </el-button>

    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'Edit Role':'New Role'">
      <el-form :model="role" label-width="80px" label-position="left">
        <el-form-item label="Menus">
          <el-tree ref="tree" :check-strictly="false" :data="routesData" :props="defaultProps" node-key="path"
            :highlight-current="true" :expand-on-click-node="false" show-checkbox default-expand-all
            class="permission-tree" />
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">
          {{ $t('permission.cancel') }}
        </el-button>
        <el-button type="primary" @click="confirmRole">
          {{ $t('permission.confirm') }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  const defaultRole = {
    key: '',
    name: '',
    description: '',
    routes: []
  }

  export default {
    data() {
      return {
        role: Object.assign({}, defaultRole),
        routes: [],
        rolesList: [],
        dialogVisible: false,
        dialogType: 'new',
        defaultProps: {
          children: 'children',
          label: 'title'
        }
      }
    },
    computed: {
      routesData() {
        return this.routes
      }
    },
    created() {
      this.getRoutes()
    },
    methods: {
      async getRoutes() {
        // 菜单列表
        this.routes = [{
            "path": 1,
            "title": "权限测试页",
            "children": [{
              "path": 11,
              "title": "页面权限"
            }, {
              "path": 12,
              "title": "指令权限"
            }, {
              "path": 13,
              "title": "角色权限"
            }]
          },
          {
            "path": 2,
            "title": "权限测试页2",
            "children": [{
              "path": 21,
              "title": "页面权限2"
            }, {
              "path": 22,
              "title": "指令权限2"
            }, {
              "path": 23,
              "title": "角色权限2"
            }]
          },
        ]
      },
      handleAddRole() {
        this.dialogVisible = true
        // 回显
        const isHuiXian = true
        if (isHuiXian) {
          const xuan_zhong = [{
            "path": 11
          }, {
            "path": 12
          }]
          this.dialogType = 'new'
          this.$nextTick(() => {
            this.$refs.tree.setCheckedNodes(xuan_zhong)
          })
        }
      },
      async confirmRole() {
        // 提交选中的菜单
        const checkedKeys = this.$refs.tree.getCheckedKeys()
        console.log(JSON.stringify(checkedKeys))
      }
    }
  }
</script>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值