自动补全输入框

根据输入内容提供对应的输入建议

Autodcomplete 组件提供输入建议。 fetch-suggestions 属性是返回建议输入的方法。 在此示例中, querySearch(queryString, cb) 返回建议通过 cb(data) 自动完成建议。

Autocomplete 自动补全输入框 - 图1

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

自定义模板

自定义如何显示输入建议。

使用 scoped slot 自定义输入建议。 在这个范围中,你可以使用 item 键来访问当前输入建议对象。

Autocomplete 自动补全输入框 - 图2

  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>

远程搜索

从服务端搜索数据

Autocomplete 自动补全输入框 - 图3

  1. <template>
  2. <el-autocomplete
  3. v-model="state"
  4. :fetch-suggestions="querySearchAsync"
  5. placeholder="Please input"
  6. @select="handleSelect"
  7. />
  8. </template>
  9. <script lang="ts" setup>
  10. import { onMounted, ref } from 'vue'
  11. const state = ref('')
  12. interface LinkItem {
  13. value: string
  14. link: string
  15. }
  16. const links = ref<LinkItem[]>([])
  17. const loadAll = () => {
  18. return [
  19. { value: 'vue', link: 'https://github.com/vuejs/vue' },
  20. { value: 'element', link: 'https://github.com/ElemeFE/element' },
  21. { value: 'cooking', link: 'https://github.com/ElemeFE/cooking' },
  22. { value: 'mint-ui', link: 'https://github.com/ElemeFE/mint-ui' },
  23. { value: 'vuex', link: 'https://github.com/vuejs/vuex' },
  24. { value: 'vue-router', link: 'https://github.com/vuejs/vue-router' },
  25. { value: 'babel', link: 'https://github.com/babel/babel' },
  26. ]
  27. }
  28. let timeout: NodeJS.Timeout
  29. const querySearchAsync = (queryString: string, cb: (arg: any) => void) => {
  30. const results = queryString
  31. ? links.value.filter(createFilter(queryString))
  32. : links.value
  33. clearTimeout(timeout)
  34. timeout = setTimeout(() => {
  35. cb(results)
  36. }, 3000 * Math.random())
  37. }
  38. const createFilter = (queryString: string) => {
  39. return (restaurant: LinkItem) => {
  40. return (
  41. restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
  42. )
  43. }
  44. }
  45. const handleSelect = (item: LinkItem) => {
  46. console.log(item)
  47. }
  48. onMounted(() => {
  49. links.value = loadAll()
  50. })
  51. </script>

Autocomplete 属性

参数属性详情类型可选值默认值
placeholder占位文本string
clearable是否可清空booleanfalse
disabled是否禁用booleanfalse
value-key输入建议对象中用于显示的键名stringvalue
model-value / v-model选中项绑定值string
debounce获取输入建议的防抖延时number300
placement菜单弹出位置stringtop / top-start / top-end / bottom / bottom-start / bottom-endbottom-start
fetch-suggestions获取输入建议的方法, 仅当你的输入建议数据 resolve 时,通过调用 callback(data:[]) 来返回它Function(queryString, callback)
popper-class下拉列表的类名string
trigger-on-focus是否在输入框 focus 时显示建议列表booleantrue
name等价于原生 input name 属性string
select-when-unmatched在输入没有任何匹配建议的情况下,按下回车是否触发 select 事件booleanfalse
label标签文本string
hide-loading是否隐藏远程加载时的加载图标booleanfalse
popper-append-to-body(deprecated)是否将下拉列表插入至 body 元素。 在下拉列表的定位出现问题时,可将该属性设置为 falsebooleanfalse
teleported是否将下拉列表插入至 body 元素booleantrue / falsetrue
highlight-first-item是否默认突出显示远程搜索建议中的第一项booleanfalse
fit-input-width下拉框是否与输入框同宽booleanfalse

Autocomplete 插槽

插槽名详情
自定义输入建议的内容。 自定义标签,参数为
prefix输入框占位文本
suffix输入框尾部内容
prepend输入框前置内容
append输入框后置内容

Autocomplete 事件

事件名描述说明参数
select点击选中建议项时触发选中的建议项
change在 Input 值改变时触发(value: string | number)

Autocomplete 方法

方法详情参数
focus使 input 获取焦点
blur使 input 失去焦点

源代码

组件 Autocomplete 自动补全输入框 - 图4 文档 Autocomplete 自动补全输入框 - 图5

贡献者

Autocomplete 自动补全输入框 - 图6 三咲智子

Autocomplete 自动补全输入框 - 图7 云游君

Autocomplete 自动补全输入框 - 图8 JeremyWuuuuu

Autocomplete 自动补全输入框 - 图9 kooriookami

Autocomplete 自动补全输入框 - 图10 qiang

Autocomplete 自动补全输入框 - 图11 bqy_fe

Autocomplete 自动补全输入框 - 图12 opengraphica

Autocomplete 自动补全输入框 - 图13 0song

Autocomplete 自动补全输入框 - 图14 Stephen.K

Autocomplete 自动补全输入框 - 图15 Xc

Autocomplete 自动补全输入框 - 图16 Delyan Haralanov

Autocomplete 自动补全输入框 - 图17 Carter Li

Autocomplete 自动补全输入框 - 图18 blackie

Autocomplete 自动补全输入框 - 图19 iamkun

Autocomplete 自动补全输入框 - 图20 btea

Autocomplete 自动补全输入框 - 图21 Aex

Autocomplete 自动补全输入框 - 图22 on the field of hope

Autocomplete 自动补全输入框 - 图23 Simona

Autocomplete 自动补全输入框 - 图24 Hades-li

Autocomplete 自动补全输入框 - 图25 zz