mamaye
mamaye

Reputation: 1064

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.

Upvotes: 2

Views: 1086

Answers (2)

mare96
mare96

Reputation: 3869

I think you need computed property like this:

computed: {
    surname () {
         if (this.asset.current_status.user.last_name 
             && this.asset.current_status.user.surname) {
       
         return this.asset.current_status.user.last_name 
                + this.asset.current_status.user.surname
         }

         return '' // or whatewer else you need...
    }
}

And set v-model="surname".

Read more about computed.

Also if you need to edit that input you will need get() and set(). You can check this example for that.

Good luck!

Upvotes: 2

mamaye
mamaye

Reputation: 1064

<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>

Upvotes: 0

Related Questions