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
JavaScript
x
19
19
1
Vue.directive('select', {
2
twoWay: true,
3
bind: function () {
4
$(this.el).select2()
5
.on("select2:select", function(e) {
6
this.set($(this.el).val());
7
}.bind(this));
8
},
9
update: function(nv, ov) {
10
$(this.el).trigger("change");
11
}
12
});
13
var app = new Vue({
14
el: '#app',
15
data: {
16
supplier_id: "niklesh"
17
}
18
})
19
$('#supplier_id').select2({});
JavaScript
1
15
15
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
2
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.4/vue.js"></script>
3
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.js"></script>
4
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css">
5
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.5/css/bootstrap.css">
6
<div id="app">
7
{{ supplier_id }}
8
9
<select id="supplier_id" class='form-control' v-model='supplier_id' v-select='supplier_id'>
10
<option value="atul">Atul</option>
11
<option value="niklesh">Niklesh</option>
12
<option value="sachin">Sachin</option>
13
</select>
14
15
</div>
Please share your reply to handle this problem.
Advertisement
Answer
To get this to work with a directive, we need to understand how v-model
works. From the docs:
JavaScript121<input v-model="something">
2
is just syntactic sugar for:
JavaScript121<input v-bind:value="something" v-on:input="something = $event.target.value">
2
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 (works in Vue 2):
JavaScript
1
22
22
1
Vue.directive('select', {
2
twoWay: true,
3
bind: function (el, binding, vnode) {
4
$(el).select2().on("select2:select", (e) => {
5
// v-model looks for
6
// - an event named "change"
7
// - a value with property path "$event.target.value"
8
el.dispatchEvent(new Event('change', { target: e.target }));
9
});
10
},
11
componentUpdated: function(el, me) {
12
// update the selection if the value is changed externally
13
$(el).trigger("change");
14
}
15
});
16
var app = new Vue({
17
el: '#app',
18
data: {
19
supplier_id: "niklesh"
20
},
21
})
22
$('#supplier_id').select2({});
JavaScript
1
15
15
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
2
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.4/vue.js"></script>
3
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.js"></script>
4
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css">
5
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.5/css/bootstrap.css">
6
<div id="app">
7
{{ supplier_id }}
8
9
<select id="supplier_id" class='form-control' v-model='supplier_id' v-select='supplier_id'>
10
<option value="atul">Atul</option>
11
<option value="niklesh">Niklesh</option>
12
<option value="sachin">Sachin</option>
13
</select>
14
15
</div>
And here’s a version that works in Vue 3 (custom directives have different syntax, linked here):
JavaScript
1
26
26
1
var app = Vue.createApp({
2
data: function() {
3
return {
4
supplier_id: "niklesh"
5
}
6
}
7
})
8
9
app.directive('select', {
10
beforeMount: function (el, binding, vnode) {
11
$(el).select2().on("select2:select", (e) => {
12
// v-model looks for
13
// - an event named "change"
14
// - a value with property path "$event.target.value"
15
el.dispatchEvent(new Event('change', { target: e.target }));
16
});
17
},
18
updated: function(el) {
19
// update the selection if the value is changed externally
20
$(el).trigger("change");
21
}
22
});
23
24
app.mount('#app');
25
26
$('#supplier_id').select2({});
JavaScript
1
15
15
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
2
<script src="https://unpkg.com/vue@3.0.11"></script>
3
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.js"></script>
4
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css">
5
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.5/css/bootstrap.css">
6
<div id="app">
7
{{ supplier_id }}
8
9
<select id="supplier_id" class='form-control' v-model='supplier_id' v-select='supplier_id'>
10
<option value="atul">Atul</option>
11
<option value="niklesh">Niklesh</option>
12
<option value="sachin">Sachin</option>
13
</select>
14
15
</div>