Setting individual state variables for each child component within a Vue loop

When I look at the code snippet below, I find myself facing an issue with state usage.

<div class="featured-card-container">
        <swiper class="swiper" :options="swiperOption">
            <template v-for="(strategy, index) in strategiesCards">
                <swiper-slide :key="'slider-' + index">
                    <strategy-card
                        :data="strategy"
                        @click.native="showModalStrategies()"
                    />
                </swiper-slide>
                <strategies-modal
                    :key="'modal-' + index"
                    :dialog.sync="modalStrategies"
                    :data="strategy"
                />
            </template>
            <div class="swiper-pagination" slot="pagination"></div>
        </swiper>
    </div>

The main challenge here is triggering a modal from its corresponding card. Figuring out how to accomplish this effectively has been quite daunting for me.

The current strategy implemented in the above code seems to be causing issues because the data state modalStrategies is shared across multiple components. This results in all existing modals being quickly changed even if only one card is clicked.

I'm seeking guidance on how to resolve this dilemma. Are there any workarounds or solutions that could address this issue effectively?

Thank you to everyone who takes the time to understand and provide input on my situation. Your efforts are truly appreciated.

Answer №1

<template v-for="(plan, number) in plansList">
    <swiper-slide :key="'slide-' + number">
        <plan-card
            :data="plan"
            @click.native="displayModalPlan(plan)"
        />
    </swiper-slide>
    <plan-modal-dialog
        :key="'dialog-' + number"
        :dialog.sync="isShowDialog"
        :data="plan"
    />
</template>
data() {
    return {
        modalPlan: null,
        currentPlan: null
    }
},
computed: {
    isShowDialog: {
        get() {
            return this.modalPlan === this.currentPlan
        },
        // https://vuejs.org/v2/guide/components-custom-events.html#sync-Modifier
        set(value) {
            if(!value) {
                this.modalPlan = null
            }
        }
    }
},
methods: {
    displayModalPlan(plan) {
        this.currentPlan= plan
        this.modalPlan = plan
    }
}

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

Utilizing the jQuery index for organizing JSON keys

Recently, I came across an interesting issue with a jQuery event handler in my code. When clicked, this specific event creates a JavaScript object based on a list of favorite links: $('#saveFavorites').on('click', function(){ a ...

The task "default" is not found within your current gulpfile configuration

After running gulp in my console, I encountered the following error: Task 'default' is not in your gulpfile I double-checked my gulpfile and it appears to be correct: var gulp = require('gulp'), LiveServer = require('gulp- ...

Why does Vuetify/Javascript keep throwing a ReferenceError stating that the variable is undefined?

I'm currently developing in Vuetify and I want to incorporate a javascript client for Prometheus to fetch data for my application. You can find the page Here. Despite following their example, I keep encountering a ReferenceError: Prometheus is not def ...

Enhancing AG-Grid with live data updates post drag and drop interaction

Exploring AG-Grid for the first time within my Vue application has been challenging. I am currently facing an issue where the data does not update after a drag&drop event. To illustrate this problem, I have created a small example on Plunker import Vue ...

Enable a VueJS directive on-the-fly from a separate directive

My goal is to simplify the process of binding a form control to vuejs by creating a directive that handles all necessary events for tracking changes in a form field. Rather than manually adding multiple event listeners like this: <input type="text" na ...

Display a div with a link button when hovering over an image

Currently, I'm attempting to display a link button within a div that will redirect the user to a specified link upon clicking. Unfortunately, my current implementation is not functioning as expected. I have provided the code below for reference - plea ...

Ember controller failing to update template upon property set within promise execution

I am facing an issue while integrating user login functionality in my application. After retrieving the user data from the server, I aim to display the user's name on the page once the process is completed. The login form appears as a popup window, in ...

An error with jQuery occurred in the client's post response, resulting in a 400 POST HTTP/1.1 error

I am struggling to identify the issue with my code, especially since I'm not very familiar with jQuery. The goal is to create an HTML form for storing car data based on this template: https://i.sstatic.net/u40nG.gif The source code for the HTML form ...

Encountering intellisense problems while declaring or utilizing TypeScript types/interfaces within Vue.js Single File Component (SFC) documents

For my Nuxt 3 project, I am developing a component and attempting to declare an interface for the component props to ensure strong typings. Here is an example: <script setup> interface Props { float: number; } const props = defineProps<Props> ...

How can I extract just the initial 2 letters of a country name using AmCharts maps?

Having trouble with Amcharts maps. I have a map that displays countries as United States, but I only want them to show as US. Is there a country formatter available for this issue? Any help is appreciated. ...

Obtaining undefined values for req and resolvedUrl in GetServerSideProps function

In my project, I am currently using next.js version ""next": "^12.1.4"" and node version ""@types/node": "^14.14.6". I have created a function called getServerSideProps with parameters req and resolvedUrl. When the ...

selenium tutorial: Testing tooltip functionality with Python

<div class="icon_png information icon_baggageyes" title="getTooltip()"></div> Here, a tooltip is displayed using the getTooltip() function. Is there a method to retrieve the return value of the function for testing with Selenium? ...

What is the best way to trigger the ajax request with the same post parameter upon pressing the browser's back button?

Is there a way to remember the post parameters and resend an AJAX request when the browser back button is pressed? I have searched online and found a couple of methods: Using localsotrage or document.location.hash when the page unloads. Using cookie ...

Tips for restricting camera movement in threejs

Being new to working with threejs, I am struggling to set limits on the camera position within my scene. When using OrbitControls, I noticed that there are no restrictions on how far I can zoom in or out, which I would like to change. Ideally, I want the c ...

Showing the initials of a user at the top of an SVG using ReactJS

https://i.sstatic.net/oJgXs.png I require the user's initials to be displayed on the avatars as a grey circle with those initials. I have the function ready, but I am unsure of how to implement it in the JSX code for the Dropdown menu (using Semantic ...

Firestore query is not displaying the expected data, resulting in an empty array being returned

I've encountered an issue where my query is returning an empty array. Despite double-checking for errors and typos in the query, no error messages are popping up. This problem arose while working on a learning project inspired by Firehip's NextJS ...

Tips for efficiently transferring a retrieved object from App.js to a child component in ReactJS version 16 and above using asynchronous methods

TL;DR How can I pass a fetched object from App.js to a child component asynchronously? Do I need to wait for the data to be completely fetched before returning App.js? If so, how can I achieve this? In an attempt to create a dashboard using react-chartj ...

Raspberry Pi encountering a TypeError with Node.js async parallel: "task is not a function" error

I am new to nodejs, so I kindly ask for your understanding if my questions seem simple. I am attempting to use nodejs on a Raspberry Pi 3 to control two motors, and I keep encountering the error message "async task is not a function." Despite searching fo ...

Transferring information from Vue Component to Vuex storage

I am currently working with a Laravel API route that looks like this: Route::get('c/maintenances/{contractor_user_id}', 'Maintenance\Api\ApiContractorMaintenanceController@index'); The contractor_user_id parameter is dynamic ...

How come TypeScript remains silent when it comes to interface violations caused by Object.create?

type Foo = { x: number; }; function g(): Foo { return {}; // Fails type-check // Property 'x' is missing in type '{}' but required in type 'Foo'. } function f(): Foo { return Object.create({}); // Passes! } functio ...