vue multiselect 1.1.4, select value by id

℡╲_俬逩灬. 提交于 2021-02-19 06:38:04

问题


i added multiselect component which looks like this

View

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

Script

<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>

Response

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

And my question is, how can i set selected book by id. when i set like this, then in input shows id, but i want the book name.


回答1:


Use track-by

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

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



来源:https://stackoverflow.com/questions/41562404/vue-multiselect-1-1-4-select-value-by-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!