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