SearchSelect.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <template>
  2. <el-select ref="selectDropRef" @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.id"></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. })
  22. const emit = defineEmits(['change', 'update:modelValue', 'changeItem'])
  23. const value = ref('')
  24. const options = ref([])
  25. const total = ref(0)
  26. const selectDropRef = ref(null)
  27. const queryParams = ref({
  28. pageSize: 10,
  29. pageNum: 1,
  30. cpyName: '',
  31. })
  32. const getList = async (hasRx = true) => {
  33. if (hasRx) {
  34. queryParams.value.pageNum = 1
  35. options.value = []
  36. }
  37. const res = await getEnterpriseList({ ...queryParams.value, ...props.params })
  38. total.value = res.total;
  39. if (hasRx) {
  40. options.value = res.rows
  41. } else {
  42. options.value = options.value.concat(res.rows)
  43. }
  44. }
  45. const remoteMethod = (text: string) => {
  46. if (text) {
  47. queryParams.value.cpyName = text
  48. selectSearch()
  49. }
  50. }
  51. const load = () => {
  52. if (options.value.length < total.value) {
  53. queryParams.value.pageNum++
  54. getList(false)
  55. }
  56. }
  57. const changeSelect = (val: string[]) => {
  58. const itemInfo = options.value.filter(item => val.includes(item.id))
  59. emit('update:modelValue', val)
  60. emit('change', val)
  61. emit('changeItem', itemInfo)
  62. }
  63. const selectSearch = debounce(() => {
  64. getList()
  65. }, 500)
  66. onMounted(() => {
  67. getList()
  68. nextTick(() => {
  69. // 获取下拉框的dom
  70. const dropdown = document.querySelector('.custom-dropdown .el-select-dropdown__wrap');
  71. dropdown.addEventListener('scroll', () => {
  72. if (dropdown.scrollTop + dropdown.clientHeight >= dropdown.scrollHeight) {
  73. // 在这里执行你的操作
  74. load()
  75. }
  76. });
  77. });
  78. })
  79. </script>