element ui を使っている場合は、
element ui についてる auto complete を使うのがベストか。
コピペで動く。
追記
いやーこれなら、
elmentui の ¶Basic multiple select のが便利かもしれんな。。。
<template>
<div>
<el-row>
<el-col class="flex-center">
<h2> Vue.js Autocomplete </h2>
<div>
<el-autocomplete
v-model="state"
:fetch-suggestions="querySearch"
@select="handleSelect"
placeholder="Please input"
:trigger-on-focus="false">
</el-autocomplete>
</div>
</el-col>
</el-row>
<!--:keyってのが無いとバグる-->
<div v-for="(v,key) in selectedTags" :key="key">
選択されたID は {{v}}
</div>
</div>
</template>
<script>
export default {
data() {
return {
links: [],
state: '',
selectedTags: []
};
},
methods: {
querySearch(query, cb) {
var links = this.links;
var results = query ? links.filter(this.createFilter(query)) : links;
var top3 = results.slice(0, 3);
console.log(JSON.stringify(top3));
cb(top3); // number of things returned
},
createFilter(query) {
return (link) => {
return link.value.toLowerCase().includes(query.toLowerCase());
};
},
loadAll() {
return [
{"id": 0, "value": "うどん", "link": "https://github.com/vuejs/vue" },
{"id": 1, "value": "うんこ", "link": "https://github.com/vuejs/vue" },
{"id": 2, "value": "らーめん", "link": "https://github.com/vuejs/vue" },
{"id": 3, "value": "vuex", "link": "https://github.com/vuejs/vue" },
{"id": 4, "value": "vue-router", "link": "https://github.com/vuejs/vue" },
{"id": 5, "value": "vue-resource", "link": "https://github.com/vuejs/vue" },
{"id": 6, "value": "element", "link": "https://github.com/ElemeFE/element" },
{"id": 7, "value": "あい", "link": "https://github.com/vuejs/vue" },
];
},
handleSelect(item) {
console.log("選択されたアイテムのIDは" + item.id);
this.state = "";
this.selectedTags.push(item.id);
}
},
mounted() {
this.links = this.loadAll();
}
}
</script>