hc-tree.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <template>
  2. <view class="hc-tree-box">
  3. <template v-if="nodeData.length > 0" v-for="item in nodeData" :key="item.key">
  4. <hc-tree-node :item="item"
  5. :check="isCheck"
  6. :strictly="isStrictly"
  7. :radio="isRadio"
  8. :counts="isCounts"
  9. :currentKey="currentNodeKey"
  10. @nodeLoad="treeLoadTap"
  11. @nodeTap="treeNodeTap"
  12. />
  13. </template>
  14. <template v-else>
  15. <view class="no-data">暂无相关数据</view>
  16. </template>
  17. </view>
  18. </template>
  19. <script setup>
  20. import {onMounted, ref} from 'vue'
  21. import {getArrValue} from "js-fast-way";
  22. //参数
  23. const props = defineProps({
  24. nodeKey: {
  25. type: String,
  26. default: 'primaryKeyId',
  27. },
  28. labelKey: {
  29. type: String,
  30. default: 'title',
  31. },
  32. leafKey: {
  33. type: String,
  34. default: 'notExsitChild',
  35. },
  36. check: {
  37. type: Boolean,
  38. default: false,
  39. },
  40. //父子是否不关联
  41. strictly: {
  42. type: Boolean,
  43. default: false,
  44. },
  45. checkKey: {
  46. type: Array,
  47. default: () => ([]),
  48. },
  49. radio: {
  50. type: Boolean,
  51. default: false,
  52. },
  53. radioKey: {
  54. type: String,
  55. default: '',
  56. },
  57. currentKey: {
  58. type: String,
  59. default: '',
  60. },
  61. counts: {
  62. type: Boolean,
  63. default: false,
  64. },
  65. })
  66. //节点数据
  67. const nodeData = ref([]);
  68. //参数配置
  69. const isCheck = ref(props.check)
  70. const isRadio = ref(props.radio)
  71. const isStrictly = ref(props.strictly)
  72. const isCounts = ref(props.counts)
  73. const currentNodeKey = ref(props.currentKey)
  74. //事件
  75. const emit = defineEmits(['load', 'nodeTap'])
  76. //渲染完成
  77. onMounted(() => {
  78. getLazyLoad({
  79. level: 0,
  80. data: {},
  81. parentNodes: {},
  82. isLoad: false
  83. })
  84. })
  85. const treeLoadTap = (item) => {
  86. getLazyLoad(item)
  87. }
  88. //懒加载数据
  89. const getLazyLoad = (item) => {
  90. if (!item.isLoad) {
  91. emit('load', item, (data) => {
  92. setLazyLoad(item, data)
  93. })
  94. }
  95. }
  96. //处理获取到的数据
  97. const setLazyLoad = (data, arr) => {
  98. const children = getArrValue(arr), newNodeData = []
  99. if (children.length <= 0) {
  100. data.loading = false
  101. data.isExpand = false
  102. data.isLeaf = true
  103. data.isLoad = true
  104. //如果是跟节点,直接赋值
  105. if (data.level === 0) {
  106. nodeData.value = newNodeData
  107. }
  108. } else {
  109. for (let i = 0; i < children.length; i++) {
  110. const item = children[i]
  111. newNodeData.push({
  112. level: data.level + 1,
  113. key: item[props.nodeKey] ?? '',
  114. label: item[props.labelKey] ?? '',
  115. isLeaf: item[props.leafKey] ?? false,
  116. isExpand: false,
  117. loading: false,
  118. childNodes: [],
  119. isCheck: ifCheckData(data, item),
  120. isRadio: false,
  121. data: item,
  122. parentNodes: data,
  123. isLoad: item[props.leafKey] ?? false,
  124. })
  125. }
  126. //设置父级数据
  127. data.childNodes = newNodeData
  128. data.loading = false
  129. data.isExpand = setBrotherExpand(data.parentNodes)
  130. data.isLoad = true
  131. //如果是跟节点,直接赋值
  132. if (data.level === 0) {
  133. nodeData.value = newNodeData
  134. }
  135. }
  136. }
  137. //设置兄弟节点展开状态
  138. const setBrotherExpand = (nodes) => {
  139. const children = getArrValue(nodes.childNodes)
  140. for (let i = 0; i < children.length; i++) {
  141. nodes.childNodes[i].isExpand = false
  142. }
  143. return true
  144. }
  145. //0未选中 1选中 2半选
  146. const ifCheckData = (data, item) => {
  147. const keys = getArrValue(props.checkKey)
  148. if (keys.indexOf(item[props.nodeKey]) > -1) {
  149. return 1
  150. } else {
  151. return 0
  152. }
  153. }
  154. //树节点被点击
  155. const treeNodeTap = (item) => {
  156. currentNodeKey.value = item.key
  157. emit('nodeTap', item)
  158. }
  159. //获取选中的key
  160. const getCheckKeys = async () => {
  161. const nodes = nodeData.value
  162. const nodeKeys = {
  163. nodes: [], keys: [],
  164. halfNodes: [], halfKeys: [],
  165. }
  166. if (!isCheck.value) {
  167. return nodeKeys
  168. }
  169. await forCheckKeys(nodes, nodeKeys)
  170. return nodeKeys
  171. }
  172. //遍历获取选中的key
  173. const forCheckKeys = async (nodes, nodeKeys) => {
  174. for (let i = 0; i < nodes.length; i++) {
  175. const item = nodes[i]
  176. //0未选中 1选中 2半选
  177. if (nodes[i].isCheck === 1) {
  178. nodeKeys.nodes.push(item)
  179. nodeKeys.keys.push(item.key)
  180. } else if (nodes[i].isCheck === 2) {
  181. nodeKeys.halfNodes.push(item)
  182. nodeKeys.halfKeys.push(item.key)
  183. }
  184. if (item.childNodes.length > 0) {
  185. await forCheckKeys(item.childNodes, nodeKeys)
  186. }
  187. }
  188. }
  189. //导出方法函数
  190. defineExpose({
  191. getCheckKeys
  192. })
  193. </script>
  194. <style lang="scss">
  195. @import "./style.scss";
  196. </style>