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
  }
}
相关推荐
恋猫de小郭21 小时前
作为 Cursor 和 Claude Code 的研发者,他们是如何看待和使用 AI
android·前端·ai编程
hai742521 小时前
通达OA批量导出PDF审批表单与批量打印功能组件介绍
前端
Dreamcatcher_AC21 小时前
慢慢买项目:一站式开发指南
前端·javascript·mongodb·node.js
烤麻辣烫21 小时前
黑马大事件学习-19(文章)
前端·css·vue.js·学习·html
西西学代码1 天前
Flutter---折线图(使用外部库)
前端·javascript·flutter
坚持学习前端日记1 天前
Android JS桥技术深度解析
android·开发语言·javascript
json{shen:"jing"}1 天前
07_表单输入绑定
java·前端·javascript
yyt3630458411 天前
K 线图高性能窗口化渲染
前端·javascript·css·vue.js·gitee·vue
XiaoYu20021 天前
第5章 Nest.js精进-IOC控制反转
前端
LV技术派1 天前
适合很多公司和团队的 AI Coding 落地范式(二)
前端·aigc·ai编程