Is it possible to apply a modal to a v-for item?

I am receiving a GET request from my API to display all users, and I want to potentially modify the name or email of each user using a modal. However, I am facing an issue where if I have 3 users displayed, clicking on one modal button triggers all 3 modals to appear.

How can I resolve this problem?

Below is my code snippet:

<template>
  <div class="container">
    <div>
      <h1>
        <b> Users </b>
        <input
          type="text"
          v-model="search"
          class="search"
          placeholder="Search User"
        />
      </h1>
    </div>
    <div class="overlay-container">
      <form>
        <div class="form-group">
          <div
            v-for="user in filterUser"
            v-bind:key="user"
            class="form-control"
          >
            <p>
              <b>{{ user.fname }} </b> {{ user.lname }}
            </p>
            <b-button v-b-modal.modal-1>Modify</b-button>

            <b-modal id="modal-1" title="User Modification">
              <p class="my-4">Please edit information</p>
            </b-modal>
          </div>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import VueAxios from "vue-axios";
export default {
  name: "users",
  data() {
    return {
      search: "",
      users: [],
    };
  },
  computed: {
    filterUser() {
      return this.users.filter((user) => {
        return user.fname.match(this.search);
      });
    },
  },
  created() {
    axios
      .get(`http://localhost:4000/api/users`)
      .then((Response) => (this.users = Response.data.data));
  },
};
</script>

I would like each user to open their own modal, instead of triggering all modals when clicking on just one button. I hope this explanation clarifies the issue!

Answer №1

To begin, extract the modal from within the v-for loop. Next, establish a method to retrieve the user index and incorporate it into the Modify button. By following this approach, you can access user data without the need for an additional HTTP Request. You will then be able to display the this.user information in your modal according to your preferences.

<div
 v-for="(user, index) in filterUser"
 v-bind:key="user"
 class="form-control"
>

 <p>
  <b>{{ user.fname }} </b> {{ user.lname }}
 </p>

 <b-button
  v-b-modal.modal-1
  @click="getUserInfo(index)"
 >
  Modifiy
 </b-button>
</div>
data() {
 return {
  search: "",
  users: [],
  user: {}
 };
},

methods: {
 getUserInfo(index) {
  this.user = this.users[index]
 }
}

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

Exploring the differences between a fixed-top navbar and dark theme in Bootstrap 5.3

I've been struggling to find a solution for having a fixed top navigation bar that remains solid without being transparent in both dark and light themes. Switching between dark and light themes results in the fixed-top navigation bar appearing transp ...

Having trouble positioning a div in AngularJS so that it stays pixel-perfect regardless of browser resize or device orientation? Unfortunately, it's

I am a newcomer to AngularJS and have been struggling with a particular issue for several days now, leading me to suspect that I may be using it incorrectly. The problem at hand involves positioning 30 divs in a specific manner: 1) Each div should displa ...

How can input be fixed in place within a list inside an Ionic framework popover?

I have the following code in a popover template: <ion-popover-view class="customPopup"> <ion-header-bar class="bar bar-header barHeaderCustom"> <h1 class="title">{{ 'AVAILABLE_SOUNDS' | translate }}</h1> </io ...

Display HTML in PHP and set the input value as a PHP variable

I'm struggling with my PHP code and need some assistance. I want to echo an HTML input field with a specific value from a variable, but it's not working as expected. Can anyone provide guidance on what might be going wrong? echo "<input type= ...

The calculator I designed using HTML, CSS, and JavaScript is experiencing difficulty adjusting to various screen sizes

I recently built a calculator using HTML, CSS, and JavaScript. It works perfectly on PC or big screens, but when viewed on smaller devices like phones or tablets, the display gets cut off and does not adjust properly. Here are some example pictures for ref ...

Align both text and images in the middle using HTML/CSS

I attempted to align an image next to text and center everything, but I'm facing some challenges. The arrow indicates where I would prefer the image to be placed. HTML <!DOCTYPE html> <head> <title>Simple Page</title> ...

"Discover the magic of jQuery: Unveiling the hidden div with one simple CSS visibility change

I want to implement a functionality on my screen where the Previous button is hidden initially and only becomes visible when the user clicks the Next button. I have set the CSS property for the Previous button to hide it by default, but despite using an if ...

Bootstrap sticky footer with adjustable height

Striving to achieve a sticky footer with a custom height on my website has turned out to be more challenging than I initially expected. Presented below is a snapshot of the current state of my footer: https://i.sstatic.net/SXaTA.png The issue arises whe ...

Having trouble retrieving the value from the input text in a dynamically generated table

How can I retrieve values from a dynamically generated table after clicking a button? Below is the code I have implemented to create a table with input fields: function createTable(rows, cols){ var body = document.body, table = document.createEl ...

Tips for sending a pre-made Vue component as a prop in Vue.js

Is there a way to pass an instantiated Vue Component as a prop to another component and then render it, similar to how it's done in React: <Button :icon=<IconComponent size="25" /> :link="http://wikipedia.org">Click her ...

Prevent the text within the textarea from wrapping onto the next line

My challenge involves a textarea where text overflows onto the next line when it exceeds the maximum characters. However, I want the text to continue on the same line with a scroll bar for better visibility. Here's an illustration: text here flows on ...

Clever method for enabling image uploads upon image selection without needing to click an upload button using JQuery

Is there a way to automatically upload the file without having to click the upload button? Detail : The code below shows an input field for uploading an image, where you have to select the file and then click the "Upload" button to actually upload it: & ...

Having trouble getting basic PHP IF/ELSE code to function correctly alongside HTML markup

I recently delved into learning the basics of PHP and everything was going smoothly until I attempted to incorporate some IF/ELSE statements within the markup. Here is a snippet of what I tried: <?php $msg = 0; echo $msg; ?> <!DOCTYPE html> ...

Is It Possible to Create Flash Content Without Using a SWF File?

Is there a way to embed Flash directly in HTML, rather than linking to an external SWF file? I am looking to send an HTML form via email for the recipient to fill out by opening it in a browser. The final step would involve copying the result to their clip ...

Remove Vue Component from the styling of current application

We integrated a Vue component (using Vuetify) into our existing .NET MVC application. The issue we are facing is that the Vue component is inheriting all the CSS styles from the rest of the application. Here's a simplified version of the HTML structur ...

The requested external js scripts could not be found and resulted in a net::ERR_ABORTED 404 error

import express, { Express, Request, Response } from 'express'; const path = require("path"); import dotenv from 'dotenv'; dotenv.config(); const PORT = process.env.PORT || 5000; const app = express(); app.use(express.static(path.join ...

What specific flag should be included in Chrome settings to prevent displaying c:/fakepath?

Is there a way to disable this security feature? I'm seeking a solution in Chrome to obtain the full file path of an uploaded or viewed file, similar to how it can be done in Internet Explorer. Despite my efforts with flags like --allow-file-access-f ...

The Input Boxes Are Too Broad

Currently, I am working on a responsive web page that features a form. However, I have noticed that all the input elements are spilling out of the form both from the left and right sides. Can someone please shed some light on why this could be happening? ...

Encountering a JavaScript error due to an erroneous character when trying to parse

I need to convert a `json` string into an object format that is extracted from a `.js` file. Here is the `JSON` string located in `document.js`: [ { "type": "TableShape", "id": "63c0f27a-716e-804c-6873-cd99b945b63f", "x": 80, ...

Is it possible to avoid widows in CSS without the need for extra HTML elements?

I have a snippet of text that is controlled within a Content Management System. The CMS restricts the use of HTML or special characters such as &nbsp; or <span>. Despite my attempts to adjust margins and padding, I cannot achieve the desired out ...