Skip to content
Advertisement

Bootstrap-select & Vue.js: selectpicker(‘refresh’) not working

I am trying to implement the bootstrap-select plugin (found here https://developer.snapappointments.com/bootstrap-select/) into a ruby on rails app with Vue.js as the javascript framework.

The objective is that in one select you can pick a category and in the other one you can pick all the teachers available for that category. To do this i use axios and vue to do a request to my own api and use that to populate the second select and it works fine with a simple select field, however I want bootstrap-select to be shown, I am aware the plugin has a function “selectpicker(‘refresh’)” to make the option reload possible but my browser console claims selectpicker is not a function when I call it on my vue instance, I can manually run it on the browser console however and it works as intended

My code:

js:

JavaScript

view:

JavaScript

Finally my application.js

JavaScript

I would really appreciate if someone could help as i simply can’t figure out what to do

Advertisement

Answer

Try changing to the following:

JavaScript

I ran into this problem in an application I’m developing, and wrote an article with a more detailed explanation, including broken and working examples. My examples change the options via JavaScript without AJAX, but the concept is the same.

In the code above, you are calling the bootstrap-select ‘refresh’ using Vue’s $nextTick function which waits until the Vue object’s update is complete before executing the code passed to the $nextTick call. (see $nextTick on Vue’s documentation)

Note: You could also do a more generic update in the Vue’s ‘update’ event handler by adding:

JavaScript

Please be aware that this will always update all bootstrap-selects that share the same class ‘selectpicker’. The update will occur on every update of the vue data, including those unrelated to the <select>s in question, which is wasteful and could potentially cause performance issues.

I believe the best practice is to refresh just the <select> that needs updating, at the moment it needs the refresh (in your case when the AJAX call returns and the ‘teachers’ property has been updated.)

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement