Customize Vue Element UI table cell appearance depending on the data

I am utilizing Element UI for my project. How can I dynamically change the color of the rate cell value in the table? I want the color to be red when rate is less than 0, and green when it is greater than or equal to 0.

I wonder if the table attribute cell-class-name can assist me with this task?

HTML

<el-table
    :data="tableData"
    style="width: 100%">
    <el-table-column
        prop="date"
        label="Date"
        width="180">
    </el-table-column>
    <el-table-column
        prop="name"
        label="Name"
        width="180">
    </el-table-column>
    <el-table-column
        prop="rate"
        label="Rate">
    </el-table-column>
</el-table>

JS

export default {
    data() {
        return {
            tableData: [{
                date: '2016-05-03',
                name: 'Tom',
                rate: 1
            }, {
                date: '2016-05-02',
                name: 'Tom',
                rate: -1
            }, {
                date: '2016-05-04',
                name: 'Tom',
                rate: 22
            }, {
                date: '2016-05-01',
                name: 'Tom',
                rate: -22
            }]
        }
    }
}

Answer №1

Attach a function to the cell-class-name attribute of <el-table>:

<el-table
  :data="tableData"
  style="width: 100%"
  :cell-class-name="applyClass"
>

Create the applyClass function to check the values in the rate column and add a class to the cell accordingly:

methods: {
  applyClass({ row, column }) {
    if (column.property === 'rate') {
      const value = row[column.property];
      if (value > 0 ) {
        return 'positiveRate'
      } else {
        return 'negativeRate'
      }
    }
  }
}

CSS

.positiveRate {
  background: green;
}
.negativeRate {
  background: red;
}

Answer №2

Utilize a table column template

<el-table-column prop="rate" label="Rate">
      <template #default="scope">
          <label class="scope.row.rate < 0 ? 'danger' : 'success'">{{scope.row.rate}}</label>
      </template>
 </el-table-column>

Resource

Modify:Resource

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

Is there any benefit to making the SVG elements width and height 100%?

The Angular Material documentation app features an SVG Viewer that is able to scale the SVG content to fit the container using the following function: inlineSvgContent(template) { this.elementRef.nativeElement.innerHTML = template; if (this.sca ...

Display a modal when clicking on a bootstrap glyph icon

I tried following a tutorial on how to create a Bootstrap modal at https://www.w3schools.com/bootstrap/bootstrap_modal.asp However, I would like to use a glyph icon in the code snippet like this: <span class="glyphicon glyphicon-pencil" class="btn btn ...

Tips for maintaining a fixed size for CSS grid cells' minimum and maximum dimensions

I am attempting to utilize CSS grid to create an entirely fixed layout. This layout is for an embedded application that contains both GUI elements and text fields. The sizes of the cells should remain unchanged and not readjust as their content grows or sh ...

Is it possible to turn a <span> element into a clickable hyperlink?

Can a span be turned into a clickable link? I have successfully made a span with only a background image (using the Gilder/Levin image replacement technique) into a clickable link. This seems to work well on my desktop in Chrome, Opera, and IE 11. Is thi ...

Struggling with the grid layout in percentage and feeling perplexed

As I delve into the world of grid systems, I find myself grappling to comprehend it fully. The complexities can sometimes leave me feeling inadequate in my understanding. From what I gather, a 12-column grid system without any margins means that the 12th ...

Need help aligning content with flexbox? Having trouble with align-content and justify-content not working correctly?

First and foremost, I want to express my gratitude in advance for any assistance you can provide. My goal was to neatly align three pieces of content within each row, similar to the image displayed here: https://i.sstatic.net/vtsRj.png To achieve this lay ...

Creating a full-width input field with text aligned to the right:

.wrap { white-space: nowrap; } input { width: 100%; } body { /* just so you can see it overflowing */ border: 1px solid black; margin: 40px; padding: 10px; } <span class="wrap"><input type="text">.pdf</span> Observing the cod ...

Issue with Navbar collapse button not displaying items on Bootstrap 4.5.0 in combination with Next.js version 9.4.4

I'm currently working on a nextjs demo project where I'm incorporating bootstrap for styling purposes. The initial setup was straightforward as I installed bootstrap using npm and included it in my app.js. import 'bootstrap/dist/css/bootstra ...

Learn how to create a horizontally scrollable ToggleButton using MUI when the ToggleButtonGroup exceeds the screen width

Looking to enhance the responsiveness of my web design project, I aim to create a horizontally scrollable ToggleButton using Mui. The goal is to have the buttons adjust when the screen width becomes too narrow to display all three buttons at once, allowing ...

The backdrop takes precedence over all other elements

The issue I'm facing is that the other Div is not appearing above the background. My goal is to have the background fill the entire screen. <title>The Ultimate Cube World Resource!</title> <link href="style.css" rel="stylesheet" type=" ...

What is a practical method for achieving a double-lined border effect?

Here is what I am referring to (with two different colors): I had to create two span divs and apply a border-right on one and a border-left on the other as my solution. However, I believe there must be a more efficient way to do this with less code. HTML ...

The annoying Facebook "add a comment" popup refuses to close

Occasionally, the "add a comment" popup (iframe) in Facebook's Like plug-in fails to close and obstructs access to the content underneath. This issue has been noted while using Chrome 21 and Firefox 15. To replicate this problem, you can visit the fo ...

adjustable height of a table (vuetify)

Main.vue <template> <v-app id="inspire"> <v-navigation-drawer v-model="drawer" app > <v-list dense> <v-list-item link> <v- ...

Troubleshooting my Vue app: the mystery behind why I'm unable to utilize debugger or console.log

After setting up a new Vue app using Vue CLI, I encountered an issue where I am unable to utilize the debugger or console.log without triggering an error in the browser. Could anyone provide insights on why this is happening and how it can be resolved? ...

Implementing dynamic properties binding in Vue components

Trying to dynamically bind a prop name to a vuetify component using vue 2.x and vuetify 1.5x. The v-tooltip component requires a prop for the tooltip location, such as bottom or right. Normally, you would pass the prop like this: <v-tooltip bottom>& ...

Unchanging Dive Field

I'm having trouble understanding why this field isn't updating with the correct number. It seems that any value less than 1 is being rounded in the alert() function. I believe all numbers are simply getting rounded down, but I'm unsure of an ...

Discover Vuejs: Display Less, Reveal More

In order to achieve a specific task, I need to display 12 items in 4 columns with 4 items each. Initially, only the first four items are visible and the rest are hidden until the user clicks the "Show More" button. Upon clicking the button, the next row ...

Reset the queue for the slideDown animation using jQuery

I am experiencing an issue with my code where multiple animation effects are running simultaneously. Specifically, when I hover over navbarli1 and then move to another li element with the same navbarli1 class, the slide-down effect is not working as expect ...

What is the recommended image size for Next.js websites?

According to documentation: The width of the image, in pixels. Must be an integer without a unit. The height of the image, in pixels. Must be an integer without a unit. https://nextjs.org/docs/api-reference/next/image Different devices come in various ...

Setting up computed properties in VueJSSetting up computed values in

I am currently working on developing a lottery number service, and I am curious about how to set up computed properties. I came across the Vue.js documentation for computed properties at https://v2.vuejs.org/v2/guide/computed.html#Computed-Properties. I tr ...