VueJS Select2 directive does not fire @change event

As the name says, I registered the Select2 directive for VueJS 1.0.15 using an example from my page. I want to catch the @change event, but it does not work.

HTML:

<select v-select="item.service" :selected="item.service" @change="serviceChange(item)">
  <option value="1">test 1</option>
  <option value="2">test 2</option>
</select>

JS:

Vue.directive('select', {
    twoWay: true,
    params: ['selected'],
    bind: function () {
        var self = this
        $(this.el)
            .select2({
                minimumResultsForSearch: Infinity
            })
            .val(this.params.selected)
            .on('change', function () {
                console.log('changed');
                self.set(this.value);
            })
    },
    update: function (value) {
        $(this.el).val(value).trigger('change')
    },
    unbind: function () {
        $(this.el).off().select2('destroy')
    }
});

var Checkout = new Vue({
      el: '.Checkout',
      methods: {
      serviceChange: function (item) {
          console.log(item);
      },
    }
});
+4
source share
4 answers

A detailed answer is Alberto Garcia.

var selectDropDown = $(".select-drop-down").select2();

selectDropDown.on('select2:select', function (e) {
    var event = new Event('change');
    e.target.dispatchEvent(event);
});
+6
source

Yes, I ran into this problem. We haven't figured out a solution yet, but this is my current round work using watch:

In VueJS:

watch: {

    /**
     * Watch changes on the select2 input
     *
     * @param integer personId
     */
    'person': function (personId) {
        var personName = $("#person").select2('data')[0].text
        this.doSomethingWithPerson(personId, personName)
    }
},

Then in your HTML:

<select
        id="person"
        v-select2="person"
        class="form-control"
        data-options="{{ $peopleJSON }}"
        data-placeholder="Choose a person"
>
</select>

Note. In the above HTML, {{ $peopleJSON }}I just embed the server side JSON server using the Blade template engine. The question is not entirely appropriate.

0

select2 vuejs, , :

https://jsfiddle.net/alberto1el/7rpko644/

var search = $(".search").select2({
  ajax: {
    url: "/echo/json",
    dataType: 'json',
    delay: 250,
    data: function (params) {
      return {
        q: params.term
      };
    },
    processResults: function (data, params) {
      var mockResults = [{id: 1,text: "Option 1"}, {id: 2,text: "Option 2"}, {id: 3,text: "Option 3"}];
      return {results: mockResults};
    },
    cache: true
  },
  escapeMarkup: function (markup) { return markup; },
  minimumInputLength: 1
});

search.on("select2:select", function (e){
    $('.result_search').val( $(this).val() ).trigger( 'change' );
});

var vm = new Vue({
  el: "#app",
  data: {
    busqueda_result : ''
  }
});

I know that you are using the directive, but maybe this helps you

0
source

Shouldn't you use $ emit?

update: function (value) {
    $(this.el).val(value);
    this.$emit('change');
}
-1
source

Source: https://habr.com/ru/post/1625510/


All Articles