When clicking the next or previous button in VueJS, showcase a list

I am looking to create a case management system in a JavaScript view. The system will track employees arriving and leaving a department, returning 3 objects (entries and exits) for the current week, next week, and the week after that. By default, the system displays the entries and exits for the current week. I would like to add two buttons, 'Previous' and 'Next', to allow users to navigate between the weeks. Is it possible to implement this using Vue JS?

<template>
    <div class="employee-list">
        <ul>
            <li v-for="employee in employees" :key="employee.id">
                <div class="employee">
                    <p class="name">{{ employee.name }}</p>
                    <p class="dept">{{ employee.dept }}</p>
                </div>
            </li>
        </ul>
    </div>
    <div>
        <button type="button" @click="selectemployee(employee)" class="btn btn-primary btn-lg">Previous</button>
        <button type="button" @click="selectemployee(employee)" class="btn btn-secondary btn-lg">Next</button>
    </div>
</template>

<script>
    export default {
        props: {
            employees: [
  {
    "inputs": [
      {
        "id": "38",
        "name": "Marcus",
        "dept": "Informatic Technology",
      },
      {  
        "id": "48",
        "name": "Dimitri",
        "dept": "Marketing",
      }
    ],
    ...

Could you please advise on how I can achieve this functionality?

Answer №1

If I have correctly understood your request, you can try using the following code snippet:

new Vue({
  el: "#demo",
  props: {
    employees: {
      type:Array,
      default: () => [
        {"inputs": [{"id": "38", "name": "Marcus", "dept": "Informatic Technology",}, {"id": "48", "name": "Dimitri", "dept": "Marketing",}], 
        "outputs": [{"id": "36", "name": "John", "dept": "Businnes",}, {"id": "82", "name": "Greg", "dept": "Marketing",}]}, 
        {"inputs": [{"id": "50", "name": "Emilie", "dept": "Informatic Technology",}, {"id": "15", "name": "Julian", "dept": "Marketing",}], 
        "outputs": [{"id": "60", "name": "Alan", "dept": "Informatic Technology",}, {"id": "19", "name": "Mireille", "dept": "Marketing",}]}, 
        {"inputs": [{"id": "71", "name": "Dupond", "dept": "Informatic Technology",}, {"id": "16", "name": "Dufourd", "dept": "Marketing",}], 
        "outputs": [{"id": "386", "name": "Tata", "dept": "Informatic Technology",}, {"id": "198", "name": "Toto", "dept": "Marketing",}]}
      ]
    }
  },
  data() {
    return {
      week: {id: 0, title: "current week"}
    }
  },
  methods: {
    employeesFilter(w) {
      return this.employees[w]
    },
    selectemployee(w) {
      this.week.id += w
      this.week.id === 0 ? this.week.title = "current week" : this.week.id === 1 ? this.week.title = "next week" : this.week.title = "week after"
    }
  }
})
.employees-list {
  flex: 2;
  max-height: 100%;
  height: 600px;
  overflow: scroll;
  border-left: 1px solid #a6a6a6;
}
.employees-list ul {
  list-style-type: none;
  padding-left: 0;
}
.employees-list ul li {
  display: flex;
  padding: 2px;
  border-bottom: 1px solid #aaaaaa;
  height: 80px;
  position: relative;
  cursor: pointer;
}
.employees-list ul li selected {
  background: #dfdfdf;
}
.employee {
  flex: 3;
  font-size: 10px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
p, ul {
  margin: 0;
}
.employee .name {
  font-weight: bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <div class="employee-list">
    <p>{{ week.title }}</p>
    <ul>
      <li v-for="(employee, i) in employeesFilter(week.id)" :key="i">
      <p>{{ i }}-{{ employee.length }}</p>
        <ul v-if="employee.length">
          <li v-for="empl in employee" :key="empl.id">
            <div class="employee">
              <p class="name">{{ empl.name }}</p>
              <p class="dept">{{ empl.dept }}</p>
            </div>
          </li>
        </ul>
        <p v-else>no people this week</p>
      </li>
    </ul>
  </div>
  <div>
    <button type="button" v-show="week.id > 0" @click="selectemployee(-1)" class="btn btn-primary btn-lg">Previous</button>
    <button type="button" v-show="week.id < 2" @click="selectemployee(1)" class="btn btn-secondary btn-lg">Next</button>
  </div>
</div>

Answer №2

To easily switch between weeks, keep track of the current week index and update it accordingly:

<template>
  <div>
    <div v-for="employee of weekEmployees">
      {{ employee.name}}
    </div>
    <button @click="prevWeek">Previous Week</button>
    <button @click="nextWeek">Next Week</button>
  </div>
</template>

<script>
export default {
  data () {
     return {
        week: 0,
        employees: [], // array containing employee data for multiple weeks
     }
  },
  computed: {
    weekEmployees() {
      return this.employees[this.week]
    }
  },
  methods: {
    nextWeek() {
      if (this.week >= this.employees.length - 1) { return }
      this.week++
    },
    prevWeek() {
      if (this.week <= 0 ) { return }
      this.week--
    }
  }
}
</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

Analyzing file data while uploading the file

Does anyone have a solution for extracting the content from an uploaded file in express/nodejs without saving it as a temporary file? I have code that successfully pipes the input to a filestream, but I'm struggling to deserialize the upload to a pla ...

Encountering serialization errors in Keras when trying to convert [ 0 1 2 ... 63] to JSON due to deep learning

I've been facing issues while trying to save my Neural Network as a ".h5" file using Keras. The problem arises when I attempt to serialize the model. The error message reads as follows: TypeError: Unable to serialize [ 0 1 2 3 4 5 6 ... 63] to ...

Can you tell me how to implement a shopping basket in vue.js that only appears when an item has been added to it?

After numerous attempts, I often find myself clearing everything and starting from scratch. How can I make this work? Observing and learning from others will greatly assist me! This area is designated for shopping cart items. If it's empty, nothing ...

Handsontable: How to update renderers when a row is deleted

Implementing Handsontable into our reporting system has been a success, except for one issue. I am using renderers to highlight error cells by setting the background color to red. However, when I remove a row using the context menu's "remove row" opti ...

Guide to displaying a template using Vue.js

Incorporating jQuery and vuejs version 2.3.4: https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js The code snippet below demonstrates my current setup: // Instantiating Vue. vueVar = new Vue({ el: '#content', ...

The array is not preserved by Mongoose once the .map(...) loop has ended

I'm encountering an issue where every time the Node.js quits the loop of .map(...), my Post.attachments array reverts back to the state of []. I'm curious as to why this behavior occurs. Below is the code snippet: req.files.map(async (file) => ...

What are the implications of adding custom attributes to HTML elements?

Similar Questions: Custom attributes - Good or Bad? Non-Standard Attributes on HTML Tags. What are Your Thoughts? While working on a current learning project, I encountered the need to add an attribute that would store a numerical value. Initially ...

The declaration for "Control" is not present, possibly being restricted by its protection level

I'm really struggling to get the jQuery datepicker working in ASP.NET. I've tried various examples, but nothing seems to work for me. Even though I'm fairly new to ASP.NET, I am learning quickly! Here is the script I am trying to use: < ...

The background color of absolute divs fails to extend across the entire screen

Trying to implement a loading screen in my VueJs app using only divs. The issue is that the background color of the divs (loadingScreen) does not cover the entire screen, but only as much as the loader's height. I've attempted adding margin to th ...

Utilizing nested JSON arrays within AngularJS for complex data structures

I'm a beginner in working with Angular and JSON, so any help would be greatly appreciated. I have a JSON array that contains information about users and the sellers from whom they have made purchases. JSON: [ { "name":"Kakaro", "sallers": ...

Display the option to "Delete" the link only when there are multiple images in my image collection

I am using jQuery and Ajax to remove banner images from my website. However, I want to make sure that if there is only one image left, it cannot be deleted. Each image in the list has a corresponding delete link: echo '<a class="delete j_bannerd ...

Declaring a Javascript variable within an if statement does not alter the value of the global variable

Currently, I am working on enhancing my HTML projects by using JavaScript to modify the video source. Below is the video element in question. <div> <video id="songVid" onmouseover="controlsVid()"> <source src=&qu ...

Error: Recaptcha does not have a constructor method

Out of nowhere, my application suddenly started throwing a TypeError: Recaptcha is not a constructor. recaptchaConfig() { this.recaptcha = new Recaptcha( config.service.recaptcha.client_key, config.service.recaptcha.sec ...

Send the td value to a PHP script with the help of JavaScript

I have an HTML table that displays records from a database. Below is a screenshot of my table There is a button in a TD (i.e column 5,7,9), when I click the button I want to perform a function to display a popup box with an HTML table. Before that, I wa ...

"Utilizing AJAX for Data Retrieval in a Form with Dynamically Generated

Hey there, I'm trying to figure out how to make my ajax data call dynamic. Here's what I've attempted: var opt_name = $(".NAME").data('opt_name'); var opt_business = $(".BUSINESS").data('opt_business&ap ...

Clicking on an image in a jQuery autocomplete menu will trigger a data post to an Express

My jquery autocomplete menu is functioning properly, displaying a list of books with author, title, and book image. I am now looking to enhance it by allowing users to click on the book image and then have the book title posted to an express app.post metho ...

Transforming text into an image during an Ajax response

Currently, I am in the process of learning jQuery and experimenting with a list of hotels that have star ratings retrieved through an Ajax call. My goal is to transform these text-based ratings into images of stars, but I'm facing some difficulties in ...

Escaping quotes in JavaScript

After receiving a JSON object in the following format: result: { image: "..." title: "text text \"text\"" } I am currently utilizing underscore.js to render the template, but I am encountering an issue where the title displays with the escape ...

The API fails to update with new data after every request

I have encountered an issue while working on my API project. The API is developed in DRF and the response is being shown on a Vue project. The problem arises when the API needs to refresh after each request (browser refresh). Initially, I display all the ...

Issue with Inline JavaScript in `href` tag not functioning correctly in Firefox

I am encountering an issue with this inline JavaScript not working in Firefox. I need to figure out how to make it function correctly in Firefox. <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <style> ...