Search code examples
javascriptvue.jsmodel-binding

Binding two or more data to a form input in VueJs


I was considering having more than one value in a vue form input binding

Below is a sample of my code.

<template> 
  <div> 
    <label for=""> Employee </label>
    <input class="form-control-plaintext" v-model="asset.current_status.user.surname" type= "text" readonly name="surname">
  </div>
</template>

<script> 
  data(){
    return:{
      asset:{},
    }
  },

  methods:{
     getAsset(){
        axios.get('/api/assets/'+this.id)
          .then ( response => {
            this.asset = response.data.data[0].data; 
            this.toFill = this.asset()
            this.assetForm.fill(this.toFill)
           })
        },
  },

   mounted(){
        this.getAsset();
    }
</script>

The v-model currently accepts and displays one value of surname, I believe it possible to include another value asset.current_status.user.last_name to the v-model inline but can't currently get things right.


Solution

  • <template> 
      <div> 
        <label for=""> Employee </label>
        <input class="form-control-plaintext" :value="showAssignedUser()" type= "text" readonly name="surname">
      </div>
    </template>
    
    <script> 
      data(){
        return:{
          asset:{},
        }
      },
    
      methods:{
         getAsset(){
            axios.get('/api/assets/'+this.id)
              .then ( response => {
                this.asset = response.data.data[0].data; 
                this.toFill = this.asset()
                this.assetForm.fill(this.toFill)
               })
            },
    
        showAssignedUser(){
            return `${this.asset.current_status.user.surname}` + ' '+ `${this.asset.current_status.user.first_name}`
        },
    
      },
    
      mounted(){
         this.getAsset();
     }
    </script>