remote-search.vue 2.4 KB

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