el-table加背景色

 

 

<template>
  <div id="app">
    <el-table
      :data="tableData"
      border
      :header-cell-style="{
        background: '#fafafa',
        color: '#333',
        fontWeight: '600',
        fontSize: '14px',
      }"
      style="width: 541px"
      :row-style="TableRowStyle"
    >
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="age" label="年龄" width="180"> </el-table-column>
      <el-table-column prop="school" label="是否上学" width="180"> </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: "app",
  data() {
    return {
      tableData: [
        {
          name: "孙悟空",
          age: 500,
          school: "上学中",
        },
        {
          name: "猪八戒",
          age: 88,
          school: "已辍学",
        },
        {
          name: "沙僧",
          age: 1000,
          school: "已辍学",
        },
        {
          name: "唐僧",
          age: 99999,
          school: "上学中",
        },
      ],
    };
  },
  methods: {
    // 当状态为 已辍学 的状态,加上背景色
    TableRowStyle({ row, rowIndex }) {
      // 注意,这里返回的是一个对象
      let rowBackground = {};
      if (row.school.includes("辍学") ) {
        rowBackground.background = "pink";
        return rowBackground;
      }
    },
  },
};
</script>

还有

cell-style的设置举例
<el-table :data="auditList" border highlight-current-row style="width: 100%" :cell-style="addClass">
...
</el-teble>
addClass({row,column,rowIndex,columnIndex}) {
var bgColor = '';
if(columnIndex == 2) {
if(row.kkk == 'Yes') {
 return 'background:#baecc6';
}
}

 




 

posted @ 2021-07-23 18:26  君子笑而不语  阅读(2841)  评论(0编辑  收藏  举报