Search code examples
javascriptvue.jswebvuejs2vue-component

How do I change a cell in the element-ui table?


Hello everyone I wanted to hang the condition on the cells. Let's say we output names in the Name column. If the name is John, then highlight the name in red. When I work in this case, then the whole row is highlighted, but I only want the name in a specific cell.

<script>
  export default {
    methods: {
      tableRowClassName({row, rowIndex}) {
        if (row.name === "John") {
          return 'warning-row';
        } else if (row.name === "Tom") {
          return 'success-row';
        }
        return '';
      }
    },
    data() {
      return {
        tableData:  [{
          date: '2016-05-03',
          name: 'Tom',
          address: 'No. 189, Grove St, Los Angeles'
        }, {
          date: '2016-05-02',
          name: 'John',
          address: 'No. 189, Grove St, Los Angeles'
        }, {
          date: '2016-05-04',
          name: 'Tom',
          address: 'No. 189, Grove St, Los Angeles'
        }, {
          date: '2016-05-01',
          name: 'Tom',
          address: 'No. 189, Grove St, Los Angeles'
        }]
      }
    }
  }
</script>
<style>
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }
</style>
<template>
  <el-table
    :data="tableData"
    style="width: 100%"
    :row-class-name="tableRowClassName">
    <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="address"
      label="Address">
    </el-table-column>
  </el-table>
</template>


Solution

  • you can use this method.

    
    <el-table 
       :data="tableData" 
      :cell-style="cellStyle"
    >
    </el-table>
    

    js

    const cellStyle = ({ row, column, rowIndex, columnIndex }) => {
      if (rowIndex === 1 && columnIndex === 1) {
        return {
          backgroundColor: 'pink'
        }
      }
    }
    

    enter image description here