element table 表头header-cell-style设置的表头不生效

只要加个!important就可以了,像这样。标点符合不能多写

header-cell-style是官网写的修改el-table表头样式的回调,且说header-row-style不生效也就罢了,header-cell-style也不生效是不是有点过分。

我抓耳挠腮的看了半天,最后手写了个样式进去,但还是气不过,把他改过来,最后在多次查资料(搜索)终于找到了原因,或许是表格父级元素使用了flex布局的原因,只要加个!important就可以了,像这样。

javascript 复制代码
   <el-table :data="tableData" border :style="{ width: width }" :height="height"
        :header-cell-style="tableHeaderCellStyle" 
        >
        <template v-for="(item, index) in tableHeader">
          <el-table-column v-if="item.label == '排序' || item.label == '排名'" :prop="item.prop" :label="item.label"
            :width="item.width" show-overflow-tooltip align="center">
            <template #default="scope">
              <span :style="getStyle(scope)">{{ scope.row[item.prop] }}</span>
            </template>
          </el-table-column>
          <el-table-column v-else :prop="item.prop" :label="item.label" :width="item.width" align="center"
            show-overflow-tooltip>
          </el-table-column>
        </template>
      </el-table>


// 修改 table header cell的背景色
const tableHeaderCellStyle = ({ row, column, rowIndex, columnIndex }) => {
  let background = null
 
  if (rowIndex == 0) {
    props.tableHeader.map((item, index) => {
      if (columnIndex == index && item.background) {
        background = `${item.background} !important`
      }
    })
  }
  // return  { background: 'pink !important' } // 使用 backgroundColor
  // 如果有背景色,则返回一个样式对象
  if (background) {
    return { background: background };  // 使用 backgroundColor
  }
}
相关推荐
前端那点事6 分钟前
Vue线上代码调试全攻略(安全无侵入,新手也能上手)
前端·vue.js
前端那点事10 分钟前
Vue批量文件上传并发踩坑指南:3步解决阻塞、限流、进度混乱
前端·面试
桔筐26 分钟前
Vue3 v-model 双向绑定导致循环触发的坑
前端·javascript·vue.js
Alice-YUE31 分钟前
前端图片优化完全指南:从格式到加载的全面提速方案
前端·笔记·学习
fen_fen44 分钟前
下载Chrome浏览器对应的Driver
前端·chrome
路光.1 小时前
ReferenceError:Can‘t find variable:structureClone
前端·javascript·html·vue2
前端那点事1 小时前
内存泄漏排查全指南:从场景识别到工具实操,新手也能上手
前端·vue.js
我这一生如履薄冰~1 小时前
浏览器多窗口同开一页面,数据同步更新(纯前端方案)
前端·javascript
Alice-YUE1 小时前
前端性能优化完全指南:从指标到实战
前端·学习·性能优化
Rkgua1 小时前
实例成员和静态成员在对象中的用法
javascript