mirror of
https://gitee.com/element-plus/element-plus.git
synced 2024-12-14 17:31:02 +08:00
93 lines
2.2 KiB
Vue
93 lines
2.2 KiB
Vue
|
<template>
|
||
|
<el-autocomplete
|
||
|
v-model="state"
|
||
|
:fetch-suggestions="querySearch"
|
||
|
popper-class="my-autocomplete"
|
||
|
placeholder="Please input"
|
||
|
@select="handleSelect"
|
||
|
>
|
||
|
<template #suffix>
|
||
|
<i class="el-icon-edit el-input__icon" @click="handleIconClick"></i>
|
||
|
</template>
|
||
|
<template #default="{ item }">
|
||
|
<div class="value">{{ item.value }}</div>
|
||
|
<span class="link">{{ item.link }}</span>
|
||
|
</template>
|
||
|
</el-autocomplete>
|
||
|
</template>
|
||
|
|
||
|
<script lang="ts">
|
||
|
import { defineComponent, ref, onMounted } from 'vue'
|
||
|
export default defineComponent({
|
||
|
setup() {
|
||
|
const links = ref([])
|
||
|
|
||
|
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) => {
|
||
|
console.log(item)
|
||
|
}
|
||
|
|
||
|
const handleIconClick = (ev) => {
|
||
|
console.log(ev)
|
||
|
}
|
||
|
|
||
|
onMounted(() => {
|
||
|
links.value = loadAll()
|
||
|
})
|
||
|
|
||
|
return {
|
||
|
links,
|
||
|
state: ref(''),
|
||
|
querySearch,
|
||
|
createFilter,
|
||
|
loadAll,
|
||
|
handleSelect,
|
||
|
handleIconClick,
|
||
|
}
|
||
|
},
|
||
|
})
|
||
|
</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>
|