【问题标题】:vue multiselect 1.1.4, select value by idvue multiselect 1.1.4,按id选择值
【发布时间】:2017-01-10 06:25:30
【问题描述】:

我添加了看起来像这样的多选组件

查看

<multiselect
  :options="books"
  :selected="selectedBook"
  :show-labels="false"
  placeholder="Choose your book"
  label="name">
  <span slot="noResult">No books were found</span>
</multiselect>

脚本

<script>
  export default {
    data() {
      return {
        books: [],
        selectedBook: null,
      }
    },
    created() {
      this.getBooks();
      this.getFav();
    },
    methods: {
      getBooks() {
        this.$http
        .get('/books/')
        .then(
          function(response) {
            this.books = response.json();
          }
        );
      },
      getFav() {
        this.$http
        .get('/fav/')
        .then(
          function(response) {
            var fav = response.json();
            this.selectedBook = fav.id;
          }
        );
      }
    }
  }
</script>

回应

[{"id":1,"name":"ABC"},{"id":2,"name":"QWE"}]

我的问题是,我如何通过 id 设置选定的书。当我这样设置时,然后在输入中显示 id,但我想要书名。

【问题讨论】:

    标签: vue.js vue-component


    【解决方案1】:

    使用track-by

    <multiselect
      ...
      track-by="id"
      label="name"
      ...
    >
    

    参考:https://vue-multiselect.js.org/#sub-single-select-object

    【讨论】:

    • 嗨,我的options也是从api获取的,但是输入track-by="id"后,显示如下错误:Property or method "id" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property
    猜你喜欢
    • 2019-06-26
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    相关资源
    最近更新 更多