1
votes

New to using Vue-Multiselect. I am using axios to do a GET request from a JSON placeholder to test.

How do I get the title and post id to show up in my drop down?

Right now, I just get [Object Object] - [title] shown in my select box.

enter image description here

    <!-- Vue component -->
<template>
  <div>
    <multiselect v-model='value' :options='posts' :custom-label='postWithTitle' placeholder='Select one' label='title' track-by='id'></multiselect>
    {{ value  }}
  </div>
</template>

<script>
import Multiselect from "vue-multiselect";
import axios from "axios";

export default {
  // OR register locally
  components: { Multiselect },
  data() {
    return {
      value: null,
      posts: []
    };
  },
  created() {
    this.getPosts();
  },
  methods: {
    getPosts() {
      axios
        .get("https://jsonplaceholder.typicode.com/posts")
        .then(response => {
          // eslint-disable-next-line
          console.log(response);
          this.posts = response.data;
        })
        .catch(error => {
          // eslint-disable-next-line
          console.log(error);
        });
    },
    postWithTitle(id, title) {
      return `${id} - [${title}]`;
    }
  }
};
</script>
1

1 Answers

0
votes

fix:

  postWithTitle(option) {
     return `${option.id} - [${option.title}]`;
  }

explaination:

i saw that when i simply console.logged inside the postWithTitle function:

the custom custom-label attribute was accepting a callback that only accepts one argument. that argument was the entire option object- a single entry of your posts array.