"Failure to manipulate the style of an HTML element using Vue's

        <vs-tr :key="i" v-for="(daydatasT, i) in daydatas">
          <vs-td>{{ daydatasT.Machinecd }}</vs-td>
          <vs-td>{{ daydatasT.Checkdt }}</vs-td>
          <vs-td>{{ daydatasT.CheckItemnm }}</vs-td>
          <vs-td>{{ daydatasT.CheckPart }}</vs-td>
          <vs-td :id="[`d${i}`]">
          </vs-td>
          <vs-td></vs-td>
          <!-- <vs-td>{{ Checker }}</vs-td> -->
          <!-- <vs-td>{{ tr.CheckUser }}</vs-td> -->
          <vs-td>{{ daydatasT.Remark }}</vs-td>
        </vs-tr>

const vm = this;
    axios
      .post("~~~")
      .then((response) => {
        // console.log("response.data : " + JSON.stringify(response.data));
        vm.daydatas = response.data;
        // console.log(response.data[i].Checkresult);
        for (let i = 0; i < 10; i++) {
          if (response.data[i].Checkresult == "1") {
            document.getElementById(`d${i}`).style.background = "#e96666";
            document.getElementById(`d${i}`).style.color = "#ffffff";
          } else {
            document.getElementById(`d${i}`).style.background = "#ceecc5";
            document.getElementById(`d${i}`).style.color = "#176f24";
          }
        }
      })
      .catch((error) => {
        console.error(error);
      });

If I change the ID, it works, but the styling does not apply to the IDs created by the for loop. While checking if the names were different using the console, they appear to be the same. Why is the styling not working as expected?

Answer №1

If you are working with Vue 2, the following code snippet should help resolve your issue:

<vs-tr :key="i" v-for="(daydatasT, i) in daydatas">
          <vs-td>{{ daydatasT.Machinecd }}</vs-td>
          <vs-td>{{ daydatasT.Checkdt }}</vs-td>
          <vs-td>{{ daydatasT.CheckItemnm }}</vs-td>
          <vs-td>{{ daydatasT.CheckPart }}</vs-td>
          <!-- reactive -->
          <vs-td :style="stylesTd[i]">
          </vs-td>
          <vs-td></vs-td>
          <vs-td>{{ daydatasT.Remark }}</vs-td>
        </vs-tr>
export default {
  ...
  data: {
    ...
    styles: []
  },
  methods: {
    ...
    <nameMethod>() {
      axios
      .post("~~~")
      .then((response) => {
        this.daydatas = response.data;
        this.styles.splice(0);
        this.styles.push(
          ...response.data.map(item => {
            if (item.Checkresult)
              return {
                background: "#e96666",
                color: "#ffffff"
              };
            return {
              background: "#ceecc5",
              color: "#176f24"
            };
          })
        );
      })
      .catch((error) => {
        console.error(error);
      });
    }
  }
}

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

When accessing the JavaScript date object, no output is being returned

I have a calendar layout consisting of floated div elements, rather than using an actual <table>. My goal is to allow the user to click anywhere on a row to add a new booking. The challenge lies in accurately calculating the time at which they clicke ...

Change prompt-sync from require to import syntax

In my Node project, I have integrated the prompt-sync module. const prompt = require('prompt-sync')(); const result = prompt(message); To maintain consistency in my TypeScript code, I decided to switch from using require to import. In order to ...

Discover the sub strings that fall between two specified regular expressions

I am looking for a way to extract substrings from a string that are between two specified regex patterns. Here are a few examples: My $$name$$ is John, my $$surname$$ is Doe -> should return [name, surname] My &&name&& is John, my & ...

Tips for ensuring that a server waits until a database connection is successfully established using node.js, MongoDB, and Express

I have a server.js file and a database.js file In the server.js file, I have the following code: const express = require('express'); var db = require('./database'); const app = express(); . . some get/post methods app.listen(3000); . ...

Every time I employ window.location, the Iframe becomes nested

I am experiencing an issue with my HTML structure: <html> <body> This is the container of the iframe <iframe src="/foo.html"> </iframe> </body> </html> (/foo.html) <html> <script type="text/javascript"> $( ...

Displaying or concealing an HTML element based on a JavaScript XHR request function

I have a single HTML element that I need to display and hide using jQuery within a pure JavaScript XHR request method. Currently, the element always remains visible without hiding when the request is complete or successful. On error, I would like it to be ...

Tips on retrieving the image URL from Aws s3 and saving it in mongodb

Can anyone help me with retrieving the URL location of an image from AWS s3 and storing it in MongoDB using mongoose? Currently, when I try to console log the req.file.location value, it shows up as undefined. I have also attempted to console log uploadFil ...

Discovering active path while utilizing dynamic routes - here's how!

Whenever I click on the project element in my HeadlessCMS with the URL /projects/slug-name, the projects button in my Navbar (LinkItem) does not highlight with a background color (bgColor). How can I modify this line of code: const active = path === href / ...

Using React and Material UI to create a table filtering system with checkboxes

I'm currently developing a filtering feature using checkboxes for a data display in a project utilizing React and Material-UI. Is there a custom solution available within Material-UI? If not, what steps should I take to achieve this? As I am rel ...

Verifying file types with HTML5 drag and drop feature

Is it possible to change the drop zone's background color to green or red based on whether the dragged payload contains supported file types (JPEG)? Do Gecko and Webkit browsers have the ability to determine the file type of drag and drop files? ...

Field must have a base type specified to proceed

Currently, I am in the process of developing a discord.js bot. The structure I have set up involves a folder that contains all the commands, and when a command is called, index.js directs it to the appropriate file. However, when attempting to run the bot, ...

Show random images of different sizes by employing jquery

I am seeking a solution for my webpage which currently loads a random image from an array and centers it using negative margins in CSS. While this method works fine with images of the same size, I am looking to modify the code so that it can handle images ...

Customize specific styles for individual divs one at a time (without the use of jQuery)

Can you assist me with this issue? I am trying to display the <span class="badge badge-light"><i class="fa fa-check-circle"></i></span> tag (initially set to "visibility: hidden") when clicking on a div with the class "role-box". He ...

How to organize and reuse code efficiently with Node.js, EJS, and front-end JavaScript?

It seems that I may have chosen the wrong platform by posting this question on the 'Software Engineering' exchange: Currently, my focus is on learning the MEAN stack (I have yet to dive into Angular, so for now I am using pure vanilla JS for the ...

Exploring Event Listeners in the World of JavaScript and/or jQuery

Is there a more sophisticated way to monitor the execution of a specific function in JavaScript or jQuery? Instead of waiting for an event like $('#mything').click(function(){ //blah }), I prefer to be notified when a particular function is trig ...

Getting the initial date of the upcoming month in JavaScript

I'm trying to figure out how to get the first day of the next month for a datePicker in dd/mm/yyyy format. Can anyone help? Here's the code I currently have: var now = new Date(); if (now.getMonth() == 11) { var current = new Date(now.getFu ...

Transform an asynchronous callback into an asynchronous generator format

I have the following function from a third-party package that I am unable to modify async function runTransaction(callback) { const client = await createClient(); try { await client.query("BEGIN"); await callback(client); } ...

What is the best way to incorporate a subcategory within a string and separate them by commas using Vue.js?

Is it possible to post subcategories in the following format now? Here is the current result: subcategory[] : Healthcare subcategory[] : education However, I would like to have them as a string separated by commas. This is my HTML code: <div id="sub ...

What could be causing my handle button to slide off the timeline towards the right?

I'm facing an issue with my volume bar component where the slider button is rendering outside of the timeline instead of on top of the progress bar. I need assistance in adjusting its position. // Here is the code for my volume bar component: import ...

What's the best way to make a toast notification appear when an API call is either successful or encounters

Seeking guidance on incorporating toast messages within an Angular + Ionic 6 application... My goal is to display a toast message in response to events such as clearing a cart or submitting an order, with the message originating from an API call. While a ...