Enhance the appearance of your Vuejs application with conditional style formatting

I want to enhance a basic table by applying conditional CSS styles to the cells based on their values. For example:

If area1 == 'one', then assign the td-one CSS style.

      <tr v-for="version in versions" :key="version.id">
        <td>{{version.name}}</td>
        <td>{{version.area1}}</td>
        <td>{{version.area2}}</td>
        <td>{{version.area1}}</td>
        <td>{{version.area4}}</td>
      </tr>

Script:

<script>
export default {
  name: 'versions-table',
  props: {
    msg: String
  },
  data () {
    return {
      versions: [
              {
                id: '1',
                name: 'Name1',
                area1: 'one',
                area2: 'one',
                area3: 'two',
                area4: 'three'
              }
           ]
        }
    }
}

CSS

<style scoped>
  table {
    margin-left: auto;
    margin-right: auto;
    padding: 10px;
  }
  td {
    padding: 5px;
  }

  .td-one {
    background-color: green;
  }

  .td-two{
    background: red;
  }

  .td-three{
    background-color: blue;
  }
</style>

Answer №1

To implement class binding, you can refer to class binding documentation.

<td :class="{'td-one':version.area1 == 'one'}">{{version.area1}}</td>

Continue adding similar bindings as needed.

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 you click on the collapsible header background, the content collapses, but the main icon does not update

I am currently using collapsible content from Bootstrap and I am encountering an issue with the icon not changing when I click on the collapsible-header background. Here is a visual representation of the problem: 1st part (not collapsed) 2nd part (After ...

Sails.js seems to be malfunctioning, as it does not seem to be recognizing the term 'sails'

It seems like I'm encountering an issue with the 'sails' command not being recognized on my Windows 10 system. Despite following all the installation steps, including globally installing Sails.js through npm and ensuring Node is installed, I ...

What is the process for updating the internal TypeScript version in VS Code?

When using VS Code, I noticed it comes with its own TypeScript version: Is there a way to update this? The current version is 4.9.3. ...

Modify CSS using JavaScript at a specific screen size

Currently, I am in the process of designing a responsive navigation system which involves implementing a button that dynamically changes the styling of the div #navigation using Javascript. The aim is to toggle between display: none; and display: block; ba ...

`How can I retrieve environment variables in React or inject them into a React application?`

I attempted to include the following code in the plugins section of my webpack configuration, but it resulted in an unusable build. It's worth noting that I am not using create-react-app. new webpack.DefinePlugin({ 'process.env': dotenv. ...

typescript what type of functionality do dynamic class methods provide

I'm currently working on building a class that creates dynamic methods during the constructor stage. While everything is functioning properly, I've encountered an issue with VS Code's auto suggestion not recognizing these dynamic methods. Ho ...

Retrieve JSON Object using a string identifier

I created a script that takes the ID of a link as the name of a JSON dataset. $('.link').click(function() { var dataset = $(this).attr("id"); for (var i = 0; i < chart.series.length; i++) { chart.series[i].setData(lata.dataset ...

Switching Background Colors with CSS

Can you change background colors using only CSS3? I attempted to use keyframe animations, but they only transition the background color. I simply want the background color to change after 5 seconds with no transition or hover effects. If I can fade it in ...

Converting Emoji to PNG or JPG using Node.js - What's the Procedure?

Currently, I am working on a project that requires me to create an image file from emoji characters, preferably using Apple emoji. Initially, I thought this task would be simple to accomplish, but I have encountered obstacles with each tool I have tried. ...

Is there a way to merge these arrays into a single array?

With the current code I am obtaining numerous JSON objects within separate arrays: Here is the code snippet: for (let i=1; i<=150;i++){ fetch(`A valid URL ${i}`) .then(result => result.json()) .then(result => console.log(result.data.results)) ...

I am puzzled as to why I keep receiving the error message "Cannot read property 'poPanel' of undefined"

CSS In my project, I am implementing a feature that displays an ordered list by looping through an array of objects and adding them on a button click. It works smoothly for adding items, but when I try to implement a remove function to delete each item, I ...

Implementing Pagination in Vue: How to Make it Work with Response Data

I am looking to integrate pagination from the response data into my existing code, while also incorporating filters. JavaScript var entriesList = new Vue({ el: "#post-list-template", data: { posts: [], categories: [], cu ...

Utilizing getElementsByClassName for web scraping: encountering inaccurate outcomes

I am attempting to extract the inner text from all classes with the className = "disabled" within the provided snippet of HTML Code: HTML code In my effort to achieve this task using MS Access (VBA), the code I have implemented is as follows: Set IE = C ...

Using the <a> </a> tag for navigating within the code logic

Currently, I am utilizing the <a> tag for clicking and navigating, as shown in the code snippet below. I am looking to navigate this code using the C# tag in the page_prerender event, based on the selected id. Can anyone offer assistance with this? ...

Apache causes HTML download tag to malfunction

I have an HTML file that includes the following code: <a href="/Library/WebServer/Documents/file.zip" download="file.zip"> Download here </a> When I test this HTML page on Chrome, it successfully allows me to download the file. However, when ...

The header and navigation bar are both set to stay in place, but unfortunately my div element is not displaying beneath

After setting both the Nav and Header to fixed, everything seems to start out well. I adjust the Nav margin-top so that it appears below the header, which works fine. However, things take a wrong turn when I try adjusting the div. The div ends up behind th ...

How can we determine if the return value from Object.values is an array or not, since it returns a JavaScript array without the brackets?

In my programming code, I work with two separate JSON files. I iterate through each item in the first file and compare its values with those in the second file. Based on the comparison results, I generate a third JSON file which essentially merges the cont ...

Customizing the date format in Vuetify's Datepicker

I am currently using a Vuetify Datepicker component in my project: <v-menu v-model="menu1" :close-on-content-click="false" max-width="290" > <template v-slot:activator="{ on }"> <v-text-field v-model="editedItem. ...

WebStorm raises an error saying "SyntaxError: Plugin '@typescript-eslint' failed to load as declared in '.eslintrc.cjs' » '@vue/eslint-config-typescript'."

Encountering an issue in WebStorm where every ts file in the project displays the error message: SyntaxError: Failed to load plugin '@typescript-eslint' declared in '.eslintrc.cjs » @vue/eslint-config-typescript': Unexpected token &a ...

The problem with clearing floats in IE7 (as well as 6)

Currently, I am facing an issue where I have a list that I want to split into three columns by using the float left property and then clearing the first column. Everything seems to be working fine in IE8 and FF, however, IE7 is causing the second column t ...