Attempting to manipulate information within the @click event handler embedded within a v-for loop

I am using a v-for loop to select dialog boxes that I want to open.

<v-card @click="page.model = true">

In this code, page.model is being used as the v-model for a v-dialog component.

  data() {
    return {
      dialog1: false,
      dialog2: false,
      pages: [
        {
          id: "1",
          model: "dialog1",
        },
        {
          id: "2",
          model: "dialog2",
        },
      ],
    };
  },

I'm wondering why @click="page.model = true" does not work, but @click=dialog1 = true " does? I also tried :@click="page.model = true" and @click="${page.model} = true"

Thank you in advance

Answer №1

Based on the provided code snippet, it seems like you have a modal setup where different pages are displayed within modals. Here is how you can achieve this:

<v-card v-for="page in pages" @click="changePage(page.id)">

<modal v-model="pages[0].isOpen">[CONTENT OF PAGE 1]</modal>
<modal v-model="pages[1].isOpen">[CONTENT OF PAGE 2]</modal>
data() {
  return {
    pages: [
      {
        id: "1",
        model: "dialog1",
        isOpen: false,
      },
      {
        id: "2",
        model: "dialog2",
        isOpen: false,
      },
    ],
  },
methods: {
  changePage(id) {
    // Close all other modal pages
    this.pages.forEach(el => el.isOpen = false);

    // Open the selected page
    const index = this.pages.findIndex(el => el.id == id);
    this.pages[index].isOpen = true;
  }
},

Answer №2

There is a method that I have used before which involves calling a specific function and passing the item's index as a parameter. This way, you can access the model associated with that index within the method.

<v-card @click="updateDialog(index)">

updateDialog(i): {
  this.pages[i].model = true
}

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

Steps for removing routing in Angular 2 after setting the folder as a dependency for another project

In my testing Angular project (referred to as project1), I am developing components and utilizing routing for organizational and aesthetic purposes. There is another Angular project (referred to as project2) which includes the component project-project1 i ...

An issue arises when using JSON.parse() with regular expression values

I am encountering an issue with parsing a JSON string encoded with PHP 5.2 json_encode(). Here is the JSON string: {"foo":"\\."} Although this JSON string is valid according to jsonlint.com, when using the native JSON.parse() method in Chrome a ...

Vue.js - finding the route path using its name in Vue.js

Currently, I have a vcard that links to a specific route. In the code snippet below, I have used the route name and passed parameters to it successfully. However, I am facing an issue where I need to use the same link in another location and pass it as a ...

Updating the state of a nested array using React Hooks

After spending some time working with React Hooks, my main struggle has been dealing with arrays. Currently, I am developing a registration form for teams. Each team consists of a list of players (an array of strings). The goal is to allow users to add t ...

What steps are necessary to configure .eslintrc to identify the use of 'require'?

I recently started using ESLint and successfully integrated it with IntelliJ. Initially, ESLint did not recognize node out of the box. After consulting the documentation, I created a configuration file named .eslintrc at the project's root folder, sp ...

Send a triggering function to two separate components

My objective is as follows: render() { return ( <div> <Child onTrigger={xxx} /> <button onClick={xxx} /> </div> ) } Upon clicking the button, I wish for some action to take place in ...

What is the best way to transfer form input data from a child component to the parent component in React?

Recently delving into the world of React, I decided to experiment with forms. My project idea was straightforward - create a form component with input fields that, upon clicking submit, would pass the input values to the parent component ('App') ...

Unknown provider in Angular when using factory inside anonymous function wrapper

I encountered an issue with an unknown provider error when using a factory and declaring it with an anonymous function: (function () { 'use strict'; angular.module('app').factory('errorCodeFactory', errorCodeFactory) ...

Deciphering the LocalDate and nested object array in an AJAX response

Seeking assistance, looking for solutions to two problems. Firstly, how can I display LocalDate in an ajax response? And secondly, how do I iterate over a list of Custom objects received in the ajax response? I am passing a List of Custom Objects and Loca ...

When using Angular 2, the array.splice() function is causing the elements to be removed from the

I am currently working with an HTML table that has default checked rows. <table> <tr> <th></th> <th>Id</th> <th>Name</th> <th>Initial</th> </tr> ...

Divide information in the chart

<table id="tab"> <tr><td class="sub">mmmmm</td><td class="sub">jjjjj</td></tr> <tr><td class="sub">lllll</td><td class="sub">wwwww&l ...

NUXT: Module node:fs not found

Encountering an error when running yarn generate in a Kubernetes container during production. The same command works fine locally and was also functioning properly in production up until last week. Error: Cannot find module 'node:fs' Require stac ...

The best practices for managing item spacing in React or React Native

I am facing some challenges while trying to adjust the spacing of my components. My goal is to have the grid occupy 90% of the screen, with the gear icon taking up the remaining 10% <View style={{ paddingLeft: insets.left, padding ...

Downloading multiple files on both iOS and Android devices

Is there a way to download assets (mp3/jpeg) in an Asynchronous manner? I have over 200 files in each case and the process is taking too long. Are there any techniques to speed up the download process on both iOS and Android? ...

Is the memory efficiency of Object.keys().forEach() in JavaScript lower compared to a basic for...in loop?

Picture a scenario where you have an extremely large JS object filled with millions of key/value pairs, and your task is to loop through each of them. Check out this jsPerf example that demonstrates the different techniques for accomplishing this, highlig ...

What could be causing the malfunction of my Superfish menu in Firefox?

I am currently experimenting with the Superfish jQuery plugin to improve a drop-down menu on my website. Unfortunately, in Firefox browser (v. 21.0), the drop-down menu does not open when hovering over it as expected. However, it works fine in Chrome and O ...

Accessing the path to an imported file in Node.js

Currently, I am working on a parser and encountering imports such as ../modules/greeting.js. Additionally, I have an absolute path to the file from where I performed the import: C:\Desktop\Folder\src\scripts\main.js. I am seeking ...

What is the best way to compare a string with a specific object key in order to retrieve the corresponding value?

I'm looking to achieve a relatively simple task, or at least I think so. My goal is to compare the pathname of a page with key-value pairs in an object. For example: if("pathname" === "key"){return value;} That's all there is to it. But I&apos ...

Vue.js: Can someone provide guidance on retrieving a list of component instances for a slot?

Looking to access the vue instances of a group of slot components, specifically needing access to the key or id of the slot components in order to preserve their state during the beforeUpdate and updated hooks. The component template is structured as foll ...

Getting into a slot within a nested component within the render function

Currently using Vue 3 and I have a component with a render() function structured like this: render() { return ( <my-component> <div ref="cont" slot="cont">content</div> <my-component> ) } In addition, w ...