Skip to content

Commit d4bf067

Browse files
committed
add select2 example
1 parent ce16e4e commit d4bf067

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

examples/select2/index.html

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Vue.js custom directive integration example (select2)</title>
6+
<script src="../../dist/vue.js"></script>
7+
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
8+
<link href="http://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css" rel="stylesheet">
9+
<script src="http://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js"></script>
10+
<style>
11+
select {
12+
min-width: 300px;
13+
}
14+
</style>
15+
</head>
16+
<body>
17+
18+
<div id="el">
19+
<p>Selected: {{selected}}</p>
20+
<select v-select="selected" options="options">
21+
<option value="0">default</option>
22+
</select>
23+
</div>
24+
25+
<script>
26+
Vue.directive('select', {
27+
twoWay: true,
28+
bind: function () {
29+
var optionsData
30+
var optionsExpression = this._checkParam('options')
31+
if (optionsExpression) {
32+
optionsData = this.vm.$eval(optionsExpression)
33+
}
34+
var self = this
35+
$(this.el)
36+
.select2({
37+
data: optionsData
38+
})
39+
.on('change', function () {
40+
self.set(this.value)
41+
})
42+
},
43+
update: function (value) {
44+
$(this.el).val(value).trigger('change')
45+
},
46+
unbind: function () {
47+
$(this.el).off().select2('destroy')
48+
}
49+
})
50+
51+
var vm = new Vue({
52+
el: '#el',
53+
data: {
54+
selected: 0,
55+
options: [
56+
{ id: 1, text: 'hello' },
57+
{ id: 2, text: 'what' }
58+
]
59+
}
60+
})
61+
</script>
62+
</body>
63+
</html>

0 commit comments

Comments
 (0)