0

我在这里要做的就是弄清楚如何将响应数据值绑定到我的多选中options,但我不知道我做错了什么。

控制台日志正确显示了我返回的结果,所以我知道它们正在被返回,但我不知道如何将它们与我的多选选项联系起来。

例如,如果我在多选中键入“Day”,我的 axios 调用会执行自动完成功能并获取匹配选项,因此我的控制台会显示:

 0:
    tag_id:  "1001"
    tag_data: "First Day"
 1:
    tag_id:   "1002"
    tag_data: "Second Day"

那么如何将这些返回值放入我的选项中?

  <div id="tagApp">
      <multiselect
      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 = [];
          }
        }
      }
    })
4

2 回答 2

1

由于您使用对象作为选项,因此您需要将labeltrack-by 属性传递给多选组件。在此处查看文档

<multiselect
  v-model="value"
  label="tag_data"
  track-by="tag_id"
  :options="options"
  :loading="loading"
  :multiple="true"
  :taggable="true"
  @search-change="val => read(val)"
></multiselect>
于 2019-04-11T14:52:07.643 回答
1

您需要添加label属性以及track-by属性。在我的示例中,title是我使用的对象的属性 as options,因此您需要使用存在于您使用 as 的数组中的属性名称options

CodePen 镜像:https ://codepen.io/oze4/pen/ROVqZK?editors=1010

Vue.component("multiselect", window.VueMultiselect.default);

new Vue({
  el: "#app",
  data: {
    value: [],
    options: []
  },
  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">
  <label class="typo__label">Simple select / dropdown</label>
  <multiselect 
    v-model="value" 
    :height="300"
    :options="options" 
    :multiple="true" 
    :close-on-select="false" 
    :clear-on-select="false" 
    :preserve-search="true" 
    placeholder="Pick some" 
    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>

于 2019-04-11T15:27:22.343 回答