Search code examples
javascriptvuejs2element-ui

Background colors to rows of a element-ui table with pagination in vueJS 2 but failing to render


I have to add red background Colour for false, and green background for true based on the result column in a table, I am using elementUI + paginations of data-tables + vuejs. I am try to add in the column declarations by using style binding on result column thanks in advance

My template code

<template slot="header" slot-scope="table" :style = "customRowBackground(table.row)">
<template slot-scope="table">{{ table.row.launch_success }}</template>
</el-table-column>

my customRowBackgrond() function

 customRowBackgrond({row}){     
      if (row.launch_success == true) {
        return {'backgrondColor': 'rgb(252, 230, 190)'};
      } 
      else if (row.launch_success == false) {
        return { backgrondColor: 'rgb(252, 230, 190)'};
      } 
      else {
        return {backgrondColor: 'rgb(252, 230, 190)'};
      }
    },

I need to get the true value to green and false to red to my whole table.... Thanks in advance.


Solution

  • Try this

    :style = "table.row.launch_success == true ? '{"backgrondColor": "rgb(252, 230, 190)"}' : table.row.launch_success == false ? '{"backgrondColor": "rgb(252, 230, 190)"}' : '{"backgrondColor': "rgb(252, 230, 190)"}'
    

    Or

    In template

    <el-table :data="tableData2" style="width: 100%" :row-class-name="tableRowClassName">
    

    Update method as below

    methods: {
          tableRowClassName({row, rowIndex}) {
            if (row.launch_success == true) {
              return 'success-row';
            } else if (row.launch_success == false) {
              return 'warning-row';
            }
            return 'other-row';
          }
        },
    

    Update CSS as below

    .el-table .warning-row {
    background: 'rgb(252, 230, 190)';
    }
    
    .el-table .success-row {
    background: 'rgb(252, 230, 190)';
    }
    
    .el-table .other-row {
    background: 'rgb(252, 230, 190)';
    }