Vue多重选择不允许创建新标签

时间:2019-04-11 15:17:32

标签: javascript vue.js axios multi-select

我在Vue上有一个工作最多的多重选择,在这里我通过对数据库的axios调用使用自动完成功能。就返回数据库结果并将其添加为选项,然后添加标签来说,它是完美的。

我的问题是我无法创建新标签。因此,如果我的axios调用返回“正在测试”作为选项,而我选择了它,它将成为标签。但是,如果在键入“新测试”并按Enter键时没有返回任何结果,则它不会成为新标记。

我做错了什么?

<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 = [];
    }
  }
}
})

1 个答案:

答案 0 :(得分:1)

我建议您阅读their documentation ...在那里,您需要的所有内容..您必须添加taggable并使用@tag="handleTagMethod"处理创建的内容

CodePen镜像: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/vue-multiselect@2.1.0/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/vue-multiselect@2.1.0/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 &amp;&amp; !isOpen">{{ values.length }} options selected</span></template>
  </multiselect>
  <pre class="language-json"><code>{{ value }}</code></pre>
  </div>
</div>