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


     主要使用了用递归。

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/6/20
    5. Time: 20:02
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>Title</title>
    12. <link rel="stylesheet" type="text/css" href="/qy151_16/js/index.css">
    13. <link rel="stylesheet" type="text/css" href="/qy151_16/css/stuInfo.css">
    14. <script type="text/javascript" src="/qy151_16/js/vue.js"></script>
    15. <script type="text/javascript" src="/qy151_16/js/index.js"></script>
    16. <script type="text/javascript" src="/qy151_16/js/axios.min.js"></script>
    17. <script type="text/javascript" src="/qy151_16/js/qs.min.js"></script>
    18. <link href="/qy151_16/favicon.ico" rel="shortcut icon">
    19. </head>
    20. <body>
    21. <div id="app">
    22. <el-tree
    23. :data="data"
    24. node-key="id"
    25. @node-click="handleNodeClick"></el-tree>
    26. <el-breadcrumb separator-class="el-icon-arrow-right">
    27. <template v-for="item in breadList">
    28. <el-breadcrumb-item @click.native="toPage(item,index)">{{item.label}}</el-breadcrumb-item>
    29. </template>
    30. </el-breadcrumb>
    31. </div>
    32. <script>
    33. var app = new Vue({
    34. el:'#app',
    35. data:{
    36. breadList: [],
    37. data: [{
    38. id: 1,
    39. level:1,
    40. label: '一级 1',
    41. associate:1,
    42. children: [{
    43. id: 4,
    44. level:2,
    45. label: '二级 1-1',
    46. associate:1,
    47. children: [{
    48. id: 9,
    49. level:3,
    50. label: '三级 1-1-1',
    51. associate:4
    52. }, {
    53. id: 10,
    54. level:3,
    55. label: '三级 1-1-2',
    56. associate:4
    57. }]
    58. }]
    59. }, {
    60. id: 2,
    61. level:1,
    62. label: '一级 2',
    63. associate:2,
    64. children: [{
    65. id: 5,
    66. level:2,
    67. label: '二级 2-1',
    68. associate:2,
    69. children: []
    70. }, {
    71. id: 6,
    72. level:2,
    73. label: '二级 2-2',
    74. associate:2,
    75. children: []
    76. }]
    77. }, {
    78. id: 3,
    79. label: '一级 3',
    80. level:1,
    81. associate:3,
    82. children: [{
    83. id: 7,
    84. level:2,
    85. label: '二级 3-1',
    86. associate:3,
    87. children: []
    88. }, {
    89. id: 8,
    90. level:2,
    91. label: '二级 3-2',
    92. associate:3,
    93. children: [{
    94. id: 11,
    95. level:3,
    96. label: '三级 3-2-1',
    97. associate:8,
    98. children: []
    99. }, {
    100. id: 12,
    101. level:3,
    102. label: '三级 3-2-2',
    103. associate:8,
    104. children: []
    105. }, {
    106. id: 13,
    107. level:3,
    108. label: '三级 3-2-3',
    109. associate:8,
    110. children: []
    111. }]
    112. }]
    113. }],
    114. },
    115. methods:{
    116. handleNodeClick(data){
    117. this.addBread(data);
    118. },
    119. //添加面包屑导航逻辑
    120. addBread(data){
    121. if(data.level === 1){
    122. this.breadList=[data];
    123. }else {
    124. //判断是否是同级菜单 并记录下
    125. let isBrother = false;
    126. let brother = -1;
    127. //记录是否存在该子菜单
    128. let isExist = false;
    129. //判断数组中是否存在该子菜单 存在则结束函数
    130. this.breadList.forEach(function (item, index) {
    131. //说明存在数组中存在同级菜单
    132. if(item.associate === data.associate && item.id !== data.id && item.level !== 1){
    133. isBrother = true;
    134. brother = index;
    135. }
    136. //说明存在数组中存在该菜单
    137. if(item.id === data.id){
    138. isExist = true;
    139. return;
    140. }
    141. })
    142. //存在该菜单就直接结束该函数防止重复添加
    143. if(isExist){
    144. return;
    145. }
    146. //如果是同级节点则替换掉 并删除其子节点
    147. if(isBrother){
    148. this.breadList.splice(brother,1,data);
    149. this.breadList.splice(brother+1,this.breadList.length)
    150. return;
    151. }
    152. //如果是字节点则添加到breadList数组
    153. this.getChild(this.breadList[0],data)
    154. }
    155. },
    156. getChild(parent,data){
    157. //当节点为null 结束函数
    158. if(parent === null) {
    159. return ;
    160. }
    161. //说明该节点是其子节点
    162. if(parent.id === data.associate){
    163. this.breadList.push(data)
    164. return ;
    165. }
    166. //继续往其子节点找
    167. parent.children.forEach(function (item, index) {
    168. app.getChild(item,data);
    169. })
    170. },
    171. //面包屑跳转
    172. toPage(item,index){
    173. this.targetPage = item.url;
    174. this.breadList.splice(index+1,this.breadList.length)
    175. },
    176. }
    177. })
    178. </script>
    179. </body>
    180. </html>

  • 相关阅读:
    大数据运维实战第七课 通过 Ambari工具自动化构建 Hadoop 大数据平台和外围应用(上)
    Ubuntu 20.04 DNS解析原理, 解决resolv.conf被覆盖问题
    Java中的Maven是什么?
    力扣-461.汉明距离
    postswigger 靶场(CSRF)攻略-- 1.没有防御措施的 CSRF 漏洞
    3. 使用tcpdump抓取rdma数据包
    基于SSM的健达企业项目管理系统的设计与实现-计算机毕业设计
    设计模式(十四)----结构型模式之组合模式
    【前端】Vue基础+本地应用
    Linux系统编程系列之线程的信号处理
  • 原文地址:https://blog.csdn.net/qq_41720578/article/details/125450905