Reputation: 481
I have a mostly working Vue multiselect where I"m using an autocomplete function through an axios call to a database. As far as returning the DB results and adding them as options, then tags, it works perfectly.
My problem is that I can't create new tags. So if my axios call returns "Testing one" as an option and I select it, it become a tag. However, if no results are returned when I typ "new test" and hit enter, it doesn't become a new tag.
WHat am I doing wrong?
<div id="tagApp">
<multiselect
label="tag_data"
track-by="campaign_tag_id"
v-model="value"
:options="options"
:loading="loading"
:multiple="true"
:taggable="true"
@search-change="val => read(val)"
></multiselect>
</div>
new Vue({
components: {
Multiselect: window.VueMultiselect.default
},
el: "#tagApp",
data() {
return{
value: [],
loading: false,
options: []
}
},
methods: {
read: function(val){
//console.log('searched for', val);
if (val) {
this.loading = true;
this.options = [];
const self = this;
console.log(val);
axios.get('campaigns/search',{params: {query: val}})
.then(function (response) {
self.options = response.data;
console.log(response.data);
});
} else {
this.options = [];
}
}
}
})
Upvotes: 2
Views: 4684
Reputation: 8538
I suggest you read their documentation... Everything you need is there.. you have to add taggable
as well as handle the creations with @tag="handleTagMethod"
CodePen mirror: https://codepen.io/oze4/pen/ROVqZK?editors=1010
Vue.component("multiselect", window.VueMultiselect.default);
new Vue({
el: "#app",
data: {
value: [],
options: []
},
methods: {
addTag(newTag) {
const tag = {
title: newTag,
// you'll need to add other items specific to your objects
};
this.options.push(tag);
this.value.push(tag);
}
},
mounted() {
var self = this;
axios
.get("https://jsonplaceholder.typicode.com/todos?_start=1&_end=10")
.then(response => {
self.options = response.data;
})
.catch(error => {
alert(error);
});
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/vue-multiselect.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/vue-multiselect.min.css">
<div id="app">
<div style="width: 600px">
<label class="typo__label">Simple select / dropdown</label>
<multiselect
v-model="value"
:height="300"
:options="options"
:multiple="true"
:taggable="true"
:close-on-select="false"
:clear-on-select="false"
:preserve-search="true"
tag-placeholder="Add this as new tag"
placeholder="Search or add a tag"
@tag="addTag"
label="title"
track-by="title"
:preselect-first="false"
>
<template slot="selection" slot-scope="{ values, search, isOpen }"><span class="multiselect__single" v-if="values.length && !isOpen">{{ values.length }} options selected</span></template>
</multiselect>
<pre class="language-json"><code>{{ value }}</code></pre>
</div>
</div>
Upvotes: 7