autocomplete.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <template>
  2. <el-row class="demo-autocomplete text-center">
  3. <el-col :span="12">
  4. <div class="sub-title my-2 text-sm text-gray-600">list suggestions when activated</div>
  5. <el-autocomplete v-model="state1" :fetch-suggestions="querySearch" clearable class="inline-input w-50" placeholder="Please Input" @select="handleSelect" />
  6. </el-col>
  7. <el-col :span="12">
  8. <div class="sub-title my-2 text-sm text-gray-600">list suggestions on input</div>
  9. <el-autocomplete v-model="state2" :fetch-suggestions="querySearch" :trigger-on-focus="false" clearable class="inline-input w-50" placeholder="Please Input" @select="handleSelect" />
  10. </el-col>
  11. </el-row>
  12. </template>
  13. <script lang="ts" setup>
  14. import { onMounted, ref } from 'vue'
  15. interface RestaurantItem {
  16. value: string
  17. link: string
  18. }
  19. const state1 = ref('')
  20. const state2 = ref('')
  21. const restaurants = ref<RestaurantItem[]>([])
  22. const querySearch = (queryString: string, cb: any) => {
  23. const results = queryString ? restaurants.value.filter(createFilter(queryString)) : restaurants.value
  24. // call callback function to return suggestions
  25. cb(results)
  26. }
  27. const createFilter = (queryString: string) => {
  28. return (restaurant: RestaurantItem) => {
  29. return restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
  30. }
  31. }
  32. const loadAll = () => {
  33. return [
  34. { value: 'vue', link: 'https://github.com/vuejs/vue' },
  35. { value: 'element', link: 'https://github.com/ElemeFE/element' },
  36. { value: 'cooking', link: 'https://github.com/ElemeFE/cooking' },
  37. { value: 'mint-ui', link: 'https://github.com/ElemeFE/mint-ui' },
  38. { value: 'vuex', link: 'https://github.com/vuejs/vuex' },
  39. { value: 'vue-router', link: 'https://github.com/vuejs/vue-router' },
  40. { value: 'babel', link: 'https://github.com/babel/babel' },
  41. ]
  42. }
  43. const handleSelect = (item: RestaurantItem) => {
  44. console.log(item)
  45. }
  46. onMounted(() => {
  47. restaurants.value = loadAll()
  48. })
  49. </script>