Vue-multiselect: Default local options with AJAX

Created on 18 Jan 2018  路  1Comment  路  Source: shentao/vue-multiselect

I don't know how to implement the follwing functionality:

  • when the component load, I want to fill the options from a local array that the user can select by clicking in the list.
  • when the user starts to type in the search box, I want to replace the options with those returned by the AJAX server.

It this possible?

Most helpful comment

Yes, you can.

in data declare array options with local array and timer for debounce

data () {
    return {
        options: ['some', 'array'],
        debounceTimer: null
    }
}

then on @search-change event attach event listener, which will send ajax requests to server:
p.s. as an optimization you also need to add debounce, to prevent much count of requests on key pressing :)

asyncFind(query) {
    if (query.match(/^\s*$/)) {    // prevent white space requests
       return
    }
    this.isLoading = true   // use it to say to multiselect to show loading spinner
    clearTimeout(this.debounceTimer)   // clearing debounceTimer
    this.debounceTimer = setTimeout(() => {   // set timer 500 ms, if not reset with line above (by new search-change event), the function will be executes the ajax request to server
        axios.get(url, params) // axios -> http client for xmlhttprequests
                .then(response => { 
                    this.options= response.data  // setting from server options
                    this.isLoading = false  // say to multiselect to hide loading spinner
                })
    }, 500)
}

and v-bind:options="options" and @search-change="asyncFind" to vue-multiselect

>All comments

Yes, you can.

in data declare array options with local array and timer for debounce

data () {
    return {
        options: ['some', 'array'],
        debounceTimer: null
    }
}

then on @search-change event attach event listener, which will send ajax requests to server:
p.s. as an optimization you also need to add debounce, to prevent much count of requests on key pressing :)

asyncFind(query) {
    if (query.match(/^\s*$/)) {    // prevent white space requests
       return
    }
    this.isLoading = true   // use it to say to multiselect to show loading spinner
    clearTimeout(this.debounceTimer)   // clearing debounceTimer
    this.debounceTimer = setTimeout(() => {   // set timer 500 ms, if not reset with line above (by new search-change event), the function will be executes the ajax request to server
        axios.get(url, params) // axios -> http client for xmlhttprequests
                .then(response => { 
                    this.options= response.data  // setting from server options
                    this.isLoading = false  // say to multiselect to hide loading spinner
                })
    }, 500)
}

and v-bind:options="options" and @search-change="asyncFind" to vue-multiselect

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shsmad picture shsmad  路  3Comments

zachleigh picture zachleigh  路  3Comments

hskrishna29 picture hskrishna29  路  3Comments

wujekbogdan picture wujekbogdan  路  4Comments

dmitov picture dmitov  路  4Comments