el-table 设置行背景颜色 鼠标移入高亮问题处理

一、 设置行背景颜色

1. 需求描述

后端返回表格数据,有特定行数需要用颜色标识。类似于以下需求:
在这里插入图片描述
在这里插入图片描述

2. 解决方式

方式区别
:row-class-name=“tableRowClassName”已返回类名的形式设置样式,代码整洁,但是会鼠标高亮,导致背景颜色失效
:cell-style=“cellStyle”以返回样式的形式设置,无鼠标高亮问题

2.1 表格代码

<el-table
    :data="tableData"
    style="width: 100%"
    class="tableStyle"
    :row-class-name="tableRowClassName"
    :cell-style="cellStyle">
    <el-table-column
      prop="date"
      label="日期"
      width="180">
    </el-table-column>
    <el-table-column
      prop="name"
      label="姓名"
      width="180">
    </el-table-column>
    <el-table-column
      prop="address"
      label="地址">
    </el-table-column>

2.2 :row-class-name=“tableRowClassName” 方式

/**
     * @description: 合计行处理:  :row-class-name="tableRowClassName" 方式
     * @param {*}row, rowIndex
     * @return {*}
     */
    tableRowClassName({row, rowIndex}) {
        if (rowIndex === 1) {
          return 'warning-row';
        } else if (rowIndex === 3) {
          return 'success-row';
        }
        return '';
      }

2.3 :cell-style="cellStyle"方式

/**
     * @description: 合计行处理:   :cell-style="cellStyle"方式
     * @param {*}row, rowIndex
     * @return {*}
     */
    cellStyle({ row, rowIndex }) {
     if (rowIndex === 1) {
            return 'background: #4D939F !important;color: #fff;'
        } else if (rowIndex === 3) {
           return 'background: #e6a23c !important;color: #fff;'
        }
        return '';
      }

3. 样式设置

3.1 row-class-name方式的样式

<style lang='scss' scoped>
/deep/ .el-table .warning-row td {
  background: #4D939F !important;
  color: #fff;
}

/deep/ .el-table .fixedRow td {
  background: #4D939F !important;
  color: #fff;
  position: sticky;
  bottom: 0;
  width: 100%;
}
<style>

4. 表头设置颜色

在这里插入图片描述

4.1 第一种直接设置

<style>
.el-table th.red {
  background-color: #FBBFBC;
  color: #fff;
}

.el-table th.green {
  background-color: #FEDDB6;
  color: #fff;
}
</style>

4.2 第二种 设置类名 避免样式污染 推荐第二种
注意,是.tableStyle.el-table 不是.tableStyle .el-table

<style>
.tableStyle.el-table th.red {
  background-color: #FBBFBC;
  color: #fff;
}

.tableStyle.el-table th.green {
  background-color: #FEDDB6;
  color: #fff;
}
</style>
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值