Ways to manage V-show in a dynamically changing environment

I am currently in the process of developing an Ecommerce shopping platform. I have been importing data from a JSON file and successfully printing it using a v-for loop. One feature I am implementing is the Show Order Details option, where clicking on it reveals more detailed order information under the order details section by using the v-show tag. However, I have encountered an issue where, upon clicking the show order details option for every third order, the details section does not display properly as it fails to identify the ID for v-show. I attempted to resolve this using v-bind, but was unsuccessful. Any assistance would be greatly appreciated.

MyOrders.vue

<template>
  <div class="container">
    <div class="row">
      <div class="col-3">
        <h1 class="">MyOrders</h1>
      </div>
      <div class="form-class">
        <div class="col-md-12" v-for="item in MyOrders"
             :key="item.id">
          <div class="row">
            <div class="col-6">
              {{ item.order_quantity }}
            </div>
            <div class="col-6">
              <button v-bind:key="item.id" @click="active = !active">Show Order Details</button>
            </div>
          </div>
          <div class="row">
            <div class="col-6">
              <span class="text-dark">{{ item.order_number }}</span>
            </div>
          </div>
          <div class="row">
            <div class="col-6">
              <span class="text-dark">{{ item.order_tracking_id }}</span>
            </div>
          </div>
            <div class="row">
              <div class="col-6">Order details
                <span class="text-dark" v-show="active">{{ item.order_details }}</span>
              </div>
            </div>
          </div>
      </div>
    </div>
  </div>
</template>
<script>
import {myOrders} from "./MyOrders";
export default {
  name: "Myorders",
  data() {
    return {
      Myorders: myOrders,
      active: false,
    }
  },
  mounted(){
  },
  methods: {}
}
</script>
<style>
</style>

MyOrder.js

export const myOrders= [
    {
        "id": 1,
        "order_number": "11",
        "order_quantity": "10",
        "order_tracking_id": "1009",
        "order_details": "The order will ship to London",
    },
    {
        "id": 2,
        "order_number": "17",
        "order_quantity": "9",
        "order_tracking_id": "1055",
        "order_details": "The order will ship to Australia",
    },
    {
        "id": 3,
        "order_number": "15",
        "order_quantity": "13",
        "order_tracking_id": "1087",
        "order_details": "The order will ship to France",
    }
]

Answer №1

Instead of using a boolean to toggle details, you can utilize item.id:

const app = Vue.createApp({
  data() {
    return {
      myOrders: [{"id": 1, "order_number": "11", "order_quantity": "10",   "order_tracking_id": "1009", "order_details": "The order will ship to London",}, {"id": 2, "order_number": "17", "order_quantity": "9","order_tracking_id": "1055",       "order_details": "The order will ship to Australia",}, {"id": 3, "order_number": "15", "order_quantity": "13", "order_tracking_id": "1087", "order_details": "The order will ship to France",}],
      active: null,
    }
  },
  methods: {
    toggleDetails(id) {
      this.active = id === this.active ? null : id
    }
  }
})
app.mount('#demo')
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
  <div id="demo">
    <div class="container">
    <div class="row">
      <div class="col-3">
        <h1 class="">MyOrders</h1>
      </div>
      <div class="form-class">
        <div class="col-md-12" v-for="item in myOrders" :key="item.id">
          <div class="row">
            <div class="col-6">
              {{ item.order_quantity }}
            </div>
            <div class="col-6">
              <button v-bind:key="item.id" @click="toggleDetails(item.id)">Show Order Details</button>
            </div>
          </div>
            <div class="row">
              <div class="col-6">Order details
                <span class="text-dark" v-show="active === item.id">{{ item.order_details }}</span>
              </div>
            </div>
          </div>
      </div>
    </div>
  </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

Click the button to save the text to your clipboard with a customized

Can anyone suggest a method for duplicating div text using JavaScript while preserving the style attributes (italics, font family, size, etc.)? My goal is to paste the copied text into Word and have it maintain the same appearance as on the webpage. For e ...

Attempting to retrieve the current time using JavaSscript

const currentTime = new Date(); const hours = now.getHours(); Console.log(hours); This block of code is returning an error message... Uncaught ReferenceError: now is not defined Please note that this snippet is written in JavaScript. I attempted to us ...

What is the best way to automatically insert data into a database at regular intervals, while ensuring that users are unable to manipulate the timing through inspect

Is there a secure way to insert 1 point into the database every x seconds without allowing users to manipulate the interval time through inspect element? var time = 1; var interval = setInterval(function() { if (time != time + 1) { ...

Utilizing HTML's multiple input type color feature to save selected colors directly to the database

I am currently using the input type color to select colors for customizing my site. I save the selected color to a database and then retrieve it to apply it to different areas of the site. This works well for a single input type color, but now I have mul ...

Looking to design an interactive grid for generating dynamic thumbnails

I am a beginner in the field of web development and I have a desire to create a website for showcasing my portfolio. This website should feature project thumbnails along with brief descriptions, all of which should be displayed dynamically. Although I poss ...

Trouble encountered when utilizing jQuery for XML to HTML conversion and vice versa (CDATA mistakenly transformed into HTML comments)

I am in the process of developing a plugin/bookmarklet that is designed to extract an XML document from the <textarea> element on a web page, make modifications to the XML content, and then reinsert the updated version back into the <textarea> ...

What is preventing Web API from triggering a CORS error in browsers such as Chrome and Edge, as well as the Postman tool?

While working on developing an API in Asp.Net Core 3.1, everything seemed to be functioning properly. However, I encountered CORS-related errors when attempting to send requests via ajax. Interestingly, these errors were not present when sending GET reques ...

What is the best way to turn off default CSS styling in KendoUI?

I am facing an issue in my application where I am using global CSS definitions for "INPUT", "SELECT", and other elements. The problem arises when I try to incorporate KendoUI widgets, as they override my default CSS styles. For instance, my CSS code looks ...

Prevent links from being clicked multiple times in Rails using Coffeescript

Please make the following link inactive after it has been clicked once <%= link_to "Submit Order", {:action => "charge"}, class: 'btn btn-primary', id: 'confirmButton' %> To permanently deactivate the link, use the code below ...

Should you hold off on moving forward until the asynchronous task finishes?

My goal is to retrieve location coordinates using the Google Maps JavaScript API in an asynchronous manner. Below is the function I've created for this purpose: function fetchCoordinates(address) { var geocoder = new google.maps.Geocoder(); ...

The Art of Revealing an Element

Is it possible to manipulate a parent div element in JavaScript without affecting its child nodes? For example, transforming this structure: <div class="parent"> <div class="child"> <div class="grandchil ...

Why is the jQuery datepicker malfunctioning when nested within ng-repeat in AngularJS?

I am currently facing an issue with the jquery ui date picker in my AngularJS application. The date picker is functioning correctly outside of any ng-repeat loops, but it stops working when placed within one. <input type="text" class="form-control date ...

Steps to creating an Ajax JQuery in WordPress with promises

Currently, I am in the process of developing a custom Wordpress Google Maps plugin. This plugin fetches locations from a database using Ajax and returns an XML file that is then handled by a Javascript script to display them on a Google Map. Everything is ...

JavaScript function to substitute instead of writing

function replaceHTML(a,b) { var x = document.getElementById("canvas_html").contentDocument; x.innerHTML = b; } Although the current function works fine, I would like to update it to directly replace the existing content instead of writing new con ...

Is there a way to customize the animation for a button in material UI?

Trying to implement material UI in React and looking for a button that does not have the standard wave animation effect upon clicking, which you can see demonstrated here. Instead, I am searching for an animation that instantly fills the entire button wit ...

Mutex in node.js(javascript) for controlling system-wide resources

Is there a way to implement a System wide mutex in JavaScript that goes beyond the usual mutex concept? I am dealing with multiple instances of a node.js cmd running simultaneously. These instances are accessing the same file for reading and writing, and ...

Ensuring validity with Vuelidate for customizable fields

There's a form where fields are dynamically added on a click event. I want a validation error to appear when the field value is less than 9 digits after changing or blurring it. The issue is that since the fields are created dynamically with the same ...

Create a dynamic select2 field based on the value selected in another dropdown menu

Starting with an initial dropdown where a value needs to be selected: <select id="indexID" name="indexID" class="form-control" onChange="updateSector();" style="width:300px;"> <option value='' selected>Choose an Index</option> ...

"Encountering a hang while using the .save() function and only

Issue with storing data in MongoDB This is only my second attempt at saving data to a database and I am still relatively new to the process. I have a form on my HTML page that sends string data to be saved in a MongoDB database. I successfully connected t ...

Tips for updating server-side variables from the client-side in Next.js

There is a code snippet in api/scraper.js file that I need help with. const request = require("request-promise"); const cheerio = require("cheerio"); let url = "https://crese.org/distintivo-azul/"; let result; request(url, ...