Dynamic v-model for a group in Vue2

I am currently working on a housing form and I have encountered a roadblock that I need assistance with. Essentially, when a user selects that their house has 2 floors in the form, they are then prompted to input the number of rooms, toilets, balconies, etc. for each floor.

The challenge I am facing is how to effectively "group" these responses so that I can send them to the backend and interpret the answers correctly.

Here is what I have attempted thus far:

data() {
  return {
    flooroptions: [
      {
        name: 'Rooms',
        id: '1',
      },
      {
        name: 'Balcony',
        id: '2',
      },
      {
        name: 'Toilets',
        id: '3',
      },
    ],
    floors: '',
    floor1: [],
  },
}

By looping through like this:

<div class="" v-for="(opt, index) in flooroptions">
  <input type="number" name="" value="" v-model="floor1[index]">
</div>

I am able to view my data using floor1:

{{ floor1 }}

However, if a user only inputs that the first floor has 2 rooms without adding any other information, I only receive 2 within floor1.

What would be the most effective approach to tackle this issue?

PS. Each floor will go through this flooroptions loop allowing users to input the number of each option for every floor, and my goal is to accurately interpret those numbers.

Answer №1

If my understanding is correct, then I would...

data() {
  return {
    roomOptions: [
    {
      name: 'Bedrooms',
      id: '1',
    },
    {
      name: 'Bathrooms',
      id: '2',
    },
    {
      name: 'Kitchen',
      id: '3',
    },
    ],
    numRooms: 2,
    roomData: [
      {'Bedrooms': 0, 'Bathrooms': 0, 'Kitchen': 2},
      {'Bedrooms': 2, 'Bathrooms': 1, 'Kitchen': 0}
    ],
  },
}

and iterate through the options like this:

<div v-for="r in numRooms">
  <div class="" v-for="opt in roomOptions">
    <input type="number" name="" value="" v-model="roomData[r][opt.name]">
  </div>
</div>

or... if you prefer to pass minimal data and know the ids on the backend

data() {
  return {
    roomOptions: {
      1: {
        name: 'Bedrooms'
      },
      2: {
        name: 'Bathrooms'
      },
      3: {
        name: 'Kitchen'
      },
    },
    numRooms: 2,
    roomData: [
      {1: 0, 2: 0, 3: 2},
      {1: 2, 2: 1, 3: 0}
    ],
  },
}

and loop through like this:

<div v-for="r in numRooms">
  <div class="" v-for="(opt, i) in roomOptions">
    <input type="number" name="" value="" v-model="roomData[r][i]">
  </div>
</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

What could be causing the JSON String decode error to occur while utilizing extjs ajax?

Here is an example of my code: Ext.Ajax.request({ url:'test.jsp', params:{id:id,password:password}, success:function(response){ console.log(response); var results = Ext.util.J ...

Tips for creating line breaks in Google Chart tooltips

I'm having trouble breaking a line in the code snippet below. Here is the complete code: <html> <head> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script ...

Best practices for consuming streams using jQuery

Looking to extract VU meter data from my shoutcast server in a continuous live stream format with the following structure: "0xa5 leftVal rightVal 0xa5 leftVal ..... etc" My goal is to capture and decipher this data using JavaScript (jQuery) for animated ...

Exploring the capabilities of nested WebGL render targets in the three.js library

I am currently developing an application for the Oculus Rift using JavaScript, three.js, and OculusRiftEffect.js. In order to create a transparent portion of a 2D ring for the menu, I am attempting to generate a MeshBasicMaterial.alphaMap texture on a Web ...

Using `require` can be successful even if `qs` is not defined

I have a project running on NodeJS 14 within Google Cloud Functions. I am utilizing typescript and the tsc command to compile my code. import qs from 'qs' console.log(`qs >>> ${qs}) The output of the code snippet above (when in product ...

How can changes be spread to other stores within the react flux flow?

My project has a simple structure consisting of 1 store, 2 components (parent and child, with the parent acting as a controller), actions, and constants. Currently, I am not making any API calls; all data is passed to the parent component as props. The fl ...

Calculate the number of rows in a table that contain identical values

I have a puzzling issue that has been on my mind. I currently have an SQL statement that selects specific rows from my database and displays them in an HTML table, which is functioning properly. However, I now need to determine how many rows have the same ...

Troubleshooting EasyTabs: Localhost Issue with Ajax Tab Configurations

I've implemented EasyTabs for organizing my tabs on a webpage. I decided to use ajax-tabs functionality in order to fetch content from other pages when users click on the navigation menu buttons. However, I've encountered an issue where the conte ...

Troubleshooting Vue.js unit test issues caused by log messages from internal components

I am working with a component structured like this: <template> <p>{{ message }}</p> <internal-comp></internal-comp> </template> <script> import InternalComp from './components/InternalComp.vue'; ...

What is the best way to incorporate Vue.js component dependencies into a multi-page application (MPA

When working with a multiple page app, incorporating a component inside another component can be challenging, especially without using a build system. $ npm install sagalbot/vue-select <template> <div id="myApp"> <v-select :value.sy ...

Is there a way to convert various elements sharing the same class into a list of array items?

Essentially, I am dealing with multiple elements sharing the same class name. My goal is to retrieve an array of integers from an API and then iterate through each element with this class name, replacing them with elements from the array sequentially. For ...

Reducing the size of a Vue project

I am currently using Vue version 2.6.10 This project was set up using Vue CLI 3, which can be found at https://cli.vuejs.org/guide/ The Vue CLI includes webpack 4 under the hood I am looking to minimize (remove all whitespaces, newlines, etc.) the source ...

How can we track and record NaN values in JavaScript/TypeScript as they occur in real-time?

Is there a reliable method to identify and prevent NaN values during runtime, throughout all areas of the application where they might arise? A) Are there effective linting tools available to alert about possible occurrences of NaN values within specific ...

Using Angular 4 with Bootstrap dropdowns necessitates the use of Popper.js

I recently created an Angular 4 CLI app. After executing the following command: npm install <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="066469697275727467764632283628362b64637267">[email protected]</a> jquery ...

Calculating the time gap between two consecutive "keyup" occurrences

I am in need of creating a basic point of sale system using Javascript. I have a barcode scanner that functions like a keyboard. My goal is to automatically identify when the input is coming from the barcode scanner and then generate a report with the sc ...

Extracting the name from a JSON key/value pair and then adding it to a TextField

Here is a sample JSON data: { "Id": 1, "Title": "Information on Jane Smith", "Comments": "Additional details here", "UpdatedBy": "Jane Smith", "UpdateDate": "May ...

Command to conceal components for users visiting as guests

I'm looking to develop a directive that hides specific elements for guest users. Here is the current code I have: angular.module('someMod') .directive('premiumUser', premiumUser) .controller('PremiumUserCtrl', Pr ...

Ways to designate ROLES within the _user database on Cloudant

I am struggling to figure out how to add Roles to users in the Cloudant user database (_users). Despite searching through Google and Cloudant Docs, I have not been able to find a solution. There is mention of a Cloudant _user db, but I can't seem to u ...

Use the column name instead of the index with Jquery's eq() function

Looking for a way to use a static column name in jQuery to get the 5th column text in a table and change the text of a textbox $('textbox').val($(e.target).closest("tr").find('td:eq(4)').text()); If the index of the columns is changed ...

sending a parameter in the reverse url using JavaScript

coding in javascript let address = '{% url candidate_resume "cnd_id" %}'; address = address.replace("cnd_id",id); document.getElementById('cell2').innerHTML= '<a href="' + address + '"> View Resume < ...