index.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <div class="treeSelectV2">
  3. <el-popover placement="bottom" :visible="visiable" @hide="visiable = false" :teleported="false">
  4. <template #reference>
  5. <el-input
  6. v-model="labelName"
  7. @input="onQueryFilter"
  8. @mouseenter="mouseHandler(1)"
  9. @mouseleave="mouseHandler(0)"
  10. @click="clickHander"
  11. :disabled="disabled"
  12. :placeholder="placeholder"
  13. >
  14. <template #suffix>
  15. <el-icon v-if="labelName && isHover">
  16. <CircleClose @click="clearHandler" />
  17. </el-icon>
  18. <el-icon :class="{ rotate: isRotate }" v-else>
  19. <ArrowDown />
  20. </el-icon>
  21. </template>
  22. </el-input>
  23. </template>
  24. <template #default>
  25. <el-tree-v2
  26. ref="treeRef"
  27. highlight-current
  28. style="width: 100%"
  29. :filter-method="filterMethod"
  30. :props="props"
  31. :data="data"
  32. :expand-on-click-node="false"
  33. @node-click="nodeClickHandle"
  34. >
  35. <template v-if="$slots.default" #default="{ node }">
  36. <slot :node="node"></slot>
  37. </template>
  38. </el-tree-v2>
  39. </template>
  40. </el-popover>
  41. </div>
  42. </template>
  43. <script setup lang="ts">
  44. import { ElTreeV2 } from 'element-plus'
  45. import { ArrowDown } from '@element-plus/icons-vue'
  46. defineOptions({
  47. name: 'TreeSelectV2'
  48. })
  49. const $props = withDefaults(
  50. defineProps<
  51. Partial<{
  52. data: any[]
  53. modelValue: string | number
  54. props: any
  55. disabled: boolean
  56. expend: boolean
  57. filterMethod: any
  58. placeholder: string
  59. labelAll: boolean
  60. }>
  61. >(),
  62. {
  63. props: {
  64. label: 'label',
  65. value: 'value',
  66. children: 'children'
  67. },
  68. expend: true,
  69. labelAll: false,
  70. placeholder: '请选择'
  71. }
  72. )
  73. const $emit = defineEmits<{
  74. (e: 'update:modelValue', v: any): void
  75. }>()
  76. const visiable = ref<boolean>(false)
  77. const isRotate = ref<boolean>(false)
  78. const labelName = ref<string>('')
  79. const isHover = ref<boolean>(false)
  80. const mouseHandler = (type: number) => {
  81. isHover.value = type === 1 ? true : false
  82. }
  83. const clickHander = () => {
  84. visiable.value = !visiable.value
  85. }
  86. const clearHandler = () => {
  87. nodeClickHandle({
  88. [$props.props['value'] ?? 'value']: '',
  89. [$props.props['label'] ?? 'label']: ''
  90. })
  91. onQueryFilter('')
  92. }
  93. watch(visiable, () => {
  94. isRotate.value = visiable.value ? true : false
  95. })
  96. const treeRef = ref<InstanceType<typeof ElTreeV2>>()
  97. const onQueryFilter = (query: string) => {
  98. treeRef.value!.filter(query)
  99. }
  100. const getExpandedIds = (nodes?: any[]) => {
  101. if (nodes && nodes.length === 0) return []
  102. const ids: any[] = []
  103. function getIdByDfs(nodes) {
  104. for (let i = 0; i < nodes.length; i++) {
  105. const node = nodes[i]
  106. if (
  107. !node[$props.props['children'] ?? 'children'] ||
  108. node[$props.props['children'] ?? 'children'].length == 0
  109. )
  110. break
  111. ids.push(node[$props.props['value'] ?? 'value'])
  112. if (
  113. node[$props.props['children'] ?? 'children'] &&
  114. node[$props.props['children'] ?? 'children'].length > 0
  115. ) {
  116. getIdByDfs(node[$props.props['children'] ?? 'children'])
  117. }
  118. }
  119. }
  120. getIdByDfs(nodes)
  121. return ids
  122. }
  123. const setExpandedKeys = async () => {
  124. await nextTick()
  125. const allChooseIds = getExpandedIds($props.data)
  126. treeRef.value?.setExpandedKeys(allChooseIds)
  127. }
  128. const nodeClickHandle = (data) => {
  129. visiable.value = false
  130. $emit('update:modelValue', data[$props.props['value'] ?? 'value'])
  131. //@ts-ignore
  132. const item = treeRef.value?.getNode($props.modelValue)
  133. if (item && $props.labelAll) {
  134. const arrs = initLabelNameAll(item)
  135. labelName.value = arrs.join('-')
  136. return
  137. }
  138. labelName.value = data[$props.props['label'] ?? 'label']
  139. }
  140. watchEffect(() => {
  141. //@ts-ignore
  142. const item = treeRef.value?.getNode($props.modelValue)
  143. if (item) {
  144. if ($props.labelAll) {
  145. const arrs = initLabelNameAll(item)
  146. labelName.value = arrs.join('-')
  147. return
  148. }
  149. labelName.value = item['label'] as string
  150. }
  151. //默认全部展开
  152. if ($props.data && $props.expend) {
  153. setExpandedKeys()
  154. }
  155. })
  156. const initLabelNameAll = (item): string[] => {
  157. const arrs: string[] = []
  158. const recursion = (item) => {
  159. if (item) {
  160. arrs.push(item.label)
  161. if (item.parent) {
  162. recursion(item.parent)
  163. }
  164. }
  165. }
  166. recursion(item)
  167. arrs.reverse()
  168. return arrs
  169. }
  170. </script>
  171. <style lang="scss" scoped>
  172. .treeSelectV2 {
  173. position: relative;
  174. :deep(.el-popper) {
  175. width: 100% !important;
  176. left: 0px !important;
  177. right: 0px !important;
  178. }
  179. :deep(.el-input__suffix) {
  180. .el-icon {
  181. color: var(--el-select-input-color);
  182. font-size: var(--el-select-input-font-size);
  183. transition: transform var(--el-transition-duration);
  184. transform: rotateZ(0);
  185. cursor: pointer;
  186. }
  187. }
  188. :deep(.is-focus) {
  189. .el-input__suffix .el-icon {
  190. cursor: pointer;
  191. transform: rotateZ(0);
  192. }
  193. }
  194. .rotate {
  195. transform: rotateZ(-180deg) !important;
  196. }
  197. :deep(.el-tree-node__content) {
  198. .is-leaf {
  199. flex-shrink: 0;
  200. }
  201. }
  202. // :deep(.el-input__wrapper) {
  203. // padding: 0px;
  204. // }
  205. }
  206. </style>