自动补全输入框
根据输入内容提供对应的输入建议
Autodcomplete 组件提供输入建议。 fetch-suggestions
属性是返回建议输入的方法。 在此示例中, querySearch(queryString, cb)
返回建议通过 cb(data)
自动完成建议。
<template>
<el-row class="demo-autocomplete text-center">
<el-col :span="12">
<div class="sub-title my-2 text-sm text-gray-600">
list suggestions when activated
</div>
<el-autocomplete
v-model="state1"
:fetch-suggestions="querySearch"
clearable
class="inline-input w-50"
placeholder="Please Input"
@select="handleSelect"
/>
</el-col>
<el-col :span="12">
<div class="sub-title my-2 text-sm text-gray-600">
list suggestions on input
</div>
<el-autocomplete
v-model="state2"
:fetch-suggestions="querySearch"
:trigger-on-focus="false"
clearable
class="inline-input w-50"
placeholder="Please Input"
@select="handleSelect"
/>
</el-col>
</el-row>
</template>
<script lang="ts" setup>
import { onMounted, ref } from 'vue'
interface RestaurantItem {
value: string
link: string
}
const state1 = ref('')
const state2 = ref('')
const restaurants = ref<RestaurantItem[]>([])
const querySearch = (queryString: string, cb: any) => {
const results = queryString
? restaurants.value.filter(createFilter(queryString))
: restaurants.value
// call callback function to return suggestions
cb(results)
}
const createFilter = (queryString: string) => {
return (restaurant: RestaurantItem) => {
return (
restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
)
}
}
const loadAll = () => {
return [
{ value: 'vue', link: 'https://github.com/vuejs/vue' },
{ value: 'element', link: 'https://github.com/ElemeFE/element' },
{ value: 'cooking', link: 'https://github.com/ElemeFE/cooking' },
{ value: 'mint-ui', link: 'https://github.com/ElemeFE/mint-ui' },
{ value: 'vuex', link: 'https://github.com/vuejs/vuex' },
{ value: 'vue-router', link: 'https://github.com/vuejs/vue-router' },
{ value: 'babel', link: 'https://github.com/babel/babel' },
]
}
const handleSelect = (item: RestaurantItem) => {
console.log(item)
}
onMounted(() => {
restaurants.value = loadAll()
})
</script>
自定义模板
自定义如何显示输入建议。
使用 scoped slot
自定义输入建议。 在这个范围中,你可以使用 item
键来访问当前输入建议对象。
<template>
<el-autocomplete
v-model="state"
:fetch-suggestions="querySearch"
popper-class="my-autocomplete"
placeholder="Please input"
@select="handleSelect"
>
<template #suffix>
<el-icon class="el-input__icon" @click="handleIconClick">
<edit />
</el-icon>
</template>
<template #default="{ item }">
<div class="value">{{ item.value }}</div>
<span class="link">{{ item.link }}</span>
</template>
</el-autocomplete>
</template>
<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import { Edit } from '@element-plus/icons-vue'
interface LinkItem {
value: string
link: string
}
const state = ref('')
const links = ref<LinkItem[]>([])
const querySearch = (queryString: string, cb) => {
const results = queryString
? links.value.filter(createFilter(queryString))
: links.value
// call callback function to return suggestion objects
cb(results)
}
const createFilter = (queryString) => {
return (restaurant) => {
return (
restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
)
}
}
const loadAll = () => {
return [
{ value: 'vue', link: 'https://github.com/vuejs/vue' },
{ value: 'element', link: 'https://github.com/ElemeFE/element' },
{ value: 'cooking', link: 'https://github.com/ElemeFE/cooking' },
{ value: 'mint-ui', link: 'https://github.com/ElemeFE/mint-ui' },
{ value: 'vuex', link: 'https://github.com/vuejs/vuex' },
{ value: 'vue-router', link: 'https://github.com/vuejs/vue-router' },
{ value: 'babel', link: 'https://github.com/babel/babel' },
]
}
const handleSelect = (item: LinkItem) => {
console.log(item)
}
const handleIconClick = (ev: Event) => {
console.log(ev)
}
onMounted(() => {
links.value = loadAll()
})
</script>
<style>
.my-autocomplete li {
line-height: normal;
padding: 7px;
}
.my-autocomplete li .name {
text-overflow: ellipsis;
overflow: hidden;
}
.my-autocomplete li .addr {
font-size: 12px;
color: #b4b4b4;
}
.my-autocomplete li .highlighted .addr {
color: #ddd;
}
</style>
远程搜索
从服务端搜索数据
<template>
<el-autocomplete
v-model="state"
:fetch-suggestions="querySearchAsync"
placeholder="Please input"
@select="handleSelect"
/>
</template>
<script lang="ts" setup>
import { onMounted, ref } from 'vue'
const state = ref('')
interface LinkItem {
value: string
link: string
}
const links = ref<LinkItem[]>([])
const loadAll = () => {
return [
{ value: 'vue', link: 'https://github.com/vuejs/vue' },
{ value: 'element', link: 'https://github.com/ElemeFE/element' },
{ value: 'cooking', link: 'https://github.com/ElemeFE/cooking' },
{ value: 'mint-ui', link: 'https://github.com/ElemeFE/mint-ui' },
{ value: 'vuex', link: 'https://github.com/vuejs/vuex' },
{ value: 'vue-router', link: 'https://github.com/vuejs/vue-router' },
{ value: 'babel', link: 'https://github.com/babel/babel' },
]
}
let timeout: NodeJS.Timeout
const querySearchAsync = (queryString: string, cb: (arg: any) => void) => {
const results = queryString
? links.value.filter(createFilter(queryString))
: links.value
clearTimeout(timeout)
timeout = setTimeout(() => {
cb(results)
}, 3000 * Math.random())
}
const createFilter = (queryString: string) => {
return (restaurant: LinkItem) => {
return (
restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
)
}
}
const handleSelect = (item: LinkItem) => {
console.log(item)
}
onMounted(() => {
links.value = loadAll()
})
</script>
Autocomplete 属性
参数属性 | 详情 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
placeholder | 占位文本 | string | — | — |
clearable | 是否可清空 | boolean | — | false |
disabled | 是否禁用 | boolean | — | false |
value-key | 输入建议对象中用于显示的键名 | string | — | value |
model-value / v-model | 选中项绑定值 | string | — | — |
debounce | 获取输入建议的防抖延时 | number | — | 300 |
placement | 菜单弹出位置 | string | top / top-start / top-end / bottom / bottom-start / bottom-end | bottom-start |
fetch-suggestions | 获取输入建议的方法, 仅当你的输入建议数据 resolve 时,通过调用 callback(data:[]) 来返回它 | Function(queryString, callback) | — | — |
popper-class | 下拉列表的类名 | string | — | — |
trigger-on-focus | 是否在输入框 focus 时显示建议列表 | boolean | — | true |
name | 等价于原生 input name 属性 | string | — | — |
select-when-unmatched | 在输入没有任何匹配建议的情况下,按下回车是否触发 select 事件 | boolean | — | false |
label | 标签文本 | string | — | — |
hide-loading | 是否隐藏远程加载时的加载图标 | boolean | — | false |
popper-append-to-body(deprecated) | 是否将下拉列表插入至 body 元素。 在下拉列表的定位出现问题时,可将该属性设置为 false | boolean | — | false |
teleported | 是否将下拉列表插入至 body 元素 | boolean | true / false | true |
highlight-first-item | 是否默认突出显示远程搜索建议中的第一项 | boolean | — | false |
fit-input-width | 下拉框是否与输入框同宽 | boolean | — | false |
Autocomplete 插槽
插槽名 | 详情 |
---|---|
— | 自定义输入建议的内容。 自定义标签,参数为 |
prefix | 输入框占位文本 |
suffix | 输入框尾部内容 |
prepend | 输入框前置内容 |
append | 输入框后置内容 |
Autocomplete 事件
事件名 | 描述说明 | 参数 |
---|---|---|
select | 点击选中建议项时触发 | 选中的建议项 |
change | 在 Input 值改变时触发 | (value: string | number) |
Autocomplete 方法
方法 | 详情 | 参数 |
---|---|---|
focus | 使 input 获取焦点 | — |
blur | 使 input 失去焦点 | — |