hc-tree.vue 5.3 KB

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