hc-tree.vue 5.2 KB

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