data-select-item.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <template>
  2. <view
  3. class="customthree-tree-select-content"
  4. :class="{
  5. border: border && node[dataChildren] && node[dataChildren].length && node.showChildren
  6. }"
  7. :style="{ marginLeft: `${level ? 14 : 0}px` }"
  8. >
  9. <view v-if="node.visible" class="custom-tree-select-item">
  10. <view class="item-content">
  11. <view class="left" @click.stop="handleNameClick(node)">
  12. <view class="icon-group">
  13. <view
  14. v-if="node[dataChildren] && node[dataChildren].length"
  15. :class="['right-icon', { active: node.showChildren }]"
  16. >
  17. <uni-icons type="right" size="14" color="#333"></uni-icons>
  18. </view>
  19. <view v-else class="smallcircle-filled">
  20. <uni-icons class="smallcircle-filled-icon" type="smallcircle-filled" size="10" color="#333"></uni-icons>
  21. </view>
  22. </view>
  23. <view v-if="loadingArr.includes(node[props.dataValue].toString())" class="loading-icon-box">
  24. <uni-icons class="loading-icon" type="spinner-cycle" size="14" color="#333"></uni-icons>
  25. </view>
  26. <view class="name" :style="node.disabled ? 'color: #999' : ''">
  27. <text>{{ node[dataLabel] }}</text>
  28. </view>
  29. </view>
  30. <view
  31. v-if="
  32. choseParent ||
  33. (!choseParent && !node[dataChildren]) ||
  34. (!choseParent && node[dataChildren] && !node[dataChildren].length)
  35. "
  36. :class="['check-box', { disabled: node.disabled }]"
  37. @click.stop="!node.disabled && nodeClick(node)"
  38. >
  39. <view v-if="!node.checked && node.partChecked && linkage" class="part-checked"></view>
  40. <uni-icons
  41. v-if="node.checked"
  42. type="checkmarkempty"
  43. size="18"
  44. :color="node.disabled ? '#333' : '#007aff'"
  45. ></uni-icons>
  46. </view>
  47. </view>
  48. </view>
  49. <view v-if="node.showChildren && node[dataChildren] && node[dataChildren].length">
  50. <data-select-item
  51. v-for="item in listData"
  52. :key="item[dataValue]"
  53. :node="item"
  54. :dataLabel="dataLabel"
  55. :dataValue="dataValue"
  56. :dataChildren="dataChildren"
  57. :choseParent="choseParent"
  58. :lazyLoadChildren="lazyLoadChildren"
  59. :border="border"
  60. :linkage="linkage"
  61. :level="level + 1"
  62. ></data-select-item>
  63. </view>
  64. </view>
  65. </template>
  66. <script lang="ts" setup>
  67. import dataSelectItem from './data-select-item.vue'
  68. import { paging } from './utils'
  69. import { ref, inject, watchEffect } from 'vue'
  70. const { nodeClick, nameClick, loadNode, initData, addNode } = inject('nodeFn')
  71. const props = defineProps({
  72. node: {
  73. type: Object,
  74. default: () => ({})
  75. },
  76. choseParent: {
  77. type: Boolean,
  78. default: true
  79. },
  80. dataLabel: {
  81. type: String,
  82. default: 'name'
  83. },
  84. dataValue: {
  85. type: String,
  86. default: 'value'
  87. },
  88. dataChildren: {
  89. type: String,
  90. default: 'children'
  91. },
  92. border: {
  93. type: Boolean,
  94. default: false
  95. },
  96. linkage: {
  97. type: Boolean,
  98. default: false
  99. },
  100. lazyLoadChildren: {
  101. type: Boolean,
  102. default: false
  103. },
  104. level: {
  105. type: Number,
  106. default: 0
  107. }
  108. })
  109. const listData = ref([])
  110. const clearTimerList = ref([])
  111. const loadingArr = ref([])
  112. watchEffect(() => {
  113. if (props.node.showChildren && props.node[props.dataChildren] && props.node[props.dataChildren].length) {
  114. resetClearTimerList()
  115. renderTree(props.node[props.dataChildren])
  116. }
  117. })
  118. // 中断懒加载
  119. function resetClearTimerList() {
  120. const list = [...clearTimerList.value]
  121. clearTimerList.value = []
  122. list.forEach((fn) => fn())
  123. }
  124. // 懒加载
  125. function renderTree(arr: any[]) {
  126. const pagingArr = paging(arr)
  127. listData.value = pagingArr?.[0] || []
  128. lazyRenderList(pagingArr, 1)
  129. }
  130. // 懒加载具体逻辑
  131. function lazyRenderList(arr: any[], startIndex: number) {
  132. for (let i = startIndex; i < arr.length; i++) {
  133. let timer: any = null
  134. timer = setTimeout(() => {
  135. listData.value.push(...arr[i])
  136. }, i * 500)
  137. clearTimerList.push(() => clearTimeout(timer))
  138. }
  139. }
  140. // 点击名称懒加载子元素
  141. function handleNameClick(node: any) {
  142. if (!node.visible) return
  143. if (!node[props.dataChildren]?.length && props.lazyLoadChildren) {
  144. loadingArr.value.push(node[props.dataValue].toString())
  145. loadNode(node)
  146. .then((res: any) => {
  147. addNode(node, initData(res, node.visible))
  148. })
  149. .finally(() => {
  150. loadingArr.value = []
  151. })
  152. } else {
  153. nameClick(node)
  154. }
  155. }
  156. </script>
  157. <style lang="scss" scoped>
  158. $primary-color: #007aff;
  159. $col-sm: 4px;
  160. $col-base: 8px;
  161. $col-lg: 12px;
  162. $row-sm: 5px;
  163. $row-base: 10px;
  164. $row-lg: 15px;
  165. $radius-sm: 3px;
  166. $radius-base: 6px;
  167. $border-color: #c8c7cc;
  168. .customthree-tree-select-content {
  169. &.border {
  170. border-left: 1px solid $border-color;
  171. }
  172. ::v-deep .uni-checkbox-input {
  173. margin: 0 !important;
  174. }
  175. .item-content {
  176. margin: 0 0 $col-lg;
  177. display: flex;
  178. justify-content: space-between;
  179. align-items: center;
  180. position: relative;
  181. &::after {
  182. content: '';
  183. position: absolute;
  184. top: 0;
  185. left: 0;
  186. bottom: 0;
  187. width: 3px;
  188. background-color: #fff;
  189. transform: translateX(-2px);
  190. z-index: 1;
  191. }
  192. .left {
  193. flex: 1;
  194. display: flex;
  195. align-items: center;
  196. .right-icon {
  197. transition: 0.15s ease;
  198. &.active {
  199. transform: rotate(90deg);
  200. }
  201. }
  202. .smallcircle-filled {
  203. width: 14px;
  204. height: 13.6px;
  205. display: flex;
  206. align-items: center;
  207. .smallcircle-filled-icon {
  208. transform-origin: center;
  209. transform: scale(0.55);
  210. }
  211. }
  212. .loading-icon-box {
  213. margin-right: $row-sm;
  214. width: 14px;
  215. height: 100%;
  216. display: flex;
  217. justify-content: center;
  218. align-items: center;
  219. .loading-icon {
  220. transform-origin: center;
  221. animation: rotating infinite 0.2s ease;
  222. }
  223. }
  224. .name {
  225. flex: 1;
  226. }
  227. }
  228. }
  229. .check-box {
  230. margin: 0;
  231. padding: 0;
  232. box-sizing: border-box;
  233. width: 23.6px;
  234. height: 23.6px;
  235. border: 1px solid $border-color;
  236. border-radius: $radius-sm;
  237. display: flex;
  238. justify-content: center;
  239. align-items: center;
  240. &.disabled {
  241. background-color: rgb(225, 225, 225);
  242. }
  243. .part-checked {
  244. width: 60%;
  245. height: 2px;
  246. background-color: $primary-color;
  247. }
  248. }
  249. }
  250. @keyframes rotating {
  251. from {
  252. transform: rotate(0);
  253. }
  254. to {
  255. transform: rotate(360deg);
  256. }
  257. }
  258. </style>