What is the best way to showcase the information of each object on a click event in Vue.js?

My goal with this code is to display each day's class schedule when it is clicked on. However, the issue I'm facing is that the entire week's schedule is being displayed instead of just the selected day. What adjustments should I make in order to fix this error?

<div id="app">
 <div v-for="day in days" :key="day.name" class="days">
  <button @click="showSchedule(day)"> {{day.name}} </button>
 </div>
 <div v-if="displaySchedule">
  <ul>
   <li v-for="n in selectedDay.schedule">
    {{n.time}} - {{n.subject}} ({{n.mode}})
   </li>
  </ul> 
 </div>
</div>

In this code snippet, the showSchedule method has been modified to receive a parameter representing the selected day and toggles the displaySchedule property accordingly.

new Vue({
 el: '#app',
 data: {
  displaySchedule: false,
  selectedDay: {},
  days: [
    {
    name: "Mon",
    date: "10",
    lessons: "4 lessons",
    grade: "Grade 9",
    schedule: [
      {
        time: "9:00",
        subject: "Biology",
        mode: "Lecture"
      },
      {
        time: "10:00",
        subject: "Chemisty",
        mode: "Tutorial"
      },
      {
        time: "11:00",
        subject: "Physics",
        mode: "Test"
      },
      {
        time: "13:00",
        subject: "Biology",
        mode: "Lecture"
      }
    ]
  },
  {
    name: "Tue",
    date: "11",
    lessons: "3 lessons",
    grade: "Grade 9 ",
    schedule: [
      {
        time: "9:00",
        subject: "Biology",
        mode: "Lecture"
      },
      {
        time: "10:00",
        subject: "Chemisty",
        mode: "Tutorial"
      },
      {
        time: "11:00",
        subject: "Physics",
        mode: "Test"
      },
      {
        time: "13:00",
        subject: "Biology",
        mode: "Lecture"
      }
    ]
  },
  {
    name: "Wed",
    date: "12",
    lessons: "5 lessons",
    grade: "Grade 8 ",
    schedule: [
      {
        time: "9:00",
        subject: "Biology",
        mode: "Lecture"
      },
      {
        time: "10:00",
        subject: "Chemisty",
        mode: "Tutorial"
      },
      {
        time: "11:00",
        subject: "Physics",
        mode: "Test"
      },
      {
        time: "13:00",
        subject: "Biology",
        mode: "Lecture"
      }
    ]
  },
],

},
     methods: {
      showSchedule(day) {
       this.selectedDay = day;
       this.displaySchedule = true;
     }
}

 })

Answer №1

After making some modifications, I eliminated the second loop and introduced a new property for each day called 'show'. This boolean property is utilized in a v-if statement to display the daily schedule.

new Vue({
 el: '#app',
 data: {
  schedule: false,
  days: [
    {
    name: "Mon",
    date: "10",
    lessons: "4 lessons",
    grade: "Grade 9",
    show: false,
    schedule: [
      {
        time: "9:00",
        subject: "Biology",
        mode: "Lecture"
      },
      {
        time: "10:00",
        subject: "Chemisty",
        mode: "Tutorial"
      },
      {
        time: "11:00",
        subject: "Physics",
        mode: "Test"
      },
      {
        time: "13:00",
        subject: "Biology",
        mode: "Lecture"
      }
    ]
  },
  {
    name: "Tue",
    date: "11",
    lessons: "3 lessons",
    grade: "Grade 9 ",
    show: false,
    schedule: [
      {
        time: "9:00",
        subject: "Biology",
        mode: "Lecture"
      },
      {
        time: "10:00",
        subject: "Chemisty",
        mode: "Tutorial"
      },
      {
        time: "11:00",
        subject: "Physics",
        mode: "Test"
      },
      {
        time: "13:00",
        subject: "Biology",
        mode: "Lecture"
      }
    ]
  },
  {
    name: "Wed",
    date: "12",
    lessons: "5 lessons",
    grade: "Grade 8 ",
    show: false,
    schedule: [
      {
        time: "9:00",
        subject: "Biology",
        mode: "Lecture"
      },
      {
        time: "10:00",
        subject: "Chemisty",
        mode: "Tutorial"
      },
      {
        time: "11:00",
        subject: "Physics",
        mode: "Test"
      },
      {
        time: "13:00",
        subject: "Biology",
        mode: "Lecture"
      }
    ]
  },
  

],

}

 })
<div id="app">
 <div v-for="day in days" :key="day.name" class="days">
  <button @click="day.show = !day.show"> {{day.name}} </button>
  <div v-if="day.show"> {{ day.schedule }} </div>
 </div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

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

Comparing elements in one array to elements in another array

In AngularJS, the $scope.categories array is populated from a multi-select element. $scope.categories = ["Adventure", "Strategy"] To compare this array with the categories in the items array below: $scope.items = [ { title: "Star Wars", ...

Enhance your website with a dynamic jQuery gallery featuring stunning zoom-in

I am currently working on a mobile website project and I'm in need of a gallery feature that allows users to zoom in on images and swipe through them using touch gestures. After some research, I haven't been able to find a suitable solution in j ...

Is there a way to eliminate the legend symbol for just one legend in Highcharts?

Looking to customize a legend in Highcharts but facing limitations due to Plot Lines and Bands not having legends. To work around this, I have added an empty series that acts as a toggle for showing/hiding plot lines. Since my plot lines are vertical, I im ...

What is the best way to perform calculations within a PHP loop for <input> elements and then display the results using a JavaScript loop?

Hello everyone, I'm currently struggling with displaying the calculations from a loop of input tags. What I'm trying to achieve is having 5 rows with input fields. At the end of each row, there should be a span area that displays the calculation ...

Strategies for resolving type issues in NextJs with Typescript

In my project using Next.js with TypeScript, I encountered an issue while trying to utilize the skipLibCheck = false property for enhanced checking. This additional check caused the build process to break, resulting in the following error: Error info - U ...

Using a .NET Web-API controller variable as a parameter in a JavaScript function

I am looking to send a "variable" from the controller to a JavaScript function. The code I have implemented is as below: <div ng-controller="faqController"> <div ng-repeat="c in categories"> <h2 onclick="toggle_visibility(&apos ...

Check for length validation including spaces in JavaScript

My code includes a functionality that calculates the length of characters in a text area using both JSP and JavaScript: <textarea class="textAreaLarge" id="citation" rows="20" cols="180" name="citation" ...

Exploring ways to assign a value to an HTML element utilizing Jquery in combination with ASP.NET MVC 4 complex model information

Within an ASP.NET MVC 4 view, I am utilizing data from a Model to populate various HTML elements. The model is used in the view to showcase values like: <div>@Model.Category.Name</div> etc... However, there is a specific div tag <div id="D ...

Initiating a Page with Dynamic Modal Window according to Backend Exigencies

Dear experts, can you help me with a problem? I want to implement a modal window on an vb.aspx page if a specific condition from the codebehind query is true. How can I achieve this? Thank you! ...

When using Javascript, you can expect to receive a modified structure that is different from

I am dealing with an array of objects that have the following structure: const data: [ { id: 21786162, label: "cBTC 2021-06-25 Put $50,000.00", active": true, type: "put", strike_price: 5000000, date_live: "2019-11- ...

Utilize Angular to inject an input from a component directly into the header of my application

I am looking to customize my Pages by injecting various components from different Pages/Components into the header. Specifically, I want to inject an Input search field from my content-component into the header Component. I initially attempted to use ng-Co ...

Having issues retrieving a JSON array in PHP with the json_decode function

Can someone assist me with passing and returning an array to a PHP script? I have successfully tested the json_encode portion, but I am facing issues with the json_decode on the PHP side. Javascript scid_list = []; $('.filter_on').each ...

Issue with updating onclick attribute of popover in Bootstrap 4

My goal is to design a popover in bootstrap 4 with a nested button inside it. However, when I update the data-content attribute of the popover with the button element, the element gets updated but the onclick attribute goes missing. I have experimented wi ...

Tips for implementing a shape divider in vuetify.js

Currently, I am incorporating the vuetify library into my project and attempting to include a shape divider similar to the one displayed in the image below. Unfortunately, I have been unsuccessful in achieving this desired effect. https://i.stack.imgur.c ...

The Google Maps geocoding service fails to provide accurate location information

I am currently attempting to utilize the Google Maps Geocoding API within my JavaScript code. Below is the snippet I have written: var geocoder = new google.maps.Geocoder(); function geocodeAddress() { var address = document.getElementById("address").v ...

The NodeJS application experiences a crash if incorrect parameters are provided to the API

Currently, I have built a CRUD API using TypeScript with Node.js, Express, and MongoDB. My goal is to ensure that the API functions correctly when the correct parameters are sent through a POST request. However, if incorrect parameters are passed, the Node ...

Vuex - Issue with Normalizr functionality not meeting expectations

I am currently in the process of developing a basic chat application. Within this app, I am dealing with three main entities: rooms, messages, and users. To simulate an API response, I am utilizing a fake API that provides data structured as follows: [{ ...

Achieving stylish CSS effects on dynamically loaded content post AJAX request

I am currently developing an application that utilizes AJAX to fetch JSON data and then uses an ES6 template literal to construct the view. It's a simple demonstration: let mountPoint = document.getElementById("mountPoint"); let view = document.cre ...

The animation freezes when trying to create geometry and mesh using Three.js

I'm currently working on developing a text animation that scrolls and dynamically adds more content as it runs. Here is the function I have created for generating the text geometry and mesh: function createText(){ textGeo = new THREE.TextGeometry( ...

What is the best way to pass route props using the <router-link> component?

Check out the button below that links to a specific route: <router-link class="q-pa-md" :to="{ name: 'Edit'}" id="item.id"> <q-btn outline>Edit</q-btn> </router-link> Take a look at my router se ...