index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. (e: 'node-click', v: any, t?: string[]): void
  76. }>()
  77. const visiable = ref<boolean>(false)
  78. const isRotate = ref<boolean>(false)
  79. const labelName = ref<string>('')
  80. const isHover = ref<boolean>(false)
  81. const mouseHandler = (type: number) => {
  82. isHover.value = type === 1 ? true : false
  83. }
  84. const clickHander = () => {
  85. visiable.value = !visiable.value
  86. }
  87. const clearHandler = () => {
  88. nodeClickHandle({
  89. [$props.props['value'] ?? 'value']: '',
  90. [$props.props['label'] ?? 'label']: ''
  91. })
  92. onQueryFilter('')
  93. }
  94. watch(visiable, () => {
  95. isRotate.value = visiable.value ? true : false
  96. })
  97. const treeRef = ref<InstanceType<typeof ElTreeV2>>()
  98. const onQueryFilter = (query: string) => {
  99. treeRef.value!.filter(query)
  100. }
  101. const getExpandedIds = (nodes?: any[]) => {
  102. if (nodes && nodes.length === 0) return []
  103. const ids: any[] = []
  104. function getIdByDfs(nodes) {
  105. for (let i = 0; i < nodes.length; i++) {
  106. const node = nodes[i]
  107. if (
  108. !node[$props.props['children'] ?? 'children'] ||
  109. node[$props.props['children'] ?? 'children'].length == 0
  110. )
  111. break
  112. ids.push(node[$props.props['value'] ?? 'value'])
  113. if (
  114. node[$props.props['children'] ?? 'children'] &&
  115. node[$props.props['children'] ?? 'children'].length > 0
  116. ) {
  117. getIdByDfs(node[$props.props['children'] ?? 'children'])
  118. }
  119. }
  120. }
  121. getIdByDfs(nodes)
  122. return ids
  123. }
  124. const setExpandedKeys = async () => {
  125. await nextTick()
  126. const allChooseIds = getExpandedIds($props.data)
  127. treeRef.value?.setExpandedKeys(allChooseIds)
  128. }
  129. const nodeClickHandle = (data) => {
  130. visiable.value = false
  131. $emit('update:modelValue', data[$props.props['value'] ?? 'value'])
  132. //@ts-ignore
  133. const item = treeRef.value?.getNode($props.modelValue)
  134. if (item && $props.labelAll) {
  135. const arrs = initLabelNameAll(item)
  136. labelName.value = arrs.join('-')
  137. $emit('node-click', data, arrs)
  138. } else {
  139. labelName.value = data[$props.props['label'] ?? 'label']
  140. $emit('node-click', data, [labelName.value])
  141. }
  142. }
  143. watchEffect(() => {
  144. //@ts-ignore
  145. const item = treeRef.value?.getNode($props.modelValue)
  146. if (item) {
  147. if ($props.labelAll) {
  148. const arrs = initLabelNameAll(item)
  149. labelName.value = arrs.join('-')
  150. return
  151. }
  152. labelName.value = item['label'] as string
  153. } else {
  154. labelName.value = ''
  155. }
  156. //默认全部展开
  157. if ($props.data && $props.expend) {
  158. setExpandedKeys()
  159. }
  160. })
  161. const initLabelNameAll = (item): string[] => {
  162. const arrs: string[] = []
  163. const recursion = (item) => {
  164. if (item) {
  165. arrs.push(item.label)
  166. if (item.parent) {
  167. recursion(item.parent)
  168. }
  169. }
  170. }
  171. recursion(item)
  172. arrs.reverse()
  173. return arrs
  174. }
  175. defineExpose({
  176. labelName
  177. })
  178. </script>
  179. <style lang="scss" scoped>
  180. .treeSelectV2 {
  181. position: relative;
  182. :deep(.el-popper) {
  183. width: 100% !important;
  184. left: 0px !important;
  185. right: 0px !important;
  186. }
  187. :deep(.el-input__suffix) {
  188. .el-icon {
  189. color: var(--el-select-input-color);
  190. font-size: var(--el-select-input-font-size);
  191. transition: transform var(--el-transition-duration);
  192. transform: rotateZ(0);
  193. cursor: pointer;
  194. }
  195. }
  196. :deep(.is-focus) {
  197. .el-input__suffix .el-icon {
  198. cursor: pointer;
  199. transform: rotateZ(0);
  200. }
  201. }
  202. .rotate {
  203. transform: rotateZ(-180deg) !important;
  204. }
  205. :deep(.el-tree-node__content) {
  206. .is-leaf {
  207. flex-shrink: 0;
  208. }
  209. }
  210. // :deep(.el-input__wrapper) {
  211. // padding: 0px;
  212. // }
  213. :deep(.el-input.is-disabled .el-input__wrapper) {
  214. background: none !important;
  215. box-shadow: none !important;
  216. }
  217. :deep(.el-input.is-disabled .el-input__inner) {
  218. color: #333 !important;
  219. -webkit-text-fill-color: #333 !important;
  220. }
  221. }
  222. </style>