remote-search.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <el-select-v2 v-model="value" style="width: 240px" multiple filterable remote :remote-method="remoteMethod" clearable :options="options" :loading="loading" placeholder="Please enter a keyword" />
  3. </template>
  4. <script lang="ts" setup>
  5. import { ref } from 'vue'
  6. const states = [
  7. 'Alabama',
  8. 'Alaska',
  9. 'Arizona',
  10. 'Arkansas',
  11. 'California',
  12. 'Colorado',
  13. 'Connecticut',
  14. 'Delaware',
  15. 'Florida',
  16. 'Georgia',
  17. 'Hawaii',
  18. 'Idaho',
  19. 'Illinois',
  20. 'Indiana',
  21. 'Iowa',
  22. 'Kansas',
  23. 'Kentucky',
  24. 'Louisiana',
  25. 'Maine',
  26. 'Maryland',
  27. 'Massachusetts',
  28. 'Michigan',
  29. 'Minnesota',
  30. 'Mississippi',
  31. 'Missouri',
  32. 'Montana',
  33. 'Nebraska',
  34. 'Nevada',
  35. 'New Hampshire',
  36. 'New Jersey',
  37. 'New Mexico',
  38. 'New York',
  39. 'North Carolina',
  40. 'North Dakota',
  41. 'Ohio',
  42. 'Oklahoma',
  43. 'Oregon',
  44. 'Pennsylvania',
  45. 'Rhode Island',
  46. 'South Carolina',
  47. 'South Dakota',
  48. 'Tennessee',
  49. 'Texas',
  50. 'Utah',
  51. 'Vermont',
  52. 'Virginia',
  53. 'Washington',
  54. 'West Virginia',
  55. 'Wisconsin',
  56. 'Wyoming',
  57. ]
  58. const list = states.map((item): ListItem => {
  59. return { value: `value:${item}`, label: `label:${item}` }
  60. })
  61. interface ListItem {
  62. value: string
  63. label: string
  64. }
  65. const value = ref([])
  66. const options = ref<ListItem[]>([])
  67. const loading = ref(false)
  68. const remoteMethod = (query: string) => {
  69. if (query !== '') {
  70. loading.value = true
  71. setTimeout(() => {
  72. loading.value = false
  73. options.value = list.filter(item => {
  74. return item.label.toLowerCase().includes(query.toLowerCase())
  75. })
  76. }, 200)
  77. } else {
  78. options.value = []
  79. }
  80. }
  81. </script>