Select2 on change event is not working in Vuejs

前端 未结 4 592
野的像风
野的像风 2020-12-11 03:50

I was working with select2 in vuejs , I found vuejs is not working with jquery select2 as vuejs is working with navite html.

I am using this code

相关标签:
4条回答
  • 2020-12-11 04:28
    <select v-model="selected">
           <option value="1">1</option>
           <option value="2">2</option>
           <option value="3">3</option>
    </select>
    
    
    const self = this;
    $("select").change(function () {
            const val = $(this).find("option:selected").val();
            self.selected = val;
     });
    
    0 讨论(0)
  • 2020-12-11 04:30

    Try add v-on:change="onChange" in your select control

    HTML template

    <select id="supplier_id" class='form-control' 
    v-model='supplier_id' v-select='supplier_id' v-on:change="onChange">
        <option value="atul">Atul</option>
        <option value="niklesh">Niklesh</option>
        <option value="sachin">Sachin</option>
    </select>
    

    "onChange" is the name of your method

    JavaScript

    methods: {
      onChange(e) {        
          alert("Change");   
      }
    }
    
    0 讨论(0)
  • 2020-12-11 04:36

    To get this to work with a directive, we need to understand how v-model works. From the docs:

    <input v-model="something">
    

    is just syntactic sugar for:

    <input v-bind:value="something" v-on:input="something = $event.target.value">
    

    In the case of a select element, v-model will listen for the change event (not input). So, if the directive dispatches a change event when the element changes, then v-model will work as expected.

    Here is an updated version of your code:

    Vue.directive('select', {
      twoWay: true,
      bind: function (el, binding, vnode) {
        $(el).select2().on("select2:select", (e) => {
          // v-model looks for
          //  - an event named "change"
          //  - a value with property path "$event.target.value"
          el.dispatchEvent(new Event('change', { target: e.target }));
        });
      },
    });
    var app = new Vue({
      el: '#app',
      data: {
        supplier_id: "niklesh"
      },
    })
    $('#supplier_id').select2({});
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.4/vue.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.5/css/bootstrap.css">
    <div id="app">
      {{ supplier_id }}
    
      <select id="supplier_id" class='form-control' v-model='supplier_id' v-select='supplier_id'>
        <option value="atul">Atul</option>
        <option value="niklesh">Niklesh</option>
        <option value="sachin">Sachin</option>
      </select>
    
    </div>

    0 讨论(0)
  • 2020-12-11 04:39

    By assigning select2 value to vuejs data I am able to fix this problem. I didn't use custom directive here.

    var app = new Vue({
      el: '#app',
      data: {
        supplier_id: "niklesh"
      },
      filters: {
        capitalize: function (value) {
          if (!value) return ''
          value = value.toString()
          return value.charAt(0).toUpperCase() + value.slice(1)
        }
      }
    });
    
    $('#supplier_id').on("change",function(){
        app.supplier_id = $(this).val();
        console.log('Name : '+$(this).val());
    });
    
    $('#supplier_id').select2({});
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.4/vue.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.5/css/bootstrap.css">
    <div id="app">
      Name : {{ supplier_id | capitalize }}
    <select id="supplier_id" class='form-control' v-model='supplier_id'>
        <option value="atul">Atul</option>
        <option value="niklesh">Niklesh</option>
        <option value="sachin">Sachin</option>
    </select>
    
    </div>

    Please comment if this is not good way or any better solution you suggest.

    0 讨论(0)
提交回复
热议问题