Element UI + tree组件 + 面包屑 实现导航

 主要使用了用递归。

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/20
  Time: 20:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>Title</title>
   <link rel="stylesheet" type="text/css" href="/qy151_16/js/index.css">
     <link rel="stylesheet" type="text/css" href="/qy151_16/css/stuInfo.css">
    <script type="text/javascript" src="/qy151_16/js/vue.js"></script>
    <script type="text/javascript" src="/qy151_16/js/index.js"></script>
    <script type="text/javascript" src="/qy151_16/js/axios.min.js"></script>
    <script type="text/javascript" src="/qy151_16/js/qs.min.js"></script>
    <link href="/qy151_16/favicon.ico" rel="shortcut icon">
</head>
<body>
  <div id="app">
      <el-tree
          :data="data"
          node-key="id"
          @node-click="handleNodeClick"></el-tree>
      <el-breadcrumb separator-class="el-icon-arrow-right">
          <template v-for="item in breadList">
            <el-breadcrumb-item @click.native="toPage(item,index)">{{item.label}}</el-breadcrumb-item>
          </template>
      </el-breadcrumb>
  </div>

  <script>

		var app = new Vue({
      el:'#app',
      data:{
      	breadList: [],
      	data: [{
          id: 1,
          level:1,
          label: '一级 1',
          associate:1,
          children: [{
            id: 4,
            level:2,
            label: '二级 1-1',
            associate:1,
            children: [{
              id: 9,
              level:3,
              label: '三级 1-1-1',
              associate:4
            }, {
              id: 10,
              level:3,
              label: '三级 1-1-2',
              associate:4
            }]
          }]
        }, {
          id: 2,
          level:1,
          label: '一级 2',
          associate:2,
          children: [{
            id: 5,
            level:2,
            label: '二级 2-1',
            associate:2,
            children: []
          }, {
            id: 6,
            level:2,
            label: '二级 2-2',
            associate:2,
            children: []
          }]
        }, {
          id: 3,
          label: '一级 3',
          level:1,
          associate:3,
          children: [{
            id: 7,
            level:2,
            label: '二级 3-1',
            associate:3,
            children: []
          }, {
            id: 8,
            level:2,
            label: '二级 3-2',
            associate:3,
            children: [{
             id: 11,
              level:3,
              label: '三级 3-2-1',
              associate:8,
              children: []
            }, {
              id: 12,
              level:3,
              label: '三级 3-2-2',
              associate:8,
              children: []
            }, {
              id: 13,
              level:3,
              label: '三级 3-2-3',
              associate:8,
              children: []
            }]
          }]
        }],
      },
      methods:{
      	handleNodeClick(data){

          this.addBread(data);
        },
          //添加面包屑导航逻辑
       addBread(data){
            if(data.level === 1){
              this.breadList=[data];
            }else {

              //判断是否是同级菜单 并记录下
              let isBrother = false;
              let brother = -1;

              //记录是否存在该子菜单
              let isExist = false;

              //判断数组中是否存在该子菜单 存在则结束函数
              this.breadList.forEach(function (item, index) {

                //说明存在数组中存在同级菜单
                if(item.associate === data.associate && item.id !== data.id && item.level !== 1){
                  isBrother = true;
                  brother = index;
                }
                //说明存在数组中存在该菜单
                if(item.id === data.id){
                  isExist = true;
                  return;
                }
              })
              //存在该菜单就直接结束该函数防止重复添加
              if(isExist){
                return;
              }

            //如果是同级节点则替换掉 并删除其子节点
              if(isBrother){

                this.breadList.splice(brother,1,data);
                this.breadList.splice(brother+1,this.breadList.length)
                return;
              }

              //如果是字节点则添加到breadList数组
              this.getChild(this.breadList[0],data)

            }
          },
          getChild(parent,data){

            //当节点为null 结束函数
            if(parent === null) {
              return ;
            }

            //说明该节点是其子节点
            if(parent.id === data.associate){
              this.breadList.push(data)
              return ;
            }
            //继续往其子节点找
             parent.children.forEach(function (item, index) {
              app.getChild(item,data);
            })
      },
        //面包屑跳转
      toPage(item,index){
        	this.targetPage = item.url;
        	this.breadList.splice(index+1,this.breadList.length)
      },

      }
    })
  </script>
</body>
</html>

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值