el-table怎么设置单个表格变颜色

项目需求:将表格数据中相同的数据背景色改为一致,效果大致如下

 HTML代码:注意一定要加上,并使用table中的单元格的 style 的回调方法cell-style

 <template slot-scope="scope">

            <span>{{ scope.row.date.value }}</span>

</template>

<template>
  <div class="about">
    <el-table
        :data="tableData"
        border
        style="width: 100%"
        // 重点,使用这个回调方法
        :cell-style="cellStyle"
      >
        <el-table-column prop="date" label="日期" width="180">
          <template slot-scope="scope">
            <span>{{ scope.row.date.value }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="姓名" width="180">
          <template slot-scope="scope">
            <span>{{ scope.row.name.value }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="地址">
          <template slot-scope="scope">
            <span>{{ scope.row.address.value }}</span>
          </template>
        </el-table-column>
      </el-table>
  </div>
</template>

再与后端协商数据格式如下:

<script>
export default {
  data() {
      return {
        tableData: [
        {
          date: { value: "2016-05-02", state: 2 },
          name: { value: "王虎", state: 0 },
          address: { value: "上海市普陀区金沙江路 1517 弄", state: 2 },
        },
        {
          name: { value: "王小虎", state: 2 },
          date: { value: "2016-05-03", state: 1 },
          address: { value: "上海区金沙江路 15市普陀16弄", state: 2 },
        },
        {
          name: { value: "王虎", state: 0 },
          date: { value: "2016-05-03", state: 1 },
          address: { value: "上海区金沙江路 15市普陀18弄", state: 2 },
        },
      ],
      }
    },
//方法如下
    methods:{
      cellStyle({ row, column, rowIndex, columnIndex }) {
      let dataColor = false;
      let nameColor = false;
      Object.keys(row).forEach((key) => {
        if (row[key].state == 1 && key == column.property) {
          if (rowIndex === rowIndex && columnIndex === columnIndex) {
            dataColor = true;
          }
        } else if (row[key].state == 0 && key == column.property) {
          if (rowIndex === rowIndex && columnIndex === columnIndex) {
            nameColor = true;
          }
        }
      });
      if (dataColor) {
        return "backgroundColor:yellow";
      }
      if (nameColor) {
        return "backgroundColor:pink";
      }
    },
    }
}
</script>

设定表格单元格颜色,围绕style 的回调方法cell-style来进行就可以啦!!!

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值