How do you bind an input using vue.js when the input is rendered using a jquery plugin such as datepicker. In my example below the two way binding doesnt work for the jquery colourpicker. When I change the color it doesn't change the corresponding vue value:
<html>
<head>
<meta charset="UTF-8">
<title>Color picker for jQuery</title>
<meta name="description" content="A very simple jQuery color picker plugin">
<link rel="stylesheet" href="jquery.simplecolorpicker.css">
<link rel="stylesheet" href="jquery.simplecolorpicker-regularfont.css">
</head>
<body>
<div id="cars">
<table>
<tr v-for="car in cars">
<td>
<input name="name" type="text" v-colorpicker="car.name">
</td>
<td>
<select class="colorpicker" name="color" v-model="car.color">
<option value="#cc6699">#cc6699</option>
<option value="#993366">#993366</option>
<option value="#cc3333">#cc3333</option>
<option value="#336699">#336699</option>
</select>
</td>
</tr>
</table>
<pre>{{$data | json }}</pre>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.js"></script>
<script src="jquery.simplecolorpicker.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/vue/1.0.13/vue.min.js"></script>
<script>
new Vue({
el: '#cars',
data: {
cars: [{
name: 'Toyota',
color: '#cc6699'
}, {
name: 'Nissan',
color: '#336699'
}]
}
});
$('select.colorpicker').simplecolorpicker({
picker: true,
theme: "regularfont"
});
</script>
</body>
</html>
*** UPDATE ****
I've manage to get something working using the following directive, the only problem is when the page first loads, each picker isn't selected with the correct color as per the cars array - they all load with the first option as selected - do i need to add an onload method to the directive below:
Vue.directive('colorpicker', {
twoWay: true,
priority: 1000,
params: ['options'],
bind: function () {
var self = this
$(this.el)
.simplecolorpicker({
picker: true,
theme: "regularfont"
})
.on('change', function () {
self.set(this.value)
})
},
update: function (value) {
$(this.el).val(value).trigger('change')
},
unbind: function () {
$(this.el).off().simplecolorpicker('destroy')
}
})
select.colorpickerinstance? Which one would jquery change? Try generating an id using the car $index and use that selector.colorpickerto wire it up and yes it is rendered next to each car. The color picker works fine but the two way binding doesn't work. If you look at the data in the pre tag you can see the data value doesnt change when you select different colors.