remote-search.vue 2.4 KB

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