tree-node.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <view class="tree-node">
  3. <view v-for="item in treeData" :key="item.id" class="node-item">
  4. <view class="tree_row" :style="getRowStyle()" @click="handleExpand(item)">
  5. <view class="checkbox_wrap" @click.stop="handleSelect(item)">
  6. <view class="checkbox" :class="{ checked: item.checked, 'half-checked': item.halfChecked }">
  7. <view v-if="(item.checked || item.halfChecked) && item.children && item.children.length > 0" class="checkbox_minus"></view>
  8. <image v-else-if="item.checked && (!item.children || item.children.length === 0)" src="/static/image/publish/correct.png" class="checkbox_check_image" mode="aspectFit"></image>
  9. </view>
  10. </view>
  11. <text class="row_text" :class="{ 'leaf-node': !item.children || item.children.length === 0 }">{{ item.name }}</text>
  12. <view v-if="item.children && item.children.length > 0" class="expand_icon">
  13. <image src="/static/image/icon/xiala.png" class="expand-arrow" :class="{ expanded: expandedIds.includes(item.id) }" mode="aspectFit"></image>
  14. </view>
  15. </view>
  16. <view v-if="expandedIds.includes(item.id) && item.children && item.children.length > 0" class="children-container">
  17. <TreeNode :tree-data="item.children" :expanded-ids="expandedIds" :level="level + 1"
  18. @toggle-expand="(ids) => $emit('toggle-expand', ids)"
  19. @toggle-select="(item) => $emit('toggle-select', item)" />
  20. </view>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. export default {
  26. name: 'TreeNode',
  27. props: {
  28. treeData: {
  29. type: Array,
  30. default: () => []
  31. },
  32. expandedIds: {
  33. type: Array,
  34. default: () => []
  35. },
  36. level: {
  37. type: Number,
  38. default: 0
  39. }
  40. },
  41. methods: {
  42. handleExpand(item) {
  43. if (item.children && item.children.length > 0) {
  44. const siblingIds = this.treeData.map(n => n.id);
  45. const isExpanded = this.expandedIds.includes(item.id);
  46. this.$emit('toggle-expand', { itemId: item.id, siblingIds, isExpanded });
  47. }
  48. },
  49. handleSelect(item) {
  50. this.$emit('toggle-select', item);
  51. },
  52. getRowStyle() {
  53. return {
  54. paddingLeft: `${24 + this.level * 48}rpx`
  55. };
  56. }
  57. }
  58. };
  59. </script>
  60. <style lang="scss" scoped>
  61. .tree-node {
  62. .node-item {
  63. background-color: #FFFFFF;
  64. border-bottom: 2rpx solid #F3F3F3;
  65. &:last-child {
  66. border-bottom: none;
  67. }
  68. }
  69. .tree_row {
  70. display: flex;
  71. align-items: center;
  72. padding: 24rpx;
  73. padding-right: 24rpx;
  74. .checkbox_wrap {
  75. margin-right: 16rpx;
  76. flex-shrink: 0;
  77. .checkbox {
  78. width: 32rpx;
  79. height: 32rpx;
  80. border: 2rpx solid #DCDFE6;
  81. border-radius: 6rpx;
  82. display: flex;
  83. align-items: center;
  84. justify-content: center;
  85. &.checked {
  86. background-color: #2E64FA;
  87. border-color: #2E64FA;
  88. .checkbox_minus {
  89. width: 16rpx;
  90. height: 0;
  91. border-top: 2rpx solid #FFFFFF;
  92. border-bottom: 2rpx solid #FFFFFF;
  93. }
  94. .checkbox_check_image {
  95. width: 32rpx;
  96. height: 32rpx;
  97. }
  98. }
  99. &.half-checked {
  100. background-color: #2E64FA;
  101. border-color: #2E64FA;
  102. .checkbox_minus {
  103. width: 16rpx;
  104. height: 0;
  105. border-top: 2rpx solid #FFFFFF;
  106. border-bottom: 2rpx solid #FFFFFF;
  107. }
  108. }
  109. }
  110. }
  111. .row_text {
  112. flex: 1;
  113. font-size: 28rpx;
  114. color: #333333;
  115. &.leaf-node {
  116. color: #666666;
  117. }
  118. }
  119. .expand_icon {
  120. flex-shrink: 0;
  121. margin-left: 8rpx;
  122. width: 24rpx;
  123. height: 24rpx;
  124. display: flex;
  125. align-items: center;
  126. justify-content: center;
  127. .expand-arrow {
  128. width: 32rpx;
  129. height: 32rpx;
  130. display: inline-block;
  131. transform: rotate(-90deg);
  132. transition: transform 0.3s ease;
  133. &.expanded {
  134. transform: rotate(0deg);
  135. }
  136. }
  137. }
  138. .arrow_right {
  139. font-size: 24rpx;
  140. color: #999999;
  141. flex-shrink: 0;
  142. margin-left: 8rpx;
  143. }
  144. }
  145. .children-container {
  146. border-top: 2rpx solid #F3F3F3;
  147. }
  148. }
  149. </style>