How can I make v-on:click trigger a modal for specific objects exclusively?

How can I modify the code below to ensure that when I click on the SAN button, the modal only displays once with the customerName (SAN) inside it? Currently, clicking on SAN, DAVID, or JAY generates the modal three times.

This is my template file:

<div id="app">
<div v-for="post in posts" v-bind:key="post.createdAt">
  <div>
    <b-col md="3">
      <div v-for="item in post.items" v-bind:key="item.id">
      <div v-on:click="item.id = !item.id" style="color: blue;">
         <b-button v-b-modal.modal-xl variant="info">{{post.customerName}}</b-button>

            /** Modal Display Code from Bootstrap VUE **/

             <b-modal id="modal-xl" centered size="xl" title="TEAM NAME 1">
                <p> Booker Name = <u style="font-weight:bold;">{{post.customerName}}</u> </p>
                  <b-container class="bv-example-row">
                     <b-row style="font-weight:bold;">
                     <b-col><p>Child Name</p></b-col>
                     <b-col><p>Text Number</p></b-col>
                     <b-col><p>No Show</p></b-col>
                    </b-row>
                    <b-row>
                      <b-col><p>David</p></b-col>
                      <b-col><p>P</p></b-col>
                      <b-col><p>817 360 2705</p></b-col>
                      <b-col><input type="checkbox" v-model="subchildNoShow"/></b-col>
                    </b-row>
                </b-container>
             </b-modal>

             /** END OF MODAL **/

          </div>
       </div>
     </b-col>
    </div>
 </div>
</div>

Script Function:

export default {
  name: 'App',
  components: {

  },

  data(){
    return{
      searchQuery: '',
      posts: [],
      subchildNoShow: []
    }
  }
}

JSON File values:

data{
 Object[0]{
    customerName:'San',
    createdAt: '2020-04-15',
    items:{
       id:'1',
       arrivalTime:'06:00 PM'
    }
  }

  Object[1]{
    customerName:'David',
    createdAt: '2020-04-15',
    items:{
       id:'2',
       arrivalTime:'07:00 PM'
    }
  }

 Object[2]{
    customerName:'Jay',
    createdAt: '2020-04-15',
    items:{
       id:'3',
       arrivalTime:'07:00 PM'
    }
  }

}

Answer №1

It is recommended to have only one modal. Place it outside of the v-for

            /** This code displays the modal from BOOTSTRAP VUE **/

             <b-modal id="modal-xl" centered size="xl" title="TEAM NAME 1">
                <p> Booker Name = <u style="font-weight:bold;">{{selectedCustomerName}}</u> </p>
                  <b-container class="bv-example-row">
                     <b-row style="font-weight:bold;">
                     <b-col><p>Child Name</p></b-col>
                     <b-col><p>Text Number</p></b-col>
                     <b-col><p>No Show</p></b-col>
                    </b-row>
                    <b-row>
                      <b-col><p>David</p></b-col>
                      <b-col><p>P</p></b-col>
                      <b-col><p>817 360 2705</p></b-col>
                      <b-col><input type="checkbox" v-model="subchildNoShow"/></b-col>
                    </b-row>
                </b-container>
             </b-modal>

             /** END OF MODAL **/

Assign selectedPost on click and display the modal.

<div v-on:click="selectItem(post, item)"

Define the selectItem function

methods: {
 selectItem (post, item) {
  item.id = !item.id
  this.selectedCustomerName = post.customerName
  // show the modal
  $bvModal.show('modal-xl')
 }
}

Answer №2

Make each modal component unique for every item by implementing the following:

  <b-button @click="$bvModal.show('modal'+item.id)" variant="info">{{post.customerName}}</b-button>
               <!--    v----------------------^ -->
 <b-modal :id="'modal'+item.id" centered size="xl" title="TEAM NAME 1">


Utilize the modal instance show method to append the item id to the word "modal," creating a unique identifier like modal1, which is then passed as an id to the modal using :id="'modal'+item.id"

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

How can I retrieve the content from an iframe whenever its state is altered using jQuery?

I am currently working on a project where I need to extract the content from a hidden iframe and display it as HTML in a div every time the iframe changes its loading state. The goal is for it to appear as if the page is being redirected and downloading it ...

Ways to troubleshoot JavaScript following an AJAX request?

My webpage is structured into three separate files. The index.html file contains a navigation bar, a content box, and a footer within 3 divs. Additionally, there are two other .html files with different content that should load upon clicking specific links ...

Display outcome generated by JavaScript in the input box

I've successfully implemented a date/time picker in a form. Using JavaScript, I've created a script to calculate the difference between two date fields and display it in a third field labeled Course Duration. However, I'm encountering an i ...

What is the best way to arrange images in a 3 by 3 grid, beginning at position 0, using JavaScript to control navigation through button clicks?

When I click on button 1, it starts at image 1 because the counter is set to 0. Clicking on button 2 takes me to image 4 with a counter value of 3, while clicking on button 3 leads to image 7 with a counter value of 6. The process should also work in reve ...

Utilize the .not() filter function in JavaScript

I am trying to apply a filter of not(.pseudo-element) using JavaScript, but I am unsure how to add it. So far, I have been able to extract #app from the DOM with the following code: const app = document.getElementById('app') app.style.filter ...

What could be causing the error when attempting to retrieve an index using the window variable?

I'm facing a strange issue where I am trying to utilize a variable that I define as follows: window.params['variable'] = { ... }; Within the function that I am using, the code looks like this: function q() { ... // for example return n ...

Guide on launching a Firefox browser through Selenium 3.6.0 with a different profile in JavaScript

How can I customize the Firefox browser settings in selenium-webdriver 3.6.0 for automated testing? I need Firefox to download files without prompting and save them to a specific directory. For Google Chrome, the approach is as follows: if (this.bName == ...

Issue with the Animated Skill Bar not functioning properly when scrolling

I've been trying to implement an animated skill bar in my Portfolio using JQuery, but I'm facing some challenges. Despite following various tutorials, the code doesn't seem to work as expected. I've tried calculating section scroll posi ...

Is there a way to retrieve JSON data from a different domain and incorporate it into my own website?

I am attempting to utilize JSON data from "" and display the data on my website. My goal is to extract the JSON field name "Stats" and retrieve the sub-field name '\"v\"'. You can refer to the documentation provided by purpleair here: h ...

The type 'void | Observable<User>' does not include the property 'subscribe'. Error code: ts(2339)

authenticate() { this.auth.authenticate(this.username, this.password).subscribe((_: any) => { this.router.navigateByUrl('dashboard', {replaceUrl: true}); }); } I'm puzzled by this error message, I've tried a few solu ...

The request header fails to function properly when used for cross-domain Ajax requests

I'm facing a challenge with adding a parameter in the request header. It works smoothly for calls within the same domain, but when making a call to a different domain (the API), I need to adjust the header parameter itself. Here is the snippet of cod ...

Is there a way to remove a portion of a string?

Looking to remove a section of a string using JavaScript? I attempted var sentence = "C:\mnt\c\User\Foo\Bar"; var updatedSentence = sentence.replace("mnt\c", ""); But the result was not as expected ...

Issues with JS submit() function not functioning as intended

I'm having an issue with the Javascript submit() function not working. When I run my PHP script (hax.php), it doesn't return any value. All I want is for AJAX to confirm that it has executed successfully. I'm wondering if the success method ...

No value found for the existing property

Recently, I began working on building a RESTful API using node.js, express, and mongodb. The progress was smooth until now, where I encountered a problem with the authentication route for users: apiRoutes.post('/authenticate', function(req, res) ...

Issue: There is a gap in the Vue router navigation when scrolled on a single-page application using v-navigation-drawer

Need help fixing gap at the bottom of side nav I'm having trouble removing the gap at the bottom of the v-navigation-drawer in my Vue project. I've included the code snippet below. Currently, I am using NUXT and VUE router for this project. & ...

Producing numerous results from a single input

How can I ensure that users input their address correctly, including street, number, entrance, floor, and apartment, into a single form field without missing any of the values? Additionally, how can I then extract each value (street, number, entrance, floo ...

After encountering a character with CSS, begin a new line

I have a CSV spreadsheet with data that looks like this: <p>Features:• first feature• second feature• third feature• fourth feature• and so on (the total number of features is variable)</p> I want each feature to display on a new li ...

How can angular DI be effectively implemented with libraries such as Modernizr, lodash, and jquery plugins to optimize performance and functionality?

As I delve into the world of Angular, I've noticed that it's not very common to wrap third-party scripts in an AngularJS provider for dependency injection. I'm still trying to figure out the best approach to leverage DI with jQuery plugins, ...

Ways to extract information from a JSON array based on its length and content

Here is an example of some data: { "_id": ObjectId("528ae48e31bac2f78431d0ca"), "altitude": "110", "description": [ { "id": "2", "des": "test" } ], "id": "1", "latitude": "24.9528802429251", ...

Obtain a collection of information from a web API by utilizing jQuery

Click on the following link to access live data from a web API: This is my first attempt at retrieving data from an API, so I may have missed some settings. To test the connection with the API, I included the following code in the HTML page: <div id= ...