SearchSelect.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <el-select ref="selectDropRef" :multiple-limit="limit" @change="changeSelect" multiple v-model="value" filterable popper-class="custom-dropdown" remote reserve-keyword :remote-method="remoteMethod" remote-show-suffix clearable placeholder="请选择适用企业">
  3. <div class="optionBox" infinite-scroll-delay="500" infinite-scroll-distance="20" v-infinite-scroll="load">
  4. <el-option v-for="item in options" :key="item.value" filterable :label="item.cpyName" :value="item.cpyid"></el-option>
  5. </div>
  6. </el-select>
  7. </template>
  8. <script setup lang="ts">
  9. import { getEnterpriseList } from '@/api/cdt/menus'
  10. // 防抖
  11. import { debounce } from 'lodash';
  12. const props = defineProps({
  13. modelValue: {
  14. type: String,
  15. default: ''
  16. },
  17. params: {
  18. type: Object,
  19. default: () => ({})
  20. },
  21. limit: {
  22. type: Number,
  23. default: 0
  24. }
  25. })
  26. const emit = defineEmits(['change', 'update:modelValue', 'changeItem'])
  27. const value = ref('')
  28. const options = ref([])
  29. const total = ref(0)
  30. const selectDropRef = ref(null)
  31. const queryParams = ref({
  32. pageSize: 10,
  33. pageNum: 1,
  34. cpyName: '',
  35. })
  36. const getList = async (hasRx = true) => {
  37. if (hasRx) {
  38. queryParams.value.pageNum = 1
  39. options.value = []
  40. }
  41. const res = await getEnterpriseList({ ...queryParams.value, ...props.params })
  42. total.value = res.total;
  43. if (hasRx) {
  44. options.value = res.rows
  45. } else {
  46. options.value = options.value.concat(res.rows)
  47. }
  48. }
  49. const remoteMethod = (text: string) => {
  50. if (text) {
  51. queryParams.value.cpyName = text
  52. selectSearch()
  53. }
  54. }
  55. const load = () => {
  56. if (options.value.length < total.value) {
  57. queryParams.value.pageNum++
  58. getList(false)
  59. }
  60. }
  61. const changeSelect = (val: string[]) => {
  62. const itemInfo = options.value.filter(item => val.includes(item.cpyid))
  63. emit('update:modelValue', val)
  64. emit('change', val)
  65. emit('changeItem', itemInfo)
  66. }
  67. const selectSearch = debounce(() => {
  68. getList()
  69. }, 500)
  70. onMounted(() => {
  71. getList()
  72. nextTick(() => {
  73. // 获取下拉框的dom
  74. const dropdown = document.querySelector('.custom-dropdown .el-select-dropdown__wrap');
  75. dropdown.addEventListener('scroll', () => {
  76. if (dropdown.scrollTop + dropdown.clientHeight >= dropdown.scrollHeight) {
  77. // 在这里执行你的操作
  78. load()
  79. }
  80. });
  81. });
  82. })
  83. watch(() => props.modelValue, (val) => {
  84. value.value = val
  85. }, { immediate: true })
  86. </script>