VirtualizationSelect.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <div v-if="props.dataLength === 0" class="nullData">
  3. 暂无数据
  4. </div>
  5. <div v-else class="container" ref="container"
  6. :style="{ height: containerHeight + 'px' }">
  7. <div class="empty" :style="{ height: emptyHeight + 'px' }"></div>
  8. <ul class="list" :style="{ transform: `translateY(${translateY})` }">
  9. <li v-for="(item,index) in listData"
  10. @click="clickLi(item,index)" :key="item" class="item"
  11. :style="styleToLi(item,index)">
  12. <slot :item="item"></slot>
  13. </li>
  14. </ul>
  15. </div>
  16. </template>
  17. <script setup name="VirtualizationSelect">
  18. import {functionDebounce} from "@/utils/debounce";
  19. const props = defineProps({
  20. modelValue: {
  21. type: Number
  22. },
  23. data: {
  24. type: Array,
  25. default: []
  26. },
  27. value: {
  28. type: String,
  29. default: '',
  30. },
  31. dataLength: {
  32. type: Number,
  33. default: 0
  34. },
  35. localSearch: {
  36. type: Boolean,
  37. default: false
  38. }
  39. })
  40. const emit = defineEmits(['change', 'update:modelValue'])
  41. const itemHeight = 34
  42. let emptyHeight = $ref(itemHeight * props.dataLength)
  43. let containerHeight = 300
  44. let itemCount = Math.ceil(containerHeight / itemHeight)
  45. const container = ref(null)
  46. const start = ref(0)
  47. const translateY = ref(0)
  48. const listData = computed(() => {
  49. return props.data.slice(start.value, start.value + itemCount + 1)
  50. })
  51. const clickLi = async (item, index) => {
  52. await emit('update:modelValue', start.value + index);
  53. await emit('change', item)
  54. }
  55. const selectedDataSubscript = (idx) => {
  56. try {
  57. container.value.scrollTop = idx * itemHeight;
  58. } catch (e) {
  59. // console.error(e)
  60. }
  61. }
  62. const styleToLi = (item, index) => {
  63. if (props.value === item.value) {
  64. return {
  65. height: itemHeight + 'px',
  66. backgroundColor: '#409EFF',
  67. color: '#ffffff'
  68. };
  69. } else if (start.value + index === props.modelValue) {
  70. return {
  71. height: itemHeight + 'px',
  72. backgroundColor: '#909399',
  73. color: '#ffffff'
  74. }
  75. } else {
  76. return {
  77. height: itemHeight + 'px',
  78. };
  79. }
  80. }
  81. const enableScrollBar = functionDebounce(() => {
  82. try {
  83. container.value.addEventListener('scroll', e => {
  84. const {scrollTop} = e.target
  85. start.value = Math.floor(scrollTop / itemHeight)
  86. translateY.value = scrollTop + 'px'
  87. })
  88. } catch (e) {
  89. }
  90. }, 200)
  91. const resetData = () => {
  92. emptyHeight = itemHeight * props.dataLength
  93. containerHeight = emptyHeight > 300 ? 300 : emptyHeight
  94. start.value = 0
  95. translateY.value = 0 + 'px'
  96. container.value.scrollTop = 0
  97. itemCount = Math.ceil(containerHeight / itemHeight)
  98. enableScrollBar()
  99. }
  100. defineExpose({selectedDataSubscript, $el: container, enableScrollBar, resetData})
  101. </script>
  102. <style scoped lang="scss">
  103. .nullData {
  104. padding: 10px 0;
  105. margin: 0;
  106. text-align: center;
  107. width: 100%;
  108. color: var(--el-text-color-secondary);
  109. font-size: var(--el-select-font-size);
  110. }
  111. .container {
  112. overflow: auto;
  113. display: flex;
  114. width: 100%;
  115. direction: rtl;
  116. ul {
  117. list-style: none;
  118. margin: 0;
  119. width: 100%;
  120. padding: 0;
  121. direction: ltr;
  122. .item {
  123. font-size: var(--el-font-size-base);
  124. padding: 0 32px 0 20px;
  125. position: relative;
  126. white-space: nowrap;
  127. overflow: hidden;
  128. text-overflow: ellipsis;
  129. color: var(--el-text-color-regular);
  130. height: 34px;
  131. line-height: 34px;
  132. box-sizing: border-box;
  133. cursor: pointer;
  134. width: 100%;
  135. }
  136. .checked {
  137. background-color: #4e91f8;
  138. color: white;
  139. }
  140. .item:hover {
  141. background-color: #fac44d;
  142. color: white;
  143. }
  144. }
  145. }
  146. </style>