I am using the Vue-Multiselect plugin for an input drop down box and trying to get the customer's last name when they fill out a form. For some reason I am just getting an object
as the value of the issue.customer_last_name
property (see screenshot below). I want to get a string Doe
, instead.
Anyone have any ideas ? My goal is to eventually submit this data (POST request) via a submit button to a remote api.
Link to codesandbox replicating the issue: https://codesandbox.io/s/vue-template-9z1wd?fontsize=14&module=%2Fsrc%2Fcomponents%2FTest.vue
Full code:
<template>
<div>
<label for="customer_last_name_input">Last Name:</label>
<multiselect
id="customer_last_name_input"
v-model="issue.customer_last_name"
:options="customers"
placeholder="Select an existing customer or type to add a new one"
:custom-label="customerSelectName"
label="lastname"
track-by="uid"
:close-on-select="true"
@select="onSelect"
></multiselect>
<br>
<!-- <input id="customer_last_name_input" type="text" class="form-control" v-model="issue.customer_last_name" placeholder required /> -->
<div>
<label for="customer_first_name_input">First Name:</label>
<input
id="customer_first_name_input"
type="text"
v-model="issue.customer_first_name"
placeholder
required
>
</div>
</div>
</template>
<script>
import Multiselect from "vue-multiselect";
export default {
name: "test",
components: {
Multiselect
},
data() {
return {
customers: [
{
uid: "1",
firstname: "John",
lastname: "Doe",
email: "johndoe@aol.com",
phone: null,
c_organization: "ACME",
c_title: null
},
{
uid: "2",
firstname: "Mary",
lastname: "Smith",
email: "msmith@aol.com",
phone: null,
c_organization: "NBA",
c_title: "Miss"
}
],
issue: {
customer_first_name: "",
customer_last_name: ""
//customer_email: ""
}
};
},
// async created() {
// await this.getCustomers()
// },
methods: {
customerSelectName(option) {
//return `${option.lastname}, ${option.firstname}`
return `${option.lastname}, ${option.firstname}`;
},
onSelect(option) {
console.log(option.lastname);
this.issue.customer_last_name = option.lastname;
this.issue.customer_first_name = option.firstname;
// this.issue.customer_email = option.email
}
}
};
</script>
The problem is the v-model="issue.customer_last_name"
on the multi-select itself. That's setting the selection of the multi-select component to the whole object that the user selects, not just the last name.
Since you're using @select="onSelect"
to do all the updating necessary, just remove the v-model (line 6 in the code sandbox) and you'll be all set.
When you're seeing unhelpful [object Object]
, it can be useful to render that somewhere else, where it's more likely to be fully expanded. It's all somewhat browser dependent, but adding <div>{{issue.customer_last_name}}</div>
was a helpful debugging tool.