vue+ele树形结构,递归遍历

<template>
  <el-dialog
    title="菜单权限"
    :visible.sync="isShow"
    width="500px"
    :before-close="handleClose"
    :destroy-on-close="true"
  >
    <div>
      <el-tree :data="setTree" show-checkbox node-key="id" :props="defaultProps">
      </el-tree>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close()">取 消</el-button>
      <el-button :loading="isLoading.changeForm" type="primary" @click="verifyForm"
        >确认</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: "roleManageChangeDialog",
  data() {
    return {
      isShow: false,
      form: {},
      setTree: [],
      defaultProps: {
        children: "childs",
        label: "menuName",
      },
      isLoading: {
        changeForm: false,
      },
      uidList: [], //菜单id列表
    };
  },
  created() {},
  methods: {
    handleClose(done) {
      this.$emit("close");
      done();
    },
    // 按钮关闭
    close() {
      this.$emit("close");
      this.isShow = false;
    },
    // 显示
    setShow(item) {
      this.form = {};
      console.log("显示");
      if (item && item.id) {
        this.$set(this.form, "id", item.id);
        /* this.getForm(item,tyep);*/
        this.getTreeData();
        this.getTreeModel();
      }
      this.isShow = true;
    },
    // 获取树形控件数据
    getTreeData() {
      let params = {};
      this.$http
        .request("getAllMenuList", params)
        .then((data) => {
          this.setTree = data.data;
          console.log(this.setTree);
          // 递归及选中、更改菜单id(防止菜单id和按钮id重复)
          this.uidList = [];
          this.disposeTree(this.setTree);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 处理菜单
    // children: "childs", label: "menuName", 【{b},{}《{}】
    disposeTree(list, isBtns = false) {
      let f = this.uidList; //菜单列表id
      if (!isBtns) {
        // 菜单
        for (let i in list) {
          if (list[i].childs && list[i].childs.length > 0) {
            // 菜单 --并且有子菜单
            list[i].id = "z_" + list[i].id;
            f.push(list[i].id);
            this.disposeTree(list[i].childs, false);
          } else {
            // 菜单 无子菜单
            list[i].id = "z_" + list[i].id;
            f.push(list[i].id);
            if (list[i].btns) {
              let childs = [];
              let btns = list[i].btns;
              console.log(btns);
              for (let j in btns) {
                childs.push({
                  id: btns[j].id,
                  menuName: btns[j].btnName,
                });
              }
              list[i].childs = childs;
            }
          }
        }
      }
      this.$forceUpdate();
    },

    // 获取树形控件选中数据
    getTreeModel() {
      this.modelKey = [];
    },
    // 验证
    verifyForm() {
      this.changePermission();
    },
    //更改权限
    changePermission() {
      let params = {
        roleId: this.form.id,
        // menus:this.$refs.tree.getTree(),
      };
      this.$set(this.isLoading, "changeForm", true);
      this.$http
        .request("roleEditRoleMenu", params)
        .then((data) => {
          this.$set(this.isLoading, "changeForm", false);
          this.$message({
            type: "success",
            message: `更改角色权限成功!`,
          });
          this.isShow = false;
          this.$emit("changeReturn");
          this.$emit("close");
        })
        .catch((err) => {
          this.$set(this.isLoading, "changeForm", false);
        });
    },
    // 修改
    modifyForm() {},
  },
  components: {},
  props: [],
  // 监听
  watch: {},
};
</script>

<style></style>

树形结构图:
在这里插入图片描述
数据结构:
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue树形结构遍历可以通过递归的方式实现。以下是一种常见的实现方式: 1. 深度优先遍历(DFS): 深度优先遍历是一种先访问根节点,然后再依次访问子节点的方式。在Vue中,可以通过递归调用自身来实现深度优先遍历。 ```javascript // 定义一个组件 Vue.component('tree-node', { props: ['node'], template: ` <div> {{ node.name }} <tree-node v-for="child in node.children" :node="child" :key="child.id"></tree-node> </div> ` }); // 在父组件中使用tree-node组件 <template> <div> <tree-node :node="treeData"></tree-node> </div> </template> <script> export default { data() { return { treeData: { name: 'Root', children: [ { name: 'Node 1', children: [ { name: 'Node 1.1', children: [] }, { name: 'Node 1.2', children: [] } ] }, { name: 'Node 2', children: [] } ] } }; } }; </script> ``` 2. 广度优先遍历(BFS): 广度优先遍历是一种先访问根节点,然后按层级依次访问子节点的方式。在Vue中,可以使用队列来实现广度优先遍历。 ```javascript // 定义一个组件 Vue.component('tree-node', { props: ['node'], template: ` <div> {{ node.name }} <tree-node v-for="child in node.children" :node="child" :key="child.id"></tree-node> </div> ` }); // 在父组件中使用tree-node组件 <template> <div> <tree-node :node="treeData"></tree-node> </div> </template> <script> export default { data() { return { treeData: { name: 'Root', children: [ { name: 'Node 1', children: [ { name: 'Node 1.1', children: [] }, { name: 'Node 1.2', children: [] } ] }, { name: 'Node 2', children: [] } ] } }; }, mounted() { this.bfsTraversal(this.treeData); }, methods: { bfsTraversal(node) { const queue = [node]; while (queue.length > 0) { const currentNode = queue.shift(); console.log(currentNode.name); if (currentNode.children && currentNode.children.length > 0) { queue.push(...currentNode.children); } } } } }; </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值