remote-search.vue 1.6 KB

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