Create a v-on:click command within a v-for loop

I have a list of images displayed with some information. I am trying to make these images clickable so that when clicked, a div will show up saying "HI!!". I attempted to set a variable as show:true in Vue data and create logic to change it to false upon clicking, but I haven't succeeded yet.

Below is the code snippet for reference:

template>
    <div>
        <h1>SpaceX</h1>
        <div v-for="launch in launches" :key="launch.id" class="list" @click="iclickthis(launch)">
            <div ><img :src="launch.links.patch.small" alt="No Image" title={{launch.name}} /></div>
            <div>ROCKET NAME: {{launch.name}} </div>
            <div>DATE: {{ launch.date_utc}} </div>
            <div>SUCCESS: {{ launch.success}} </div>
            <div>COMPLETENESS: {{ launch.landing_success}} </div>
            
        </div>
        <!-- <v-model :start="openModel" @close="closeModel" /> -->
    </div>
</template>
<script>

import axios from 'axios'
export default {
  name: 'SpaceXTimeline',
  components: {
  },
  data: () => ({
    launches : [],
    openModel : false,
    show : true,
  }),
  methods: {
    iclickthis(launch) {
        // this should load a model search v-model / bootstrap on vue  npm install v-model 
        console.log(launch.name + " is launched");
        console.log("DETAILS: "+ launch.details);
        console.log("ROCKET INFO: "+ launch.links.wikipedia);
        console.log("CREW DETAILS: "+ launch.crew);
        console.log("Launchpad Name: "+ launch.launchpad);
        this.openModel = true;
    },
    closeModel () {
        this.openModel = false;
    }
  },
  async created() {
    const {data} = await axios.get('https://api.spacexdata.com/v4/launches');
    data.forEach(launch => {
      this.launches.push(launch);
    });
  }
}; 
</script>

<style scoped>
.list {
  border: 1px solid black;
}
</style>

Thank you for your help!

Answer №1

v-model is a binding mechanism and not an actual HTML element, unless you have defined it as a component. Is it possible that it was meant to be spelled as "modal"?

Regardless, it seems like what you need is a conditional rendering using v-if:

<v-model v-if="openModel" @close="closeModel" />

For example:

new Vue({
  el: '#app',
  components: {},
  data() {
    return {
      launches: [],
      openModel: false,
      show: true,
    };
  },
  methods: {
    iclickthis(launch) {
      // This is supposed to display a modal window when clicked
      console.log(launch.name + ' has been launched');
      console.log('DETAILS: ' + launch.details);
      console.log('ROCKET INFO: ' + launch.links.wikipedia);
      console.log('CREW DETAILS: ' + launch.crew);
      console.log('Launchpad Name: ' + launch.launchpad);
      this.openModel = true;
    },
    closeModel() {
      this.openModel = false;
    },
  },
  async created() {
    const { data } = await axios.get('https://api.spacexdata.com/v4/launches');
    data.forEach((launch) => {
      this.launches.push(launch);
    });
  },
});

Vue.config.productionTip = false;
Vue.config.devtools = false;
.modal {
  cursor: pointer;
  display: flex;
  justify-content: center;
  position: fixed;
  top: 0;
  width: 100%;
  height: 100vh;
  padding: 20px 0;
  background: rgba(255, 255, 255, 0.5);
}

img {
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

<div id="app">
  <h1>SpaceX</h1>
  <div v-for="launch in launches" :key="launch.id" class="list" @click="iclickthis(launch)">
    <div>
      <img :src="launch.links.patch.small" alt="No Image" :title="launch.name" />
    </div>
    <div>ROCKET NAME: {{ launch.name }}</div>
    <div>DATE: {{ launch.date_utc }}</div>
    <div>SUCCESS: {{ launch.success }}</div>
    <div>COMPLETENESS: {{ launch.landing_success }}</div>
  </div>
  <div v-if="openModel" @click="closeModel" class="modal">
    MODAL
  </div>
</div>

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

Top method for constructing a JSON object by combining data from two queries and a joining table

Currently, I am developing a small webservice in Node.js to handle commands in a restaurant using Express.js. In my database, I have the following tables: DISH(id,name) COMMAND(id,table,status) COMMAND_DISH(idCommand,idDish,quantity) I am interested in ...

Struggling to retrieve JSON data from the MercadoLibre API while consistently encountering the CORS error?

I have been attempting to access a mercadolibre API that provides JSON data I need to utilize. However, whenever I make an AJAX GET request, I keep receiving the same error: "Response to preflight request doesn't pass access control check: It does n ...

Tips for distinguishing specific content when generating pages dynamically with Gatsby JS

While working on a Gatsby site, I've found programmatically creating pages to be incredibly useful. Using the gatsby-node file to generate pages for different content categories is straightforward and efficient. However, I'm currently facing a ch ...

Seeking assistance from experienced individuals! Issue with countdown timer functionality in JavaScript

Hey there! I recently started my journey in learning HTML and wanted to create a timer for my website. Although the timer is displaying, the numbers are not showing up and it's not functioning properly. I would really appreciate some assistance from ...

Notify of an Invalid CSRF Token within the Action Buttons Present in the Table

I've encountered a problem with the CSRF token when using a Handlebars each loop. I created a data table using the each loop but I'm unable to submit the delete button. The CSRF token only seems to work for submitting new data (POST) and updating ...

Angular 4 Password Regular Expression Not Working

My validation regex checks for at least 8 characters, including one number, one uppercase letter, one lowercase letter, and one special character. Here is the regex I am using: '(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$@$!%*?&])[A-Za-zd$@$!%*?& ...

Learning how to toggle default snippet keywords on and off based on specific scenarios within the Angular Ace Editor

Within the Ace editor, I have incorporated custom snippets alongside the default ones. However, there are certain scenarios where I would like to exclusively display the custom snippets and hide the default ones. Is there a way to disable or conceal the ...

Issue with ng-click not functioning in Internet Explorer when selecting an option in AngularJS

I have the following HTML code snippet HTML: <select style="width: 100%" name="multipleSelect" id="multipleSelect" ng-model="data.multipleSelect" multiple> <option ng-click="BRPTab.AddFilesToOpenorDelete(fileName)" ng-repeat="fileName in BRP ...

Displaying SQL database values on hover in Leaflet map without using popups, utilizing PHP for data retrieval

     I am currently working on a leaflet map featuring multiple polygons that represent power outages in a specific state. Each town on the map can be interacted with by hovering over or clicking on it, which in turn updates information displayed with ...

Tips for transferring v-model to a child element along with the change event

In my Vue2 project with Vuetify, I am working on implementing filters to hide certain games when specific filters are turned off. Everything functions correctly until I attempt to move the filters into a separate component. The goal is to make these filt ...

Tips for generating click event with Angular directive

I am completely new to AngularJs directive creation. I have created a directive where, when the user clicks on the delete button, I am trying to print the values of scope, element, and attrs in the console. However, nothing is getting printed. The json dat ...

Consolidate radio group in Vuetify platform

I'm having trouble centering the v-radio-group. Here's my current setup: <v-container grid-list-md text-xs-center> <v-form ref="form"> <div v-if="question.question_type == 'YESNO' "> <v-radio-group ...

Typography Addition on Flexslider

I am currently working with flexslider and trying to incorporate a unique text overlay on each individual slide, but so far I have been unsuccessful. <div class="flexslider"> <ul class="slides"> <li> <img src ...

What causes the conflict between Nodejs usb module and Electron?

Apologies for the lengthy post, but I've been tirelessly searching for a solution online without any luck. My goal is to create a basic Electron application that can display an HTML page (which is working fine) and control a printer through the USB mo ...

The datepicker automatically updates when it reaches December

If you follow this link, you'll find a datepicker I am interested in using. There seems to be an issue when clicking on the next month button - once December is reached, the dates change but the title of the month remains the same. Similarly, when c ...

Error: Attempting to use the 'append' method on an object that does not support the FormData interface

$(document).on('submit','#form_pem', function(event){ event.preventDefault(); var kode = $('#kode').val(); var name = $('#name').val; var price = $('#price'). ...

When integrating react-hook-form with Material-UI TextField in a form, an error occurs stating that "TypeError: e.target is undefined" when

Hey there, I stumbled upon something fascinating and could really use some assistance. Every time I attempt to perform an onChange, I run into the following error: TypeError: e.target is undefined. Here's a snippet of my setup: import React, { useE ...

Incorporating a discreet progress bar into traditional file uploading processes

Have you noticed the trend of new, advanced file uploaders, many of which are Flash-based like SWFUpload? These uploaders can display a progress bar while files are being uploaded, making it much easier for users with shaky or low-bandwidth connections. H ...

Experimenting with Selenium and JavaScript to run automated tests using Fancybox as a demonstration

After integrating the fancybox scripts into my playframework application, I encountered a problem with using play auto-test selenium for testing the page where the script is applied. It seems that the auto-test function cannot accurately simulate the behav ...

The lookat function in Three.js appears to be inverted

Here is a demonstration of my point: Check out the Test Site or (Backup link) I am facing an issue where, despite the correct mouse vector, my object always rotates by 90 degrees in favor of the positive Y axis. The problem seems to be related to the call ...