What is the most effective method for utilizing v-model with a pre-populated form in Vue.js?

Need some help with a form and looping through items in a module to generate textfields. Take a look at the photo for context:

Link: https://i.sstatic.net/MPAVq.jpg

Currently, I'm using a structure like this...

                <v-row class="d-block d-md-flex">
                    <v-col v-for="planning in module.plannings" :key="planning.id">
                        <v-textarea
                            outlined
                            hide-details
                            dense
                            :label="planning.name"
                        ></v-textarea>
                    </v-col>
                </v-row>

This is how the "module" structure looks like:

created_at:"2022-01-31T15:56:06.000000Z"
deleted_at:null
.....
plannings:[
  {
    "id": 76,
    "performance_management_set_module_id": 103,
    "name": "Performance Criteria",
    "ord": 1,
    "updated_at": "2022-01-31T15:56:11.000000Z",
    "created_at": "2022-01-27T11:18:46.000000Z"
  },
  {
    "id": 77,
    "performance_management_set_module_id": 103,
    "name": "Evidence Collection",
    "ord": 2,
    "updated_at": "2022-01-27T11:18:51.000000Z",
    "created_at": "2022-01-27T11:18:51.000000Z"
  },
  {
    "id": 78,
    "performance_management_set_module_id": 103,
    "name": "Support & Training",
    "ord": 3,
    "updated_at": "2022-01-27T11:19:01.000000Z",
    "created_at": "2022-01-27T11:19:01.000000Z"
  }
]
....
updated_at:"2022-01-31T15:56:06.000000Z"

When users fill out the form, we retrieve data from the backend:

{
  "76": {
    "id": 1,
    "performance_management_set_module_planning_id": 76,
    "user_id": 2,
    "text": "this is a test 1",
    "created_at": "2022-01-31T15:25:31.000000Z",
    "updated_at": "2022-01-31T15:25:31.000000Z"
  },
  "77": {
    "id": 2,
    "performance_management_set_module_planning_id": 77,
    "user_id": 2,
    "text": "this is a test 2",
    "created_at": "2022-01-31T15:25:31.000000Z",
    "updated_at": "2022-01-31T15:25:31.000000Z"
  }
}

Looking for advice on the best way to v-model each entry to the v-textfield:

  • Tried using keys as parent IDs for v-model but faced issues...

  • Also experimented with binding :value to a function:

bindToItem(id){
      
                if(this.plannings){
                    if(this.plannings.find(i => i.performance_management_set_module_planning_id === id)) {
                        return this.plannings.find(i => i.performance_management_set_module_planning_id === id).text;
                    }
            },

Any simpler approach that I might be missing?

Answer №1

Have you had success with utilizing indexes?

<v-row class="d-block d-md-flex">
    <v-col v-for="(planning, index) in module.plannings" :key="planning.id">
        <v-textarea 
           outlined hide-details dense
           :label="planning.name"
           @input='(evt) => { updateItem(index, evt.target.value) }' 
           :value="planning.text"></v-textarea>
    </v-col>
</v-row>
//
...
methods: {
    updateItem(index, value) { this.module.plannings[index].text = value; }
}

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

Issue with Material-ui autocomplete not updating its displayed value according to the value prop

My task involved creating multiple rows, each containing a searchable Autocomplete dropdown populated through an API, along with other fields. Everything was functioning perfectly until I encountered an issue when deleting a row from the middle. After dele ...

Best Practices for Organizing Image and JavaScript Files in Your Project

Currently, I am working on a project in Eclipse and I'm unsure about the proper location to store my image files and JavaScript files. Can anyone provide guidance on this matter? Thank you. ...

Creating a shared observable array in KnockoutJs to be used for multiple select elements

When an employer needs to assign a specific employee and premium amount, they can click on the "Add Employee" button to reveal another form with a dropdown menu of employees and an input field for the premium amount. <select> <option>John& ...

Accessing JavaScript variables within Selenium

Currently utilizing Selenium Webdriver 2.0 to test an HTML page that incorporates an x.js file. Within x.js, the variable "price" is defined based on data entered from the HTML form and various Javascript calculations. In the HTML, the correct price valu ...

How to trigger a force reload on a VueJS route with a different query parameter?

Is there a method to refresh the page component when two pages utilize the same Component? I have encountered an issue where the router does not reload and the previous edit values persist. { path: "/products/new", component: ProductPage, meta: { ...

Building a ReactJS application that displays an array of images with a distinct pop-up feature for each image

Having trouble creating unique popups for each image in React. I have a set of 20 images that should be clickable, with only one popup open at a time, each containing text and an image. Currently mapping out the images from an array. Looking for assistan ...

`Misconceptions in understanding AngularJS directives`

I am currently working on a new app that includes both a header and main view, utilizing directives. At first, I had the directive in its own file but decided to relocate it into app.js in order to resolve an issue. Throughout this process, I have experim ...

Utilizing Polymer 3 in a different context such as ASP.NET MVC allows for the development of versatile components known as PolymerElements that can be reused

I am currently working on integrating Polymer 3 components into my ASP.NET MVC application. I'm not entirely sure if my approach to this issue is correct at the moment. My main goal is to execute everything from IIS Express. However, I'm encou ...

Transform Ajax response into dropdown menu option

I have made an ajax call and received HTML as a response. Now, I need to convert this output into options and add them to select tags on my webpage. <div class="views-element-container"> <div class="view view-contact-view-id-conta ...

Tips for rearranging sibling divs while maintaining the order of their child elements

Is there a way to shuffle the order of div classes shuffledv, while maintaining the same order of id's each time the page is refreshed? <div class="shuffledv"> <div id="2"></div> <div id="3"></div> <div id="1">< ...

Adjusting the input in a Textfield within React using Material UI

const [formData, setFormData] = useState({}); useEffect(() => { fetch("/formdata") .then((res) => res.json()) .then((data) => setFormData(data)); }, []); console.log("Form Data", formData); //Sorting by order let attr; ...

Fixed-positioned elements

I'm facing a small issue with HTML5 that I can't seem to figure out. Currently, I have a header image followed by a menu div containing a nav element directly below it. My goal is to make the menu div stay fixed when scrolling down while keeping ...

I am requesting for the average to be adjusted based on the user's choice between Hindi or English percentage scale

Currently, the average is being calculated in the HTML code. When a user input is changed, the average breaks. What can be done to update the average for each individual person? Controller var app = angular.module('myApp', []); app.controller( ...

issue with the emit() and on() functions

While diving into my study of nodejs, I encountered a puzzling issue where emit() and on() were not recognized as functions. Let's take a look at my emitter.js file function Emitter(){ this.events = {}; } Emitter.prototype.on = function(ty ...

Vuejs - Enhancing User Experience with Multiple Unique Loading Indicators for Various States

I am in search of a spinner like the one showcased in this tutorial on Adding Loading Indicators to Your Vue.js Application. However, I need this spinner to function differently for various elements and states. For instance, when the first image is being ...

Updating state within a loop using Quasar (VueX)

I am currently working on a Quasar application and utilizing VueX to manage the state of my app. However, I am encountering an issue when trying to update properties within a for loop. Despite inputting values, they do not seem to be getting set in the st ...

Ways to guarantee a distinct identifier for every object that derives from a prototype in JavaScript

My JavaScript constructor looks like this: var BaseThing = function() { this.id = generateGuid(); } When a new BaseThing is created, the ID is unique each time. var thingOne = new BaseThing(); var thingTwo = new BaseThing(); console.log(thingOne.id == ...

Utilizing middleware with express in the proper manner

Hello there! I'm just double-checking to see if I am using the correct method for implementing middleware in my simple express app. Specifically, I am trying to ensure that the email entered during registration is unique. Here's an example of wha ...

How to completely disable a DIV using Javascript/JQuery

Displayed below is a DIV containing various controls: <div id="buttonrow" class="buttonrow"> <div class="Add" title="Add"> <div class="AddButton"> <input id="images" name="images" title="Add Photos" ...

Arranging elements within an outer array by the contents of their inner arrays

I need help organizing an array based on the alphabetical order of a specific value within the inner arrays. For example: I want to sort this array by the prefix "old," so old A, old B, etc. const array = [ { personName: "Vans", personTags: ["young", " ...