element在多级表头如何设置不同的颜色

使用了多级表头之后,如何用不同的颜色去实现或者给每个多级表头的下面的边框颜色不同,

使用不同的背景颜色,或者不同的边框 便于区分不同表头下面的数据

img

该回答引用chatgpt:
在 element-ui 中,可以通过在表格头部的 el-table-column 标签中,使用 header-cell-style 属性来设置多级表头的样式。

例如,如果我们要设置第一列多级表头的背景颜色为红色,可以在该列的 el-table-column 标签中添加以下代码:

<el-table-column prop="name" label="姓名" width="120">
  <template slot="header" slot-scope="{ column }">
    <div style="background-color: red; color: white; text-align: center;">
      {{ column.label }}
    </div>
  </template>
</el-table-column>



同样的,如果我们要设置第二列多级表头的边框颜色为绿色,可以在该列的 el-table-column 标签中添加以下代码:


<el-table-column prop="age" label="年龄" width="120">
  <template slot="header" slot-scope="{ column }">
    <div :style="{ border: '1px solid green', textAlign: 'center' }">
      {{ column.label }}
    </div>
  </template>
</el-table-column>


其中,slot="header" 表示该标签用作表格头部,slot-scope="{ column }" 则表示该标签可以访问表格头部的 column 对象,从而可以对该列的表头进行样式设置。在这里,我们分别使用了 background-color 和 border 属性来设置背景颜色和边框颜色。

加个动态绑定样式

  <el-table
    :data="tableData"
    border
    :header-cell-style="rowClass"
    :cell-style="cellStyle"
  >

js逻辑:


  methods: {
    // 设置表头的颜色 行 rowIndex  列columnIndex
    rowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0 && columnIndex === 2) {
        return "background:red!important";
      } else if (rowIndex === 0 && columnIndex === 7) {
        return "background:red!important";
      }
    },
    // 设置指定行、列、具体单元格颜色 行 rowIndex  列columnIndex
    cellStyle({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 2) {
        return "background:yellow";
      } else if (columnIndex === 7) {
        return "background:yellow";
      } else {
        return "";
      }
    },
  },