elementUI修改table样式

在Vue项目中,如果使用的是单文件组件(.vue),并且样式是通过<style>标签定义的,vue2可以使用/deep/,vue3可以使用::v-deep选择器来修改ElementUI组件的样式。

1.修改表头背景色
  /deep/.el-table__header th {
    background-color: #000f1c;
    color: #3b496b;
  }
2.设置表格背景色
  /deep/.el-table,
  /deep/.el-table__expanded-cell {
    background-color: #000f1c !important;
  }
3.设置表格边框
  /deep/.el-table tr,
  /deep/.el-table th,
  /deep/.el-table td {
    border: none !important;
  }
4.修改滚动条样式
  ::-webkit-scrollbar {
    width: 8px;
    height: 8px;
    background-color: #104ddb;
  }
  ::-webkit-scrollbar-thumb {
    box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
    -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
    background-color: #0071bb;
    border-radius: 6px;
  }
  ::-webkit-scrollbar-track {
    box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
    background: #0b253a;
  }
5.设置隔行变色
<script> 
export default {
  methods: {
    tableRowClassName({ row, rowIndex }) {
      console.log(row);
      if (rowIndex % 2 === 1) {
        return "warning-row";
      } else {
        return "success-row";
      }
    },
  }
}
</script>

<style scoped lang="scss">
  /deep/.el-table .warning-row {
    background: #000f1c;
    color: #ffffff;
    border: 0;
  }

  /deep/.el-table .success-row {
    background: #041628;
    color: #ffffff;
  }
</style>
6.鼠标滑过当前行变色
  /deep/.el-table__body tr:hover > td {
    background-color: #083a5a !important;
  }
7.完整代码
<template>
  <div class="cameraList">
    <el-table
      :data="tableData"
      style="width: 30%"
      height="200"
      size="mini"
      :row-class-name="tableRowClassName"
    >
      <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>
    </el-table>
  </div>
</template>
<script>
export default {
  methods: {
    // 表格隔行变色
    tableRowClassName({ row, rowIndex }) {
      console.log(row);
      if (rowIndex % 2 === 1) {
        return "warning-row";
      } else {
        return "success-row";
      }
    },
  },
  data() {
    return {
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
      ],
    };
  },
};
</script>
<style scoped lang="scss">
.cameraList {
  width: 1920px;
  height: 1080px;
  background: #000f1c;

  /* elementUI表格table样式修改 */
  /deep/.el-table__header th {
    background-color: #000f1c;
    color: #3b496b;
  }
  /deep/.el-table .warning-row {
    background: #000f1c;
    color: #ffffff;
    border: 0;
  }

  /deep/.el-table .success-row {
    background: #041628;
    color: #ffffff;
  }

  // 鼠标滑过,当前行变色
  /deep/.el-table__body tr:hover > td {
    background-color: #083a5a !important;
  }

  // 表格背景色
  /deep/.el-table,
  /deep/.el-table__expanded-cell {
    background-color: #000f1c !important;
  }

  // 表格边框
  /deep/.el-table tr,
  /deep/.el-table th,
  /deep/.el-table td {
    border: none !important;
  }

  /deep/.el-table::before {
    height: 0;
  }

  // 滚动条上面超出的部分
  /deep/.el-table .el-table__cell.gutter {
    background: #000f1c;
  }

  // 修改滚动条样式
  ::-webkit-scrollbar {
    width: 8px;
    height: 8px;
    background-color: #104ddb;
  }
  ::-webkit-scrollbar-thumb {
    box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
    -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
    background-color: #0071bb;
    border-radius: 6px;
  }
  ::-webkit-scrollbar-track {
    box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
    background: #0b253a;
  }
}
</style>
8.效果图如下

### ElementUI 表格组件自定义样式 在使用 `el-table` 组件时,为了满足项目需求中的特定设计风格,通常需要对表格的默认样式进行调整。这可以通过多种方式来完成。 #### 使用内联样式或类名修改单元格样式 对于简单的样式更改,可以直接通过给定列定义中的 `class-name` 或者 `header-align`, `align` 等属性来进行基本布局上的定制化处理[^2]: ```html <template> <el-table :data="tableData"> <!-- 定义一列表头并指定其样式 --> <el-table-column prop="date" label="日期" width="180" header-align="center" align="right"></el-table-column> ... </el-table> </template> ``` #### 动态设置行/列样式 当涉及到更复杂的逻辑判断下的样式变化时,则可以利用 `row-style` 和 `cell-style` 方法动态返回对象形式的 CSS 样式规则。此方法允许基于每一行的数据特性灵活地改变外观表现[^3]: ```javascript // JavaScript 部分 methods: { tableRowClassName({ row, rowIndex }) { if (rowIndex % 2 === 0) { // 偶数行背景颜色变浅灰色 return 'warning-row'; } else { return ''; } }, cellStyle({ row, column, rowIndex, columnIndex }) { const styles = {}; // 如果是最后一列则字体加粗显示 if(columnIndex === this.columns.length - 1){ styles.fontWeight = "bold"; } return styles; } } ``` ```css /* 对应上面偶数行高亮 */ .warning-row { background-color: #f0f9eb !important; } ``` #### 自定义表头 Tooltip 提示 针对某些情况下表头信息过长而无法完全展示的问题,可通过插槽机制配合 `el-tooltip` 实现悬停查看完整描述的功能[^1]: ```html <template slot="header" slot-scope="scope"> <el-tooltip effect="dark" :content="scope.column.label" placement="top-start"> {{ scope.column.label }} </el-tooltip> </template> ``` 以上便是几种常见的 `ElementUI el-table` 组件自定义样式的实践方案,具体实施可根据实际业务场景和个人偏好做出适当调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值