What is the best way to show a nested div element within a v-for loop in Vue.js?

One interesting feature of my coding project is that I have an array nested within another array, and I iterate through them using two v-for loops. The challenge arises when I try to display a specific div in the second loop only upon clicking a button. To better illustrate this issue, I have created a demonstration on jsFiddle: https://jsfiddle.net/h8L6zegc/1/

<template> 
<div id="app">
  <h1>My Orders</h1>
  <div v-for="(orders, index) in SubmittedOrders" :key="index">
    <h3>#2222</h3>
    <h4>170$</h4>
    <button @click="openModal(orders)">Order Details</button>
    <hr>
    <br>
    <div v-show="Showmodal">
      <div v-for="order in orders" :key="order.id">
        <h3>{{ order.orderName }}</h3>
        <h3>{{ order.price * order.quantity }}</h3>
      </div>
      <hr>
      <h3>#2222</h3>
      <h4>170$</h4>
      <button @click="closeModal(orders)">Close Modal</button>
    </div>
  </div>
</div>

</template>
<script>

 data() {
    return {
      SubmittedOrders: [
        [{
            orderName: 'Nissan',
            price: 50,
            quantity: 1,
          },
          {
            orderName: 'Ferrari',
            price: 120,
            quantity: 2,
          },
        ],
        [{
            orderName: 'Porsche',
            price: 90,
            quantity: 1,
          },
          {
            orderName: 'Mercedes',
            price: 180,
            quantity: 4,
          },
        ],
      ],
      Showmodal: false,
    };
  },
  methods: {
    openModal(orders) {
      console.log(orders);
      this.Showmodal = true;
    },
    closeModal(orders) {
      this.Showmodal = false;
    }
  }

</script>

Answer №1

To better manage the display of orders in your application, consider creating a variable within the data section to store open orders and utilize it to create a condition for the v-show directive. Here is an example:

<div v-show="orders===activeOrders">
 data() {
    return {
      activeOrders: null
  ...
 methods: {
    openModal(orders) {
      console.log(orders);
      this.activeOrders = orders;
    },
    closeModal(orders) {
      this.activeOrders = null;  
    }
  }

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

Challenge encountered while creating a child tag that can be assigned to multiple parent tags

I have an xml format like the one provided. <xml> <parent id="0"> <child type="name"> </child> <child type="age"> </child> </parent> <parent id="1"> <ch ...

Exploring the depths of a multidimensional dictionary within AngularJS

I am currently working on a project using AngularJS. The data I have is in the form of JSON: { "leagues":{ "aLeague":{ "country":"aCountry", "matchs":{ "aUniqueID1":{ "date":"2014-09-07 13:00:00", "guest_play ...

An unexpected error occurred during page generation. Any relevant console logs will be shown in the browser's terminal window

Encountered a RangeError: Maximum call stack size exceeded while compiling this web app in the browser. The error occurred on my header tag and sometimes on the return tag. What should I do? export default function Header() { return ( <> ...

What are some ways to execute a script prior to installing an npm package?

I need help creating a test for npm packages in my project. I want to ensure that every time I attempt to install a module using npm install <module>, a script runs before the module is installed. However, I've noticed that the preinstall script ...

How Meteor Handles HTTP Requests in its package.js File

When accessing data from an external source at autopublish.meteor.com, our goal is to gather information about a package's latest release tag either from GitHub or the NPM registry: var version; try { var packageJson = JSON.parse(Npm.require(' ...

The ngClass directive does not seem to be functioning properly when utilizing multiple conditions

Trying to apply [ngClass] under different conditions. Here is what I have: condition [ngClass]="{'validator':lang.VideoURL!=null, 'labeltitle': lang.VideoURL==null}" However, when the value of lang.VideoURL is null, the labeltitle cl ...

Executing the slideToggle() function in JQuery with javascript

Trying to create a script that expands and collapses when users click on either the text or image. The image is a basic arrow that switches to an arrow pointing upwards when the div is expanded. The issue I'm facing is that the text disappears when t ...

Error message in TinyMCE stating that the right-hand side of the 'instanceof' operator is not callable

For over a year now, I have been utilizing TinyMCE on my website without any issues. However, today it has suddenly ceased functioning and is displaying the following error message. Can anyone provide guidance on resolving this issue? ...

Tips for sending two values to a PHP file using JavaScript Ajax

I have created a code for two dropdown menus. The goal is to select values from both menus and send them to a php file using the GET method. The values should be sent to the php file only when both menus have selections made. Below is the code snippet: ...

Can you explain the process of variable allocation?

I have a query regarding the following JavaScript code snippet. It might be a basic question, but I'm seeking clarification. // 'response' contains JSON data received from the backend: ....then(response => { this.myVar = response.data; ...

serving index.html using express and react

I am currently working on an application with Express, which includes a create-react-app. My goal is to display the index.html file located in the public folder of the create-react-app when a user visits the root URL. Below is the code snippet from my inde ...

The ng-click function ceases to trigger after the $location dependency is included in the controller

I need some help with running a ng-click function from my controller. The strange thing is, when I don't use the $location dependency, the ng-click function works fine. But as soon as I add $location to the controller, the ng-click function stops work ...

Having a problem with the glitch effect in Javascript - the text is oversized. Any tips on how to resize

I found some interesting code on the internet that creates a glitch text effect. However, when I implement it, the text appears too large for my webpage. I'm struggling to adjust the size. Here is the current display of the text: too big This is how ...

React-router-dom v6 causing MUI Drawer to not render

I have implemented ReactJS and I am working on incorporating a drawer/menu to display different routes on each page. I have set up the routes using react-router-dom@v6 in my index.js file. When I directly enter the URL for a specific page, I can see the co ...

WebPack Error: When calling __webpack_modules__[moduleId], a TypeError occurs, indicating that it is not a function during development. In production, an Invalid hook call error

Encountering a WebPack error when utilizing my custom library hosted as a package and streamed with NPM Link. Interestingly, the production version functions flawlessly. Below are my scripts: "scripts": { "dev": "rm -rf build ...

Vue-Router failing to redirect in a Single Page Application using Vue and Laravel

I have been attempting to implement vue-router in a Vue.js/Laravel project. My project consists of two simple pages: home page: https://i.stack.imgur.com/GmHOR.png about page (single-page scrolling): https://i.stack.imgur.com/CZDnZ.png The files used ...

Modifying button attribute through dropdown selection

In my project, I have a dropdown that gets its data from a database. Depending on the selection made in the dropdown, I would like to change the attribute of a button (data-uk-modal="{target:'#modal-'value of dropdown'}"). <select id "ci ...

I encountered an issue with loading an array from session storage in Java Script

I am struggling to restore and reuse a created array in HTML. I attempted using JSON, but it was not successful for me. In the code below, I am attempting to reload items that were previously stored in an array on another page. However, when I try to loa ...

What is the reason my answer for the powerset problem is flawed? Both my recursive and iterative methods are attached for review

How can I generate all possible subsets from an array of unique integers? For instance, if I have powerSet[1,2,3], the expected output should be [[], [1], [2], [3], [1,2], [1,3], [2,3], [1,2,3]] I've tried a recursive approach: function powerset(arr ...

Illustrate an element positioned beneath a child element within a different element

I am in the process of designing an overlay on a Google Map that resembles a real map placed on a table, using 2 pixel lines as creases above the map. This design does not significantly impact interactions with the map; only 6 out of 500 vertical lines are ...