autocomplete-template.vue 2.1 KB

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