T.A.
T.A.

Reputation: 640

Vue.js How to Access Elements of an Object

I have an array of objects:

data: function() {
        return {
            customers:[],
         }
    },

that populates this select box:

         <label>DSO Affiliation:</label>
                <select  class="select-box form-control" name="customer" id="customer" v-model='customer_id' style="-webkit-appearance: none;">
                     <option value="" selected>Choose Customer</option>
                           <option v-for="customer in customers" :value="customer.id">
                                    {{ customer.customer_name }}
                           </option>
                </select>

Once a customer is selected I need to get the customer data from the selected object so that I can populate other form elements such as:

<label>Customer Address:</label>
<input type="text" class="form-control" name="cust_address" v-model='cust_address'>
                       

I have the data in the customers:[ ] array. How do I get the customer data that was was selected in the select box without an additional trip to the server?

Upvotes: 1

Views: 1455

Answers (1)

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

Watch the customer_id and update cust_address by filtering the customers array :

data: function() {
        return {
            customers:[],
         }
    },
watch:{
   customer_id(val){
     let found=this.customers.find(cust=>cust.id===val);
     this.cust_address=found?found.address:'';
    }
}

Upvotes: 1

Related Questions