Generating Placeholder Variables Within a v-for Iteration

Encountering a problem with Vue-JS involving v-for loops and v-model properties.

I have an array of items:

<input
  v-for="i in list"
  v-model="i.model"
  ... (other tags that use i)
>
</input>

Accompanied by some JavaScript:

let model_thing = ref();

let list = [
  { model: ref(), data: "Whatever things" }
  { model: model_thing, data: "..." }, //Tried placing the reference variable outside the list as well
]

The issue lies in the fact that the v-model isn't functioning properly, likely due to the reference variable being within an array.

A potential solution involves creating a dummy variable inside the v-for loop:

<input
  v-for="i in list"
  v-model="let dummy = ref(); /* use 'dummy' as the ref"
  ... (whatever else)
>
</input>

Utilizing Maz-UI, an undocumented UI framework, complicates finding a quick fix, like using select dropdowns instead of inputs:

<!-- Using select dropdowns instead -->
<MazSelect
  v-for="i in list"
  :value="i.model"
  @input="i.model = $event.target.value"
>
</MazSelect>

Unfortunately, this results in displaying [object, Object] and placeholder text still appearing.

Looking for guidance on how to create such dummy variables?

Answer №1

As noted in the feedback, there is no issue with how you are utilizing v-model. My suggestion would be to try encapsulating it within a reactive for it to potentially work:

let model_thing = ref();

let list = reactive([
  { model: ref(), data: "Various things" },
  { model: model_thing, data: "..." }, // I attempted something similar by creating ref() outside of the list
])

It's important to understand that because of how reactive functions, you could eliminate intermediate refs as well:

let list = reactive([
  { model: undefined, data: "Various things" },
  { model: undefined, data: "..." }, // I also tried creating this structure where ref() is placed outside of the list
])

Creating temporary variables is typically discouraged. Why? They lack reactivity, causing only part of the v-model functionality to be effective: the binding aspect.

Additionally, the approach you envisioned is not feasible since Vue's JS compiler for binds exclusively permits expressions and not statements.

You can utilize literals, function calls, etc., but you cannot generate a conventional variable dynamically.

For these reasons, it seems unlikely that you can create refs on the go. In instances of such complexity, it may signal a need for refactoring or dividing the problem into smaller components.

Your query prompts an interesting discussion on data modeling: vertical (multiple arrays) versus horizontal grouping (single array of objects).

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

Managing the hovering of a mouse over an image within an isometric grid displayed on a

I have implemented an isometric grid in HTML canvas. My goal is to handle mouse hover events on the buildings within the grid. Some buildings will vary in heights. In the image below, you can see that when hovering over a tile, the highlighted area shif ...

Having trouble aligning material-ui GridList in the center

I am currently working with a GridList in order to display Cards. The styling for these components is set up as shown below: card.js const useStyles = makeStyles({ card: { maxWidth: 240, margin: 10 }, media: { heigh ...

"The FindByIdAndUpdate function is successfully updating the data, but it is unexpectedly

This is my first time seeking guidance here, as I've reached a dead end with my issue. I am currently working on a household collection that includes a member collection within it. Whenever new members join, I need to update the household collection ...

Utilizing PHP and jQuery to dynamically populate Google Maps with multiple markers

I am currently working on integrating a Google map with a database to dynamically display multiple markers using PHP and MySQL. Below is the code I have been using: <?php //This section retrieves data from the database for later use in jQuery //CREATE ...

The toggle class feature of jQuery is malfunctioning when placed inside a different div

Hello everyone, I am currently working on a toggle effect on my webpage. However, I encountered an error when trying to move the button close to another part of the page. The button works fine if it is placed in one part of the HTML, but does not work if i ...

What is causing the return statement to not function properly in the Mongoose findOne method?

I am attempting to locate an item by its name, then update the table and return the updated result. However, the return statement is not working as expected in my code: class addSongsToArtist { constructor(artistName) { Artist.findOne({ ...

What is the best way to add a URL dynamically when a click event occurs?

Here is the code where I am making an API call: export const filteredProducts = (e) => { const radio = e.target.checked; return dispatch => { dispatch({type: actionTypes.TOGGLE_LOAD}); axios.get(radio ? `/store?limit=9&skip=0&subc ...

When utilizing multer for handling multipart data, hasOwnProperty appears to become undefined

Below is the code snippet I am currently working with: var express = require('express'); var mongoose = require('mongoose'); var bodyParser = require('body-parser'); var multer = require('multer'); var user = requir ...

Can you show me the method to retrieve the value of client.query in Node JS using PG?

I have been working with node.js to establish a database connection with postgresql. Here is what my dbConfig.js file looks like: var pg = require('pg'); var client = new pg.Client({ host:'myhoost', port:'5432', ...

Implementing the requiredUnless validator of vuelidate for checkboxes: A step-by-step guide

When utilizing Vuelidate, the 'required' validator now accepts boolean 'false' as a valid value. To enforce required validation for checkboxes, you must use 'sameAs' such as sameAs: sameAs( () => true ). How can 'requi ...

Effortlessly move and rearrange various rows within an HTML table by utilizing JQuery alongside the <thead> elements

My JsFiddle has two tables and I want to switch rows <tr> between them using Jquery. However, due to the presence of the <thead> tag, it is not functioning properly. I suspect that the issue lies with the items in the selector, but I am unsure ...

Navigating through objects within arrays within objects in Angular

I seem to be encountering some difficulty in displaying data from an API call on Mapbox. Only one marker is showing up on the map instead of the expected 10 markers. I suspect there might be an issue with my ng-repeat implementation, but I can't pinpo ...

Creating Flutter UI designs that seamlessly adapt to all screen sizes without the need for scrolling

Just a quick question as I embark on creating my very first Flutter App. Currently, I am working on the Login Screen, which features a simple design with a welcoming text and two rows for Google and Apple sign-in options. I want to avoid making the page s ...

Switch up the CSS file based on the URL route

My project consists of the following files: App.vue, changcolor.vue, config.json, main.js, index.html, xyz.css, abc.css. I need a solution where based on the URL, the appropriate CSS file is applied. For instance, if the URL is "xyz.local.com" then xyz.cs ...

Exploring the Depackaging of ES6 Nested Objects

How can I implement ES6 with Destructuring to give users options? I'm having trouble dealing with nested objects and preventing the defaults from being overwritten by partial objects. Check out this simple example on MDN: function drawES6Chart({si ...

Is it possible to toggle all parent targets in Bootstrap?

When trying to showcase my point, I believe it is best demonstrated by visiting Bootstrap documentation at https://getbootstrap.com/docs/4.0/components/collapse/ and viewing the "multiple targets section." In this section, you will find three buttons: togg ...

Tips for capturing a screenshot of the ESRI Map using Angular

Is there a way to capture a screenshot of the Esri map in its current state on the UI and then convert it into a PDF for download using Angular? Below is my current .ts code, but I am open to any additional suggestions. esri-map.component.html <!-- Map ...

@casl/vue: What is the recommended structure for an ability.js file?

I'm currently working on integrating @casl/vue with Vue 3, and it seems like I'm encountering some issues. Following the provided instructions, I've included the following code in my /main.js: import { abilitiesPlugin } from '@casl/vue ...

Ways to obtain the output of an If/Else statement

It seems like I might be missing something, but I am unsure of how to extract the result from an else-if statement. Take this code snippet that I've been working on for example: In this scenario, the output would read "It's warm!", and what I wa ...

Controlling the Vuetify Navigation drawer from a separate component

Within my App.vue component, I have a Navigation Drawer menu containing main items and some subitems. These subitems are meant to be displayed only after a specific event, such as a button click on the child component's page (when the variable totalRe ...