简体   繁体   中英

Vue multiselect with axios call to database, laravel

For some reason, I can't quite figure out how to get an 'autocomplete' style multiselect with Vue working properly.

I properly set the route that is being called in my axios block, and the controller is set to use the query as a way to hit the database with a LIKE clause, but something is going wrong somewhere and my multiselect is not being filled with results from database that would make it searchable.

What am I doing wrong here?

Route:

Route::get('search', 'Controller@searchTags')
    ->name('search');

Controller:

public function searchTags(Request $request)
{
    if ($request->get('query')) {
        $query = $request->get('query');
        $data = TAGS::where('TAG_DATA', 'LIKE', "%{$query}%")->get();

        $output = '<ul class="dropdown-menu" style="display:block; position:relative">';
        foreach ($data as $row) {
            $output .= '<li><a href="#">' . $row->tag_data . '</a></li>';
        }
        $output .= '</ul>';

        return $output;
    }
}

Blade:

<div id="tagContent">
    <multiselect v-model="value" open-direction="bottom" :options="options" :multiple="true" :close-on-select="false" :taggable="true" :clear-on-select="false" :preserve-search="true" placeholder="Add Tag(s)" label="name" track-by="name">
        <template slot="selection" slot-scope="{ values, search, isOpen }"><span class="multiselect__single" v-if="values.length &amp;&amp; !isOpen">{{ values.length }} options selected</span></template>
    </multiselect>
</div>

new Vue({
        components: {
            Multiselect: window.VueMultiselect.default
        },
        data () {
            return {
                value: [],
                options: []
            }
        },
        methods: {
            read(){
                window.axios.get('campaigns/search').then(({ data }) =>{
                    console.log(data)
                });
            },
            addTag (newTag) {
              const tag = {
                name: newTag,
                code: newTag.substring(0, 2) + Math.floor((Math.random() * 10000000))
              }
              this.options.push(tag)
              this.value.push(tag)
            }
        }
    }).$mount('#tagContent');

There are a few things missing from your example, I believe.

  1. You need to trigger the read function when the search input changes - use the @search-change event for that

  2. You need to make use of the results of your axios request by sending them to this.options , so that the multiselect component can make use of them.

In this example , I've mocked the data request using a timeout, but you should get the idea. You can also make use of the loading property to let your users know something is happening behind the scenes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM