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

What is the best way to integrate a PHP page with its own CSS and JavaScript into a Wordpress page?

I'm facing a challenge with integrating a dynamic PHP table into my WordPress page. Despite working perfectly when opened locally, the CSS and JavaScript used to create the table are not functioning properly when included in a WordPress page via short ...

I'm struggling to figure out why my code is throwing an Unexpected token error. What am I missing here?

I keep encountering an Unexpected token error in my code, specifically with a closing parenthesis ). What exactly does this error signify? Experimented by adding and removing parentheses as well as curly brackets. const getUserChoice = userInput => {u ...

How to work with a JSON object in Internet Explorer 6

Looking for some quick answers that should be easy for someone with expertise to provide. I have a basic asp.net site that relies on JSON for various tasks (and JSON.stringify). Everything works fine in Firefox and the like, but in IE6 I'm getting a ...

The output is: [object of type HTMLSpanElement]

<form> <table> <tr> <td>Distance:</td> <td><input type="number" id="distance" onKeyUp="calculate();">m</td> </tr> <tr> <td>Time:</td> ...

The appearance of an unforeseen * symbol caused a

Having issues with this particular line of code, import * as posenet from '@tensorflow-models/posenet' The error 'Uncaught SyntaxError: Unexpected token *' keeps popping up, I have the latest version of Chrome installed and I've ...

Look into HTML and JavaScript problems specific to IOS devices

I have encountered some HTML markup and JavaScript functionality issues on my web-app that seem to only occur on iPad and iPhone. Unfortunately, I do not have access to any iOS devices to debug these problems. How can I replicate and troubleshoot these i ...

Utilize Google Charts Table Chart to extract data from an object-literal notation data source

Here's a look at the data source and Listener function: Data Source var data = new google.visualization.DataTable( { cols: [{ type: 'string', label: 'Col1' }, ...

Encountered a JQuery error in the application: trying to call the 'open' method on dialog before initialization is complete

I am having trouble integrating a dialog box into my application and encountering the error mentioned above. Here is the jQuery code I'm using: $( "#dialog" ).dialog({ autoOpen: false, width: 450, modal: true, ...

Is it acceptable to employ async: false when requesting small amounts of data?

I am curious about the best practices for retrieving small data from the server. One example is using an ajax (or sjax) call to check for new notifications for a user. function checkNewNotifs() { $.ajax({ url: '/Home/CheckNewN ...

React.js onClick does not display the dropdown

Hello, I have a question regarding my navbar icon functionality. When I click on the icon, it is supposed to open a dropdown menu. However, although the div name changes when clicked, the CSS properties remain the same as the initial class. I am unsure w ...

Learn how to move to a new line when inputting data into a CSV file with JavaScript

My challenge involves taking an array of objects, for example: array=[hello, how, are, you], extracted from the document.innerHTML. I aim to write these objects to a CSV file using puppeteer and JavaScript, each on a new line. Although when using the sta ...

accessing information from webpage using hyperlink reference

This seems to be a rather straightforward issue, but unfortunately, I lack the necessary expertise to address it. Despite conducting thorough research, I have been unable to find a solution - mainly because I am uncertain about what specific terms or techn ...

Ways to stop jQuery from stripping the <script> elements

Is there a way to stop jquery from removing my JS default behavior? function loadPageSuccess(data) { var data = $(data).find('#content'); alert($(data).html()); $("#content").html(data); $("#page").fadeTo(100,1); } function loadP ...

Choosing an id of my preference to set as the value in the ng-options select list and ensuring proper binding with ng-model

When using Angular, I am trying to create a select list where the value corresponds to the actual id of an object. I want to bind this correctly with the ng-model directive. Here is my attempt: <select ng-model="selectedPersonId" ng-o ...

Enforce a limit of two decimal places on input fields using jQuery

Looking to limit an input field so that users can only enter numbers with a maximum of two decimals. Is it possible to achieve this using jQuery? Any way I could utilize the jQuery toFixed() function for this purpose? Thank you in advance! ...

Managing the uploading of files when a new property is included directly from the JavaScript File Object

When processing files using the POST method, I am able to access the default information such as name, type, size, tmp_name, and error through PHP. However, the file being sent contains additional data that I need PHP to extract. For instance, in the scre ...

The Angular service "this" is altering the context of the window object

I must have made a mistake somewhere and I know I am overlooking something obvious. The aim is to create a service that provides basic authentication features such as login, logout, and checking if a user is logged in or not. Upon loading the page, I veri ...

Issues with zDepth functionality in Material-UI (React.js) not being functional

Can anyone explain the concept of zDepth to me? I have a component with the following render method: render() { return ( <div> <AppBar zDepth={2} title="Some title" iconElementLeft={<IconButton onClick={this ...

Retrieve the file using React and receive it through Express

Despite trying several solutions on SO, none have been successful for me thus far, so I kindly ask for your patience. In React front-end, the user initiates a file download request with some variables to the backend: const data = text.value; fetch(" ...

Exploring the Node.js Connector: Utilizing Collection.remove

I can't wrap my head around the meaning of this: w, {Number/String, > -1 || ‘majority’ || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = ‘majority’ or tag acknowledges the ...