Using Vue to store a variable amount of inputs within a dynamic two-dimensional array

My goal is to dynamically create text-fields based on the elements in a multi-dimensional array called response that I receive from the back-end. Each element in the response array contains inner elements like response[0][0] and response[0][1], which are objects with properties such as caption, name, etc. (e.g., response[0][0].name). I want to bind these text-fields to another two-dimensional array named data so that I can retrieve their values and use them however I need. Here's the code snippet:

 <v-layout  row wrap v-for="(row,i) in response" :key = "i">
      <v-layout  v-for="(col,j) in row" :key = "j">
         <v-text-field
          :name = "col.name"
          :label = "col.caption"
          v-model="data[i][j]"//Need to create data[i][j] element first, like data[i] = [] 
          >
         </v-text-field>
      </v-layout>
  </v-layout>

And here is the script section:

data () {
 return {
  data: [],
  response: []
 }
},
 mounted: function () {
   // Retrieve response from the back-end
 }

I'm relatively new to Vue and JavaScript, so any assistance would be greatly appreciated... Feel free to ask for clarification if anything is unclear.

Answer №1

It seems like everything should be working fine... Can you provide more details on the issue at hand? Is it related to generating the response array?

new Vue({
  el: '#app',
  data: {
    data: [
      [{
          name: 'name00',
          caption: 'caption00'
        },
        {
          name: 'name01',
          caption: 'caption01'
        }
      ],
      [{
          name: 'name10',
          caption: 'caption10'
        },
        {
          name: 'name11',
          caption: 'caption11'
        }
      ]
    ],
    response: [
      ['',''],
      ['','']
    ]
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.5/vue.min.js"></script>
<div id="app">
  <div v-for="(i, ii) in data" :key="ii">
    <div v-for="(j, jj) in i" :key="jj">
      {{j.name}}
      
      <input v-model="response[ii][jj]" />
    </div>
  </div>
  <pre>{{response}}</pre>
</div>

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

The disappearance of node_modules results in the failure to install any dependencies for a React application, causing an error to be thrown

I've been struggling to get a react app installed, but every time I try, I encounter the following error and the entire node_modules folder disappears. npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! Found ...

Ways to transfer a variable from JavaScript/jQuery to Java

I am trying to access data retrieved from another server in a Java class using JavaScript/jQuery. The data I receive from the server as a post result to my servlet is in the form of a JSON String. The response looks like this: [{"id":"1","bool_m":"0","na ...

Executing a Ruby method on a server using JavaScript - A guide

I'm facing a seemingly simple issue that I can't seem to find a clear solution for anywhere. Essentially, I have a JavaScript application hosted on a server that requires passing information between the JS app and a Rails-built server. The proces ...

Why isn't my onScroll event triggering in my React.js application? What mistake am I making?

I am facing an issue with my onScroll event in react js. My goal is to implement infinite scrolling in react js, but unfortunately, the onScroll event is not triggering as expected. The process involves fetching posts from an API and passing them to the ...

Implement the use of `require` to update the `$watch` in an

As a new Angular user, I am facing an issue that involves having two directives in my HTML code like this: <parent-dir param="par"> <child-dir></child-dir> </parent-dir> And defining them in my JavaScript code as follows: In t ...

Top recommendation for ensuring that a component is not being displayed

Imagine a scenario where a component returns null in the render method based on a specific prop. How can you effectively use expect to ensure that the component is not rendered? For example: import React from 'react'; import { render, fireEvent ...

Retrieve your username data using Ajax

I'm working on a JSP page with the following structure: https://i.sstatic.net/3aFoz.png In updating the table in my database, I've added a new username column that accepts any string of text. Once the 'update user' button is clicked, ...

React 18 introduces a new feature, ReactDOMClient.createRoot(), which allows for hot module replacement with Webpack. This allows developers to update components in real time without

After upgrading React to version 18, I encountered a console error with my Webpack dev server when the hot module replacement triggers and injects new JavaScript code: Warning: You are calling ReactDOMClient.createRoot() on a container that has already be ...

Search for data in Mongoose by utilizing a query object that has been obtained from a query string

After extracting and parsing a querystring from a URL, I am able to map it into an object structure like this: { '$and': [ { length: { '$gt': '2' } }, { length: { '$lt': '55555' } } ] } This object is st ...

The function of adding data to a list in AngularJS seems to be malfunctioning

I have a JSON list that I am storing in an AngularJS variable called score $scope.jobTemplate = [{ type: "AddInstructions", visible: false, buttonText: "Add Instructions", editableInstructionsList: [{ Number: totalEditableInstruction, Text: "Instruction ...

Execute JavaScript AJAX Request On Page Load

I am facing an issue with creating an onload event in which an AJAX function needs to be called and passed a value. The challenge lies in the fact that I have both my header and footer split into sections via PHP, therefore I cannot place it on the body ta ...

How can I convert JSON data from an array to an array of objects using JavaScript?

My goal is to load data into an ag-grid. I have obtained the json data in the following format: {"my_data":{"labels":[1,2,3,...], "idx":["idx1", "idx2", ...]}} In order to pass it to the grid, I need to transform it to this format: {"my_data":[{"labels" ...

If I only use a data property for determining its existence, will a computed property still rely on it for its calculation?

Here is the Vue component in question: export default { data: function() { return { editor: new Editor({ //some options }), } }, computed: { doc(){ // <--------------------- pay ...

Encountering an issue in REACTJS where the error message "TypeError: navigate.push is not a function"

I'm currently working on setting up the homepage for my react.js website. Everything looks good with my layout and the code compiles successfully. However, when I try to click on a button, I encounter an error on the live website application: TypeErr ...

Move to the first item on the list without any unnecessary scrolling of the view - ReactJS

Whenever I try to scroll to a specific item in the list, clicking on it causes the entire view to scroll instead. Is there a way to ensure that only the div containing the list scrolls? <AlertsList> {productInventoryAlert?.map((prod ...

Limiting the types of files a user can access through a web browser

Hey there! I have a unique question that sets it apart from others, as it involves restricting file types for users with a browser popup. My goal is to prevent users from selecting certain file types before they choose a file. I've come across some s ...

The project is throwing an error: Unable to locate module './lib/md5-hex' in Ember JS

I attempted the following steps: uninstalling md5-hex using npm with the command: npm uninstall md5-hex --save reinstalling md5-hex using npm with the command: npm install md5-hex --save After performing these actions, I ran ember s but unfortunately, i ...

Protractor - Error: prop is undefined

Need assistance in identifying an error. Currently, I am learning Protractor and attempting to create a basic test using Page Object. //login_pageObject.js let loginContainer = function() { this.usernameInput = $("input.login-form-01"); this.passwordInp ...

Creating Conditional Vue Components

I am looking to dynamically create a list of actions, each of which is a component, based on a condition where the object variable store.plan is not empty. I attempted using v-if, which worked well for rendering but not for creating the component. However ...

Store the arrays in a JSON file before utilizing Array.push() to append data into it

I'm currently developing a calendar software and I want to store events in a JSON file. My strategy involves nesting arrays within arrays in the JSON format, allowing for easy iteration and loading during program initialization. My main question is: ...