element-ui表格行颜色

需求:

如果有多条id相同时,所在行展示颜色,并且重复id若是不一样,所在行颜色也要不一样。具体展示效果用时间列代替id。
效果图

实现思路:

因为考虑到列表中 oldArr(原数组)重复id的排序并不是连续的,所以对id进行去重,并列出重复次数大于1的id数组 arr 。对列表数据重新处理,如果所在行的id等于arr里的某个id,那么就添加一个type属性,为展示颜色做准备。实现代码如下:

const idCounts = oldArr.reduce((acc, obj) => {
	acc[obj.policeId] = (acc[obj.policeId] || 0) + 1;
	return acc;
}, {});

// 找出重复的 id
const arr = Object.keys(idCounts).filter((key) => idCounts[key] > 1).map(Number);
console.log(arr, "arr");
oldArr.forEach((item, index) => {
	if (arr.indexOf(item.policeId)) {
		item.type = arr.indexOf(item.policeId);
	}
});
行样式设置

对列表处理后,就可以根据添加的type属性设置行样式了。实现代码如下:
<el-table> 标签里添加 :row-class-name="tableRowClassName",在 methods 中添加方法:

 methods: {
    tableRowClassName({ row, rowIndex }) {
      console.log(row.type, row.type % 3);
      if (row.type % 3 == 0) {
        return "yellow-row";
      } else if (row.type % 3 == 1) {
        return "blue-row";
      } else if (row.type % 3 == 2) {
        return "green-row";
      } else {
        return "";
      }
    },
 }

设置相应的 css 样式

::v-deep .el-table .blue-row {
  background: #daecfe;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值