`Multiple confirmation modals appearing from delete button in vue-bootstrap`

Trying to find a solution for the issue of having a unique reference to the modal within this loop. I attempted giving the ref a grade.id at the end, but it didn't work with String interpolation and the b-modal ref. I also tried using :ref.

Below is the code in the Vue file, along with a screenshot illustrating how when I click to delete a grade, it displays all the modals because the modal is inside a loop.

<template>
  <div>
    <b-form @submit="onSubmit" @reset="onReset">
      <b-row>
        <b-col class="underline">Add Grades</b-col>
      </b-row>
      <br />
      ...
        // rest of the template code
      ...
                <p>
                  Confirm that you are about to delete grade
                  {{ getTitle(grade) }}
                </p>
              </b-modal>
            </span>
          </b-card-text>
        </b-card>
      </b-card-group>
    </div>
  </div>
</template>

// Script section here...

// Style section here...

https://i.sstatic.net/Hkkv0.png

Answer №1

To enhance efficiency, consider moving the modal outside of the loop. To pass along the data from the clicked grade, store it in your data as "activeGrade". In the modal, you can then display the information related to "activeGrade", allowing you to open the same modal with the specific data for the clicked grade.

Below is a simplified version of your code focusing on the highlighted issue:

<template>
  <div class="hello">
    <b-card-group class="mb-4" v-for="grade in grades" :key="grade.id">
      <b-card :title="grade.title">
        <b-card-text class="list-card">
          Some Text (grade properties)

          <span style="float: right">
            <b-button
              class="mr-1"
              type="reset"
              variant="danger"
              @click="showModal(grade)"
              size="sm">
              <!-- Fix css to icon middle -->
              <b-icon icon="trash-fill" size="is-small"></b-icon>
            </b-button>
          </span>
        </b-card-text>
      </b-card>
    </b-card-group>

    <b-modal v-if="activeGrade"
            scrollable
            id="modal-delete-grade"
            centered
            @ok="actionDelete(activeGrade.id)"
            class="d-block text-center"
            title="Confirm Delete Grade">
      <p>
        Confirm that you are about to delete grade
        {{ activeGrade.title }}
      </p>
    </b-modal>
  </div>
</template>

<script>
export default {
  data() {
    return {
      grades: [
        { id: 1, title: 'Grate 1' },
        { id: 2, title: 'Grate 2' },
        { id: 3, title: 'Grate 3' },
      ],
      activeGrade: null
    }
  },
  methods: {
    actionDelete(gradeId) {
      console.log('delete ' + gradeId)
    },
    showModal(grade) {
      this.activeGrade = grade

      this.$nextTick(() => {
        this.$bvModal.show("modal-delete-grade");
      })
    }
  }
}
</script>

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

The functionality of jQuery on dropdown list change seems to be malfunctioning in Codeigniter

As a novice in CodeIgniter, I've scoured various StackOverflow threads for a solution to my issue without any luck. Below is the view code causing trouble: <select name="select1" id="select1"> <option value="0">None</option> ...

What steps do I need to take to set up Vue-CLI 3 to generate a webpage with no JavaScript?

My dilemma is this: I have a simple static page that does not require JavaScript. Using vue-cli 3, I am attempting to pass the HTML file through webpack for minification purposes. Unfortunately, it seems that accomplishing this task is not as straightforwa ...

Tips for customizing table cell styling in editable cells with React material table

My code utilizes a material table with editable cells. However, I am encountering a strange style issue when I edit a cell in the table. Please refer to the image below. Can anyone suggest a solution to fix this problem? https://i.sstatic.net/Miiov.png ...

Running a PHP MySQL function with Onclick without using POST data

I am seeking to trigger a MySQL search using an ONCLICK event in PHP. Initially, I considered utilizing a JavaScript or jQuery function to parse the selected ID through JS. However, I am unsure of how to re-execute the PHP to conduct a new search on the d ...

When using a function linked to an API request, an uncaught TypeError is thrown: Unable to access the 'includes' property of an undefined value

Utilizing the movie DB API (), I am displaying the results of my call on my page using Vue. The API supplies all the necessary data for me to achieve my objective, as demonstrated in this image https://i.stack.imgur.com/vP4I2.jpg Beneath each show's ...

I'm struggling to understand the folder arrangement and the system is telling me it can't locate my file

I'm having trouble identifying what might be causing issues with my file structure. Currently, I am using Aurelia for the front end and node for the server. I attempted a fix by performing a join operation, which resolved some of the problems. However ...

Retrieve the form identification in jQuery Mobile on the 'pageshow' event

Is there a way to retrieve the form ID of the current page using 'pageshow' in jQuery Mobile? I am currently able to obtain the ID of the active page by following this method. $(document).on('pageshow', function () { var id = $.mobile. ...

JavaScript has the ability to manipulate the width of an element by using methods to both retrieve

I have a unique situation where I need to dynamically adjust the width of a ul list element based on the text content inside it. Specifically, I want the width of the ul list to be equal to the width of the first list item, which can change frequently. My ...

What would be the JavaScript version of the below jQuery code?

I am interested in creating a spinning wheel where instead of the entire wheel rotating, only the pointer rotates. I currently have a jQuery code snippet that achieves this effect, but I am looking to convert it into JavaScript. Current jQuery code: $( d ...

What can Cordova and express js bring to the table together?

I'm feeling pretty lost when it comes to displaying express views in a Cordova app client. It seems like the app would have to send a GET request and then the express application would render the view. But I'm unsure about how to actually make t ...

Compilation of documentation from various GitHub repositories to create a static website

Assistance is needed in finding the right solution to streamline documentation creation for a group of friends. We have multiple repositories where a doc folder containing various .MD files will be stored. Repo1 |- Readme.MD |-docs |- Installation.MD | ...

show the most up-to-date data in the array upon selecting a particular element

I'm working on a project with an array of objects representing different pets. Each pet card should display specific information about the pet when clicked, but I'm struggling to make only the relevant information show up. How can I achieve this ...

Updating language settings on-the-fly in a Vue application does not automatically refresh the translated content displayed on the website

My Vue app is quite large, built with Vuetify, and I recently integrated vue-i18n into it. The json translation files are set up correctly, and the $t() calls work fine in my components and other .js files. However, I'm struggling to change the locale ...

Using jQuery, you can easily modify the color of a TD cell by applying the css properties assigned to the div element

I am attempting to implement a jQuery script that will execute upon the loading of the document. The objective is for the script to retrieve the background color of a div located within a td cell and apply it as the background color for the respective td c ...

The timepicker is set to increment by 30-minute intervals, however, I would like the last time option to be 11:

I am currently using a timepicker plugin and am trying to set the last available time option to be 11:59pm. Despite setting the maxTime attribute in my code, the output does not reflect this change. Any suggestions on how to achieve this would be highly ap ...

Does the rendered ID of an ASPX control always appear the same in the source HTML code?

Let's say I have an aspx textbox with id="txtkms". In the HTML view source, it appears as ContentPlaceHolder1_Gridview1_txtkms_1. I'm curious if this control will always be rendered as ContentPlaceHolder1_Gridview1_txtkms_1 every time I run my as ...

Writable Input-Buttons in Bootstrap now enabled

This is the appearance of my Button: <input class="btn btn-outline-primary" id="dashboard" value="Zurück" onclick="doStuff();"> It's just your typical Bootstrap styling. However, things take a strange turn when I click the button and change i ...

concealing the dropdown in React upon clicking outside of it

I have been delving into learning React by challenging myself with a React problem. The code challenge I'm facing involves trying to close a drop-down when clicking outside of it. Despite my efforts to debug, I haven't had much luck in solving th ...

What is the process for including a checkbox with a label in Card Media?

I attempted to create this feature using code. However, I encountered an issue where the CardMedia and the checkbox would not align properly, resulting in a lack of responsiveness. <Card> <CardMedia ...

Using HTML code within a Bootstrap 5 tooltip is a great way

After referring to the official documentation, I am trying to show some HTML content inside a bootstrap 5 tooltip. Unfortunately, simply copying and pasting code from the documentation does not produce the desired result. <button type="button" ...