Reputation: 1525
I need to have multiple select in Vue withut library. I found a good jsFiddle code. Here:
https://jsfiddle.net/02rafh8p/
I have write this directive in another js to have it globally like this:
import Vue from 'vue';
export const Select = {
twoWay: true,
priority: 1000,
params: ['options'],
bind: function() {
let self = this;
$(this.el)
.select2({
data: this.params.options
})
.on('change', function() {
self.set($(self.el).val())
})
},
update: function(value) {
$(this.el).val(value).trigger('change')
},
unbind: function() {
$(this.el).off().select2('destroy')
}
};
Vue.directive('select', Select);
And here is my component where I want to have my custom directive:
<template>
<div id="el">
<p>Selected: {{selected}}</p>
<select v-select="selected" multiple :options="roles2" style="width: 400px; height: 1em;">
<option value="0">default</option></select>
</div>
</template>
import {Select} from '../select.js';
export default {
directives: {
Select
},
data() {
return {
form: new Form({
memberId: this.member.id,
firstname: this.member.user.firstname,
lastname: this.member.user.lastname,
email: this.member.user.email,
roles: Object.values(this.member.actual_roles),
rate: this.member.billing.rate,
currency: this.member.billing.currency_id,
type: this.member.billing.type
}),
fullname: this.member.user.full_name,
selected: [],
roles2: [
{id: 1, text: 'hello'},
{id: 2, text: 'what'}
]
}
},
}
And first error is :
TypeError: Cannot read property 'el' of undefined
When I change in select.js this piece:
let self = this;
$(this.el) => hange to : $('#el')
.select2({
data: this.params.options
}) ...
then I have another error:
TypeError: Cannot read property 'params' of undefined
This is my first custom directive and I don't know why It doesn't work completley. Can someone help me to figure it out? I don't even know or my jQuery to find #el is goood :(
Upvotes: 0
Views: 897
Reputation: 2550
The vue version of your fiddle is 1.0.16. https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.16/vue.js
I think you are using latest version of vue. So you will need to following the guide https://v2.vuejs.org/v2/guide/custom-directive.html.
Upvotes: 1