5

I want to use bootstrap select plugin on a vuejs project and I am confused on how to incorporate it into my project as a directive. I came up with the following after looking at the select2 example in the vuejs website.

<span id="el">
  <select class="selectpicker" data-style="btn-primary" v-selectpicker="selected" :options="workflow">
    <option value="0">default</option>
  </select>                                                    
</span>

Here is my directive and view

require("bootstrap-select");

Vue.directive('selectpicker',{  

    twoWay: true,

    deep: true,

    bind: function() {          
        $(this.el).selectpicker().on("change", function(e) {
            this.set($(this.el).val());
        }.bind(this));
    },
    update: function (value) {
        $(this.el).selectpicker('refresh').trigger("change");
    },
    unbind: function () {
        $(this.el).off().selectpicker('destroy');
    }
});


new Vue({

    el: '#el',

    data: {

        tasksLoaded: false,
        tasks: [],
        workflow: [
            {
                id:'todo',
                value: 'To Do'
            }, 
            {
                id: 'backlog', 
                value: 'Backlog'
            },
            {
                id: 'doing', 
                value: 'Doing'
            },
            {
                id: 'restest', 
                value:'Retest'
            },
            {
                id: 'completed', 
                value: 'Completed'
            },
            {
                id: 'deployed',
                value: 'Deployed'
            }
        ],
        sections:[],
        tests:[],
        sectionId: '',
        testId: ''
    },

    watch: {            
        // watch these particular models for changes and then fire on trigger       
        sectionId: function(value) {                        
            this.getTests(value);                   
        },
        testId: function(value) {
            this.getResults(value);
        }
    }
});

I end up getting the error

Uncaught TypeError: $(...).selectpicker is not a function

and I have already checked if is already called or not and it is already present.

Also, I am using laravel spark so it already has jQuery defined inside the app.js file through

require('laravel-spark/core/bootstrap');

1 Answer 1

2

Can you show us where you are including jQuery and Bootstrap?

jquery must be available as $ when require("bootstrap-select"); is run. Here's how I have it in my app:

window.$ = window.jQuery = require('jquery');
require('bootstrap');
require('bootstrap-select');

These need to all 3 be called in the proper order, or else they won't properly extend each other

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.