Surprising responsiveness discovered in Vue.js

As part of my college project, I am tasked with developing a Vue.js application for managing home automation. The backend system is a basic json server that stores data for each room, including time slots.

These time slots determine the status of specific elements in a room within a given timeframe (such as temperature).

To display each time slot for a particular element in a room, I am utilizing a v-data-table component. However, when it comes to representing the boolean values (true or false) as 'open' or 'closed' in the table, I encountered some unexpected issues.

When attempting to use the array map function to replace true/false with open/closed, the behavior was not as expected. The new array always displayed 'open' irrespective of the original value and the values in the json server also got updated to 'open' inexplicably.

Below is the code snippet for the data table:

<v-data-table
        v-if="room"
        :headers="headers"
        :items="room.timeslots.filter(timeslot => {return timeslot.element == 'cur'}).map(timeslot => {if(timeslot.value) {timeslot.value='open'} else {timeslot.value='gesloten'} return timeslot;})"
        :custom-sort="customSort"
        hide-default-footer
        class="elevation-1 ma-5"
      >
        <template v-slot:[`item.actions`]="{ item }">
          <v-icon @click="deleteTimeslot(item.id)">mdi-delete</v-icon>
        </template>
        <template slot="no-data">
            Voor dit element zijn er nog geen tijdssloten ingesteld.
        </template>
      </v-data-table>

Here's an example of sample data from the json server:

{
    rooms: [
        {
      "id": 2,
      "name": "Bureau",
      // Other room details...
      "timeslots": [
        {
          "id": 1,
          "start": "12:00",
          "end": "13:00",
          "value": "21",
          "element": "temp"
        },
        {
          "id": 4,
          "start": "03:20",
          "end": "04:20",
          "value": true,
          "element": "cur"
        },
        {
          "id": 5,
          "start": "14:00",
          "end": "15:00",
          "value": false,
          "element": "cur"
        }
    ]
}

I would greatly appreciate if someone could shed light on this peculiar behavior and provide guidance on how to rectify it.

Answer №1

It caught my attention why the map function seemed to be altering data when in fact it is meant to be non-altering.

The reason for this confusion is that I mistakenly treated it as an altering method, which goes against best practices.

Here's a proper implementation:

<v-data-table
   v-if="room"
   :headers="headers"
   :items="room.timeslots.filter(timeslot => {return timeslot.element == 'cur'}).map(timeslot => {return {...timeslot, value: timeslot.value ? 'open' : 'closed'}})"
   :custom-sort="customSort"
   hide-default-footer
   class="elevation-1 ma-5"
>
   <template v-slot:[`item.actions`]="{ item }">
        <v-icon @click="deleteTimeslot(item.id)">mdi-delete</v-icon>
   </template>
   <template slot="no-data">
        No time slots have been set up for this element yet.
   </template>
</v-data-table>

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

NodeJS - The server returns a 404 error before ultimately displaying the requested page

I'm having trouble with my nodeJS application. When I make an asynchronous call using ajax, the server first responds with a 404 error before loading the page. The application functions properly, but I keep receiving repetitive logs stating "Can' ...

How to extract multiple values from a multidimensional array returned by a PHP function parameter

Having trouble with this code? Here's the full snippet: function logRequest($currIP){ include("include/opendb.php"); $gets = $_SERVER['QUERY_STRING']; $posts = http_build_query($_POST); $ref = $_SERVER['HTTP_REFERER']; $agent = ...

Revamp the Twitter button parameters or alter its settings

I'm working on a web page that includes a Twitter button. Before the button, there is an input field and a form where users can easily add relevant hashtags for the page. The goal is to take the text from the input field and populate it in the Twitter ...

Ways to identify if there is a problem with the Firestore connection

Can anyone help me understand how I can use angularfire2 to check the accessibility of the cloud firestore database and retrieve collection values? If accessing the cloud database fails, I want to be able to retrieve local data instead. This is an exampl ...

Looping through each combination of elements in a Map

I have a Map containing Shape objects with unique IDs assigned as keys. My goal is to loop through every pair of Shapes in the Map, ensuring that each pair is only processed once. While I am aware of options like forEach or for..of for looping, I'm s ...

The Javascript class is throwing a syntax error because it's not okay

I'm using this code snippet: Class Addition { constructor () { } add (a = 1, b = 1) { console.log(a+b) } } but it's giving me an error Uncaught SyntaxError: Unexpected identifier ...

Hierarchy-based state forwarding within React components

As I embark on the journey of learning Typescript+React in a professional environment, transitioning from working with technologies like CoffeeScript, Backbone, and Marionettejs, a question arises regarding the best approach to managing hierarchical views ...

Creating interactive JSON objects through the use of JavaScript and AngularJS

When using AngularJS to build a dynamic JSON from server data, I encountered an issue where my current declaration only works if the server data contains one item in the object array. How can I modify this to handle multiple items dynamically? $scope.it ...

Leveraging the Composition API in Vue 3: Implementing reusable code from a separate file into the template

I have developed a reusable code snippet in dateTime.js: import { ref, computed, watch } from 'vue'; import * as dayjs from 'dayjs'; export default function dateTime() { const newDateTimeString = ref(null); function showDateT ...

Issue detected in React Rollup: the specific module 'name' is not being exported from the node_modules directory

Currently in the process of creating a library or package from my component. The tech stack includes React, Typescript, and various other dependencies. Encountering an error while using Rollup to build the package: [!] Error: 'DisplayHint' is ...

Attempting to transmit JavaScript information to my NodeJS server

Having some trouble sending geolocation data to NodeJS through a POST request. When I check the console log in my NodeJS code, it's just showing an empty object. I've already tested it with postman and had no issues receiving the data. The probl ...

Is it possible for the Chrome mobile camera to take up the full screen size on

Currently, I am using the code below to access the camera and display the stream. The width of the element is 100%, but the height seems to be around 70%. Is there a better way to make it fill the entire screen? HTML <video autoplay class="screen"> ...

What is the most efficient method to swap out an element in an array of models?

If there is an Array containing instances of a User class defined as follows. class User { var id: String var name: String init(id: String, name: String) { self.id = id self.name = name } } Assume we have 5 user models with i ...

What is the best way to make a prevent default function for an input field?

In the case where a user inputs numerical values, only even numbers should be displayed, with odd numbers being prevented by default. HTML: Input any number: <input type="text"> <p id="even"></p> JavaScript: function ...

How to refresh an array in Angular 4 after inserting a new element using splice method?

I have a Angular list displayed in a table, and I need to insert an element at a specific position. I have tried using the following code: array.splice(index, 0, element); While everything seems fine in the console with the element being added at the corr ...

Python and Javascript clashing with one another

(Updated question for clarity). I am currently developing a flask app game that involves users inputting guesses via the web browser. The backend, which runs on Python, checks these guesses and provides feedback to the user about their accuracy. Additional ...

Best practices for selecting a checkbox with Selenium in JavaScript and Xpath

I'm currently facing a challenge in targeting a checkbox to agree to the terms and conditions. Unfortunately, there is no name or id that I can use for targeting. I have been attempting to use innerHTML and innerText in Selenium with JavaScript on Chr ...

What causes the lack of impact on lambda rendering speed despite integrating webpack?

Hey there, I've been working on implementing webpack for a project that involves microservices, Node.js, TypeScript, AWS, and AWS SAM. My main objectives are: Reduce the cold start time of lambda functions. Minimize security vulnerabilities by e ...

Steps for generating a hierarchical menu utilizing data from a CSV file

I am looking to use a CSV file to structure the menu on my webpage. Could someone assist me in creating a nested menu using JavaScript with the provided CSV data? The columns consist of: Level, Menu name, URL 0;"Service"; 1;"Service 1";"http://some-url- ...

Error encountered while utilizing MUI Button: Unable to access property 'borderRadius' from an undefined source

import React, { Component } from 'react'; import './App.css'; import Screen from './components/Screen/Screen'; import Button from './components/Button/Button'; import { MuiThemeProvider, createMuiTheme } from 'm ...