"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);
      });

https://i.sstatic.net/BgEYe.png

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

Combining the devexpress dxDataGrid with Angular's $scope for seamless web development

I'm encountering difficulties with binding $scope in angular and dxDataGrid. Utilizing the devexpress library dx.all.js, which enhances the dxDataGrid with various features, I have a div for dx-data-grid and attempting to transfer the selected row da ...

What is the best way to bring a nested object to the top level without deleting the original top level

Imagine having the following dataset: data = [{ "_id" : "2fApaxgiPx38kpDLA", "profile" : { "name" : "Karina 1", "avatar" : "avatar1.jpg", "bio" ...

Having trouble creating a full-screen modal with NgbModal by passing content as a component?

I've been using Bootstrap widgets and trying to create a full-screen modal where the header sticks on top, the footer stays at the bottom, and the body scrolls in the middle. Initially, I found a simple HTML solution for this: However, when I want to ...

Load the content of the dialog and transfer variables

After struggling for days, I am still unable to find a solution to my current dilemma. In my database, there are approximately 1300 items each with its own unique "id", a corresponding "name", and a property called "enabled". My goal is to display links t ...

What could be causing the lack of data to be returned by jQuery.getJSON?

I've come across this method: function getUserName(guid) { var name = "Unknown"; $.getJSON(urlCurrent, { "method" : "get_user_info", "guid" : guid, "auth_token" : temporaryAuthToken }, function(data) { if ...

Mix up and present cards for a game of blackjack (Javascript)

Have you noticed why "card2" is randomly adding an object to the array? It should consistently add objects to the array. const cards=[ { card: '&#127137', value: '1' }, { card: '&#127138', valu ...

Tips for incorporating filtering and sorting functionality in a API-focused application using React and Next.js

In my current project, I am developing a React application using Next.js. The main goal is to fetch data from an API and display cards based on user-selected filters. Specifically, I aim to retrieve the cards initially and then filter them according to the ...

Opting for css3 translate over using position: left for positioning elements

Why Using translate() for Element Movement is Superior to Pos: abs Top/left Read the full article Lisa Anderson I struggled with implementing a CSS3 translate animation, so I opted for a jQuery solution that achieves the same effect. Click on the X icon . ...

Check that EACH radio button has been either selected or left unselected when the button is clicked

I'm still learning Javascript and currently working on a function triggered by a button click. My goal is to display an alert if NONE of the radio buttons have been selected, regardless of whether they are "Yes" or "No." If ALL radio buttons have been ...

The system is unable to locate the command "nvm"

Lately, I've been experimenting with using nvm to manage different versions of node. After successfully installing nvm on my Mac OS Catalina(10.15.6), I was able to easily switch between versions through the terminal. However, when attempting to do t ...

Error in Node JS: When attempting to use "require", a ReferenceError is thrown

After deciding to utilize a MySQL database, I proceeded by installing MySQL using the command npm i mysql. Following the installation, I included the line of code below in order to begin utilizing it: var mysql = require('mysql'); However, upon ...

Modifying tag classes dynamically with JavaScript

I am working on creating a list of projects where the user can select one as the default project for future use in other processes. The issue I am facing is that each project in the list has a unique id and by default, they all have the RegularIcon class ...

Is it possible for a Vuex module to observe or monitor another Vuex module?

Is it possible for Vuex modules to monitor the state of other modules and accordingly trigger actions? Consider this scenario: store.js import time from './store/time' ; import position from './store/position' ; const store = new Vu ...

VueJs - Adding visual indicators to distinguish the active tab and emphasize the initial tab upon page load

I typically have a set style for highlighting tabs, but this time I decided to try something different. Below is the approach I took: TabPage.vue **template** <div class="tab" @click.prevent> <ul> <li> <a class ...

Exploring the Shopware 6 Admin API: Saving a new entity with a default entity property

How can I set a default value for a new entity property (such as defaultCurrency.id) within the sw-entity-single-select component before calling this.repository.save()? Is this possible, or do I have to wait until after calling save() and then listen for ...

The journey of data starting from a PHP file, moving through JavaScript, and circling back to PHP

I've encountered an interesting challenge with my PHP file and Wordpress shortcode. It all starts when the shortcode is embedded in a webpage, triggering a php function from within the file. This function executes an SQL query to extract data, then s ...

Achieve an overlapping effect between absolutely positioned sibling divs without the need to specify a background color

Exploring the development of a swipe-to-action list component. <div class="list-container"> <div class="row"> <div class="content"> Sample Content 1 </div> <div class="action"> ...

What is the best method for integrating UL / LI into JSON to HTML conversion?

Currently, I am working on converting a JSON string into HTML format. If you want to take a look at the code, here is the jsfiddle link: http://jsfiddle.net/2VwKb/4/ The specific modifications I need to make involve adding a li element around the model da ...

Fixing TypeError: Object #<IncomingMessage> has no method 'flash' in ExpressJS version 4.2

Currently, I am utilizing ExpressJS 4.2 and PassportJS for authenticating local users. Everything seems to be working smoothly except for when attempting to display a failureFlash message. Below is my configuration setup, thank you in advance! ==== Necess ...

Is it considered acceptable to modify classes in a stateless React component by adding or removing them?

My stateless component functions as an accordion. When the container div is clicked, I toggle a couple of CSS classes on some child components. Is it acceptable to directly change the classes in the DOM, or should I convert this stateless component to a ...