如何为v模型数据对象分配默认值

时间:2018-07-12 11:19:59

标签: vue.js

这里我想为sendqntrecieveqnt设置默认值

    export default{
    data: function(){

        return{

     businessforCustomer:false,
    transactionData:{
       business:{},
       senditems:{
         sendqnt:0,
       },
       recieveitems:{
         recieveqnt:0,
       },
    },
   }
  }

在循环中将那些数据设置为v-model

                      <tr v-for="ri in transactionData.recieveitems">
                        <td>{{ri.item_name}}</td>
                         <td>
                          <input type="text" class="form-control"  placeholder="Item Quantity" v-if="" v-model.number="ri.recieveqnt" >
                         </td>
                      </tr>

                    <tr v-for="si in transactionData.senditems">
                         <td>{{si.item_name}}</td>
                         <td><input type="text" class="form-control"  placeholder="Item Quantity" v-model.number="si.sendqnt">
                         </td>
                     </tr>

提前谢谢。

“设置transactionData.senditems”

axios.get('/api/transactions/getItems')
       .then(function (response){
         app.transactionData.senditems = response.data;
         console.log(response);
       })
       .catch(function (response){
         console.log(response);
       })

1 个答案:

答案 0 :(得分:0)

如果响应不是数组,则将其忽略。如果它是一个数组-如果缺少每个项目的属性receiveqnt,则将其设置为0。

axios.get('/api/transactions/getItems')
       .then(function (response){
         if(Object.prototype.toString.call(response.data) === '[object Array]') app.transactionData.receiveitems = response.data.map((item) =>
         {
           item.recieveqnt = item.recieveqnt || 0;
           return item;
         });
         else app.transactionData.receiveitems = [];
         console.log(response, app.transactionData.receiveitems);
       })
       .catch(function (response){
         console.log(response);
       })