Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use bootstrap select plugin with vuejs directive and browserify

Tags:

jquery

vue.js

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');
like image 325
Bazinga777 Avatar asked Oct 30 '22 09:10

Bazinga777


1 Answers

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

like image 95
Jeff Avatar answered Nov 09 '22 03:11

Jeff