当前位置:   article > 正文

vue3树状图,树状关系图_vue3绘制树状图

vue3绘制树状图

首先在主页面使用 

  1. <image-tree :list="imageTreeData"></image-tree>
  2. import imageTree from '@/components/tree-img/image-tree.vue'
  3. const imageTreeData = reactive([ //数据具有扩展性,根据你自己的数据定义变量,只要children和name 和imgPathUrl不改变就行
  4. {
  5. name: '一1',
  6. imgPathUrl:
  7. 'xxx',
  8. children: [
  9. {
  10. name: '二1',
  11. imgPathUrl:
  12. 'xxx',
  13. children: [
  14. {
  15. name: '三1',
  16. imgPathUrl:
  17. 'xxx',
  18. },
  19. {
  20. name: '三2',
  21. imgPathUrl:
  22. 'xxx',
  23. },
  24. {
  25. name: '三3',
  26. imgPathUrl:
  27. 'xxx',
  28. }
  29. ]
  30. },
  31. {
  32. name: '二2',
  33. imgPathUrl:
  34. 'xxx',
  35. children: [
  36. {
  37. name: '三1',
  38. imgPathUrl:
  39. 'xxx',
  40. children: [
  41. {
  42. name: '三1',
  43. imgPathUrl:
  44. 'xxx',
  45. },
  46. {
  47. name: '三2',
  48. imgPathUrl:
  49. 'xxx',
  50. },
  51. {
  52. name: '三3',
  53. imgPathUrl:
  54. 'xxx',
  55. }
  56. ]
  57. },
  58. {
  59. name: '三2',
  60. imgPathUrl:
  61. 'xxx',
  62. children: [
  63. {
  64. name: '三1',
  65. imgPathUrl:
  66. 'xxx',
  67. },
  68. {
  69. name: '三2',
  70. imgPathUrl:
  71. 'xxx',
  72. },
  73. {
  74. name: '三3',
  75. imgPathUrl:
  76. 'xxx',
  77. children: [
  78. {
  79. name: '三1',
  80. imgPathUrl:
  81. 'xxx',
  82. },
  83. {
  84. name: '三2',
  85. imgPathUrl:
  86. 'xxx',
  87. },
  88. {
  89. name: '三3',
  90. imgPathUrl:
  91. 'xxx',
  92. }
  93. ]
  94. }
  95. ]
  96. }
  97. ]
  98. }
  99. ]
  100. }
  101. ])

创建一个.vue文件的组件

  1. <template>
  2. <!-- 第一层 -->
  3. <div class="flex">
  4. <div v-for="(item, index) in obj" :class="{ heng: !(index == num - 1) && num, zou: index > 0 }">
  5. <div class="div">
  6. <div class="div2" :class="{ xia: xia(item, obj), shang: shang2 }" @click="handleimageclick(item)">
  7. <img :src="item.imgPathUrl">
  8. <!-- {{ item.name }} -->
  9. </div>
  10. </div>
  11. <template v-if="item.children && item.children.length">
  12. <image-tree v-if="item.show" :list="item.children" shang2="false" :num="item.children.length"></image-tree>
  13. </template>
  14. </div>
  15. </div>
  16. </template>
  17. <script setup>
  18. import { ref, reactive } from 'vue';
  19. import imageTree from './image-tree.vue';
  20. const obj = ref([]);
  21. const shang = ref(true);
  22. const props = defineProps({
  23. list: {
  24. type: Array,
  25. default: () => []
  26. },
  27. shang2: {
  28. type: Boolean,
  29. default: ''
  30. },
  31. num: {
  32. type: Number,
  33. default: ''
  34. },
  35. })
  36. if (props.list) {
  37. obj.value = props.list.map(item => {
  38. return { ...item, show: true };
  39. });
  40. shang.value = props.shang2;
  41. }
  42. // 计算属性计算下线
  43. const xia = (item) => {
  44. if (item.children && !item.children.length) {
  45. return '';
  46. }
  47. return item.children && item.show ? 'xia' : '';
  48. };
  49. //点击的时候子节点显示隐藏
  50. const handleimageclick = (item) => {
  51. if (item.children) {
  52. item.show = !item.show
  53. }
  54. }
  55. </script>
  56. <style lang="scss">
  57. * {
  58. margin: 0;
  59. padding: 0;
  60. }
  61. .box {
  62. margin-left: 100px;
  63. margin-top: 100px;
  64. }
  65. .flex {
  66. display: flex;
  67. }
  68. .div {
  69. width: 100px;
  70. height: 115px;
  71. display: flex;
  72. /* 盒子距离 */
  73. margin: 0 auto;
  74. /* margin-bottom: 50px;
  75. margin-right: 50px; */
  76. img {
  77. width: 100%;
  78. height: 100%;
  79. }
  80. }
  81. .div2 {
  82. // background-color: aquamarine;
  83. margin: 0 auto;
  84. position: absolute;
  85. left: 50%;
  86. transform: translateX(-50%);
  87. }
  88. div {
  89. position: relative;
  90. }
  91. /* 下线条 */
  92. .xia::after {
  93. content: '';
  94. width: 1px;
  95. /* 线条往下的高度 */
  96. height: 25px;
  97. bottom: -25px;
  98. position: absolute;
  99. left: 50%;
  100. transform: translateX(-50%);
  101. background-color: #D8D8D8;
  102. }
  103. /* 上线条 */
  104. .shang::before {
  105. content: '';
  106. width: 1px;
  107. /* 线条往下的高度 */
  108. height: 25px;
  109. top: -25px;
  110. position: absolute;
  111. left: 50%;
  112. transform: translateX(-50%);
  113. background-color: #D8D8D8;
  114. }
  115. /* 横线条 */
  116. .heng::after {
  117. content: '';
  118. /* 需要剪去盒子的大小和偏移量 */
  119. width: 50%;
  120. height: 1px;
  121. position: absolute;
  122. left: 50%;
  123. top: -25px;
  124. background-color: #D8D8D8;
  125. }
  126. .zou::before {
  127. content: '';
  128. /* 需要剪去盒子的大小和偏移量 */
  129. width: 50%;
  130. height: 1px;
  131. position: absolute;
  132. left: 0;
  133. top: -25px;
  134. background-color: #D8D8D8;
  135. }
  136. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/231610
推荐阅读
相关标签
  

闽ICP备14008679号