Combining multiple data sources into a single input field in VueJs

I've been exploring the idea of incorporating multiple values into a vue form input binding

Here's an example snippet 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 is currently configured to handle and display a single value for surname, I am contemplating adding another value

asset.current_status.user.last_name
to the v-model inline but struggling to implement it correctly.

Answer №1

To solve this issue, consider implementing a computed property in the following manner:

computed: {
    fullName () {
         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 any other desired outcome...
    }
}

Ensure to set v-model="fullName".

For more information on using computed properties, visit the link.

If editing the input is necessary, you will need to utilize both get() and set(). Refer to this example for guidance.

Best of luck!

Answer №2

<template> 
  <div> 
    <label for=""> Employee Name </label>
    <input class="form-control-plaintext" :value="displayAssignedUser()" type= "text" readonly name="surname">
  </div>
</template>

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

  methods:{
     fetchEmployee(){
        axios.get('/api/employee/'+this.id)
          .then ( response => {
            this.employeeData = response.data.data[0].data; 
            this.fillData = this.employeeData()
            this.employeeForm.fill(this.fillData)
           })
        },

    displayAssignedUser(){
        return `${this.employeeData.current_status.user.surname}` + ' '+ `${this.employeeData.current_status.user.first_name}`
    },

  },

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Omitting specific modules from the Webpack DLL compilation

I have a universal JavaScript application built with Webpack. I've utilized the DLL plugin to pre-build all my node_modules, but recently encountered an issue when adding a new library. This specific library is causing the DLL build process to fail (d ...

Issue with selecting multiple items in DataTables using the shift key

Currently, I am working with datatable 1.10 and have successfully created a table. However, I am unable to enable the multiple "shift select" functionality for its rows. Referring to the official DataTables documentation: The TableTools plugin offers fou ...

Implementing a watcher property in JavaScript to dynamically add a class to an element

I'm currently facing an issue where I need to apply a class to an element when a certain data property changes. My approach involves using a watcher to monitor the value change and adding a class through JavaScript, as demonstrated in the code snippet ...

I have the ability to see HTTP-only cookies within my web browser

So, I always believed that httpOnly cookies could only be accessed during a http request. But the other day, while inspecting Firefox dev tools, I noticed that I could actually view the cookies' values. Is this standard behavior? ...

Utilize JavaScript to append elements to an array in each loop iteration, gradually building a single, unified

I find myself in a situation where an async request is sending data to a child react component. Upon logging the "data" in the component, I receive multiple arrays sequentially. Ideally, I would prefer not to rewrite the request in the parent component and ...

Determine if a specific value is present in an array of objects using AngularJS

var arr = [ { id:1}, {id:2} ]; var obj = {id:1}; var result = arr.indexOf(obj.id) == -1; console.log(result); I am trying to determine if obj.id exists in the array arr[]. Please note: arr[] is an array of objects ...

"Unleashing the power of plugins in your Nuxt.js store: A step

I am facing an issue with my gtag (analytics plugin) where I can access it on my components but not on my store. Any suggestions are welcome. Thank you. plugins/vue-gtag.js import Vue from "vue" import VueGtag from "vue-gtag" export d ...

The principle of event delegation in jQuery

Are event handlers delegated across both <div> tags? In other words, is there one or two event handlers being used? I'm looking to extract the data-id from the event.delegateTarget. It's straightforward when attached to each of the <div& ...

Learn how to establish a state using an array and effectively utilize the setState() method in React

For my latest project, which is API based, I am working with arrays that contain sub-arrays. How can I define a state with an array and utilize the setState() method to work with the entire array? ...

Is the express.json() middleware for parsing JSON request body designed to handle synchronous calls?

According to Express.js's documentation, it is recommended to avoid using synchronous functions as much as possible. This is because in high-traffic websites, the accumulation of synchronous calls can negatively impact the performance of the applicati ...

Utilizing AngularJS: Transforming JSONP information into HTML

I'm relatively new to utilizing $http and fetching data from various websites. My main query is, how can I convert JSONP into HTML? Specifically, when using $http to request the Atari Wikipedia page, the content is displayed with and HTML elements. ...

How can I activate the copy function in jQuery?

I'm trying to figure out how to initiate a copy event using JavaScript or jQuery. I need to be able to simulate the copy event by clicking on a button, but I haven't been able to find a solution yet. I want to avoid using ZeroClipboard or any oth ...

What is the proper way to utilize the ES6 import feature when using a symbolic path to reference the source file?

I am seeking a deeper understanding of the ES6 import function and could use some assistance. The Situation Imagine that I have a portion of code in my application that is frequently used, so I organize all of it into a folder for convenience. Now, in t ...

What is the process of linking current components to Angular?

New to Angular and currently navigating my way through an existing codebase. There is a particular element that already exists in the root document (index.html) before Angular library is loaded. Due to this, the ng-click directive does not get registered. ...

Tips for setting an identification value within mongodb?

Currently, my focus is on utilizing node.js and mongoose. I am in the process of developing a REST API to showcase my User model: var userSchema = new Schema({ _id: {type:Number}, username: {type:String}, age: {type:Number}, genre:{type: Number,ref:&a ...

Reorder a list of objects using buttons for moving items up or down

I'm currently facing a specific issue: I am trying to display a list of items on the frontend based on the 'order' key: <div id="app"> <ul> <li v-for="item in sorted"> {{item.order}} {{item ...

The child component is experiencing issues with receiving props from the father component, even though it is functioning

After successfully passing data from the father component to the child and displaying it in the view, everything seemed to be working fine at first. However, upon checking the console, I noticed that there was an issue occurring, even though the code appea ...

Tips for incorporating an HTML file using ng-include in double curly brace syntax {{ }}

Here is the code snippet I am working with: <div ng-repeat="pTabs in child.productTabs" ng-click="toggleProductTab($index)" ng-if="productTabIsActive(pTabs, $index)"> <div ng-repeat="specs in pTab.additionalSpecs"> <p>{{spec ...

The challenge of transferring documents to the server

There is a form on the webpage where users can select a file and click a button to send it. <form enctype="multipart/form-data"> <input type="file" id="fileInput" /> <button type="button&quo ...

The Javascript eval method throws a ReferenceError when the variable is not defined

In my constructor, I was trying to create a dynamic variable from a string. Everything was working smoothly until I suddenly encountered this error out of nowhere. I didn't make any changes that could potentially disrupt the system, and the variables ...