autocomplete-template.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <el-autocomplete
  3. v-model="state"
  4. :fetch-suggestions="querySearch"
  5. popper-class="my-autocomplete"
  6. placeholder="Please input"
  7. @select="handleSelect"
  8. >
  9. <template #suffix>
  10. <el-icon class="el-input__icon" @click="handleIconClick">
  11. <edit />
  12. </el-icon>
  13. </template>
  14. <template #default="{ item }">
  15. <div class="value">{{ item.value }}</div>
  16. <span class="link">{{ item.link }}</span>
  17. </template>
  18. </el-autocomplete>
  19. </template>
  20. <script lang="ts" setup>
  21. import { onMounted, ref } from 'vue'
  22. import { Edit } from '@element-plus/icons-vue'
  23. interface LinkItem {
  24. value: string
  25. link: string
  26. }
  27. const state = ref('')
  28. const links = ref<LinkItem[]>([])
  29. const querySearch = (queryString: string, cb) => {
  30. const results = queryString
  31. ? links.value.filter(createFilter(queryString))
  32. : links.value
  33. // call callback function to return suggestion objects
  34. cb(results)
  35. }
  36. const createFilter = (queryString) => {
  37. return (restaurant) => {
  38. return (
  39. restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
  40. )
  41. }
  42. }
  43. const loadAll = () => {
  44. return [
  45. { value: 'vue', link: 'https://github.com/vuejs/vue' },
  46. { value: 'element', link: 'https://github.com/ElemeFE/element' },
  47. { value: 'cooking', link: 'https://github.com/ElemeFE/cooking' },
  48. { value: 'mint-ui', link: 'https://github.com/ElemeFE/mint-ui' },
  49. { value: 'vuex', link: 'https://github.com/vuejs/vuex' },
  50. { value: 'vue-router', link: 'https://github.com/vuejs/vue-router' },
  51. { value: 'babel', link: 'https://github.com/babel/babel' },
  52. ]
  53. }
  54. const handleSelect = (item: LinkItem) => {
  55. console.log(item)
  56. }
  57. const handleIconClick = (ev: Event) => {
  58. console.log(ev)
  59. }
  60. onMounted(() => {
  61. links.value = loadAll()
  62. })
  63. </script>
  64. <style>
  65. .my-autocomplete li {
  66. line-height: normal;
  67. padding: 7px;
  68. }
  69. .my-autocomplete li .name {
  70. text-overflow: ellipsis;
  71. overflow: hidden;
  72. }
  73. .my-autocomplete li .addr {
  74. font-size: 12px;
  75. color: #b4b4b4;
  76. }
  77. .my-autocomplete li .highlighted .addr {
  78. color: #ddd;
  79. }
  80. </style>