0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

vue.js autocomplete

Last updated at Posted at 2019-11-21

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>

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?