Transferring information via a function to a modal component in VueJS

Seeking advice on passing data to a modal.

I'm attempting to pass data to a modal, where a function within a v-for loop gathers the video URL for later opening.

The modal is placed outside the v-for loop to prevent all videos from playing the same one, especially in cases where there are multiple videos in a season.

<div class="item_XBG3T" v-for="(item, index) in data.season[show_season]" :key="index">
  <div class="image_33keh lazyloaded" @click.sync="openWatch">
    <img :src="'/_assets/img/covers/episodes/' + item.backdrop" class="lazyload" :alt="item.name">
    <div class="duration_Ob58r">{{ runtime(item.duration) }}</div>
    <div class="play_2ONZn"><svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 55 55"><circle cx="27.5" cy="27.5" r="26.75" fill="none" stroke="#fff" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"></circle><path fill="none" stroke="#fff" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M20.97 40.81L40.64 27.5 20.97 14.19v26.62z"></path></svg></div>
  </div>
  <h2 class="name_hMDmw" v-if="item.episode_number >= 10"><strong>E{{ item.episode_number }}</strong> {{ item.name }}</h2>
  <h2 class="name_hMDmw" v-else><strong>E0{{ item.episode_number }}</strong> {{ item.name }}</h2>
  <div class="overview_1HUXl">{{ item.overview | truncate(125, '...') }}</div>
  <app-play-episode v-if="watchVisible" :video="item.video" :name="item.name" type="iframe" @close="closeWatch"></app-play-episode>
</div>

Here's the method to pass data to the modal when it's opened:

openWatch() {
   this.watchVisible = true;
},

closeWatch() {
  this.watchVisible = false;
},

This is how the episode data is retrieved:

computed: mapState({
  data: state => state.series.show,
  loading: state => state.series.loading
}),

beforeDestroy() {
  this.$store.commit('CLEAR_SERIES_SHOW_DATA');
},

mounted() {
  this.$store.dispatch("GET_SERIES_DETAILS", this.$route.params.id);
},

The goal is to avoid displaying the same URL for all the videos that need to be viewed.

If you have any insights on how to achieve this, I would greatly appreciate it.

Answer №1

Within the div that will be clicked inside the v-for, add the following:

@click="openModal(item.video, item.name)"

The openModal method is where we assign values to the data later on.

<app-play-episode v-if="watchVisible" :video="videoplay" :name="videoname" type="iframe" @close="closeWatch"></app-play-episode>

Place <app-play-episode> outside of the v-for and add the following in the data() section:

data() {
  return {
    videoplay: null,
    videoname: null,
    watchVisible: false,
  };
},

In the methods section, include the following:

openWatch(video, name) {
   this.videoplay = video;
   this.videoname = name;
   this.watchVisible = true;
},

These methods assign values to the variables.

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

Switch between showing or hiding at least three divs

I'm currently using a simple script that toggles the visibility of two div elements: <script type='text/javascript'> $(window).load(function(){ function toggle_contents() { $('#page1').toggle(); ...

Looking to incorporate HTML and JavaScript to create two unique forms that can be submitted separately on a single webpage

If I can't find an answer, then I ask: There are two different forms on the page: one is the "ask for call" form (which appears on every page of the site) and the other is a simple "contact form" (specifically placed on the contact page). Both forms ...

The request.body in Express.js is currently undefined

const express = require('express'); const cors = require('cors'); const app = express(); app.use(express.json()) app.use(cors()); app.post('/', (req,res) => { console.log(req.body); res.send('received') ...

Vue3 is struggling to apply dynamic styling exclusively to certain buttons

Currently, I am diving into the world of vue and facing a challenge in styling buttons dynamically when clicked individually. These buttons serve as filters for a list of products, and my goal is to apply one style when the filter is 'on' and ano ...

Is there a way to define the width of an element within the display:flex property in CSS?

Could you please review the following: https://codepen.io/sir-j/pen/dyRVrPb I am encountering an issue where setting the input [type=range] to 500px doesn't seem to make a difference from 100px, 500px, or 800px. This leads me to believe that display ...

Bidimensional Selenium matrix

Could someone please help me understand how to extract values from a bi-dimensional array using Selenium? I have a resources.js file with the array created, and need to access it in Selenium. Here is the structure of the array: The array consists of 4 col ...

Imitate adjustment of orientation without the need to resize the browser window

Is there a way to simulate page portrait orientation on a PC browser without resizing the window? In my HTML code, I have a <div> element that displays content dynamically based on screen orientation. Is it possible to use JavaScript to trick this & ...

Adding a detached element under certain conditions

When attempting to add a detached span based on a specific condition, I encountered the following situation. const arrow = d3 .create('span') .classed('arrow', true) .text('\u2192'); //d3.select('bod ...

Action outcome yielding identical content

Is there a way in MVC C# to return an ActionResult without making any changes to the view? I have attempted: Returning null and new EmptyResult(), but these result in an empty page Returning an empty JavaScript (again, resulting in an empty page) Retur ...

Create a function that generates an HTML string and returns it to be used in JSX

As a newcomer to JSX/React, I am seeking guidance on removing inline code. I have created a function that returns the value to be displayed in the template/JSX. The function works well except when it needs to return a string along with an element like a Li ...

eliminate empty lines from csv files during the uploading process in Angular

I have implemented a csv-reader directive that allows users to upload a CSV file. However, I have noticed an issue when uploading a file with spaces between words, resulting in blank lines being displayed. Here is an example: var reader = new FileReader ...

I'm encountering a typescript error as I migrate a Paho MQTT function from Angular 1 to Angular 2 - what could be causing this issue?

When connecting to my MQTT broker, I am working on several tasks. In my Ionic 2 and Angular 2 application, I have created an MQTT provider. Here is the provider code: import { Component } from '@angular/core'; import { NavController, ViewControl ...

Navigating Next.js: Mastering the art of localStorage Access

Currently, I am developing my first member area using next.js. My goal is to store some data in localStorage (such as token, expiresAt, userInfo), which will eventually be moved to an http-only cookie. The code below is generating the error: "LocalStorage ...

Switching over from create-react-app to NextJS, encountering difficulties with integrating ThreeJS

A while back, I had successfully implemented a 3D model viewer in React. Now, I'm facing a challenge as I try to migrate this project to Next.js. The specific issue lies with the GLTFLoader and OrbitControls dependencies that were previously imported ...

Is there a way to shut down a browser tab without being asked, "Are you sure you want to close this window"?

Is there a way to gracefully close a browser window without being interrupted by the annoying Do you want to close this window prompt? I've noticed that whenever I attempt to use the window.close(); function, this prompt always pops up. ...

Clickable elements are not functioning on dynamically generated divs

In the process of developing an application using Angular, I encountered a scenario where I needed to fetch and display data from a web service. The challenge was in dynamically creating div elements with the retrieved data: for(var i = 0 ; i < data.Ou ...

Passing data from React component to JavaScript page or function

I am trying to pass a variable called "pokemon" from Cell.js to Detail.js. Cell.js creates buttons, each representing a different pokemon. When clicked, it should navigate to a detailed page about the specific pokemon. Therefore, I need to send the pokemo ...

Customize the appearance of a React component depending on its unique identifier

After creating a simple TODO app with drag and drop functionality, I am now looking to apply a line-through CSS style to any task added or dragged into the second column of my app. What is the best way to target these tasks using their unique ID: <div c ...

Exploring the possibilities of utilizing JavaScript within TypeScript

My dynamic javascript object holds all the resources (translation strings) for my app. Here's how it is structured: var ResourceManager = (function () { function ResourceManager() { var currentLanguage = $('#activeLanguage').htm ...

Tips for generating a server error during the retrieval of JS files

I'm a beginner in JavaScript and I've been given a task to send an email input from a form to a node server. Everything is working correctly, but there's one requirement I need to implement: Whenever the entered email is [email protec ...