简体   繁体   中英

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?

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:'';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM