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
  }
}
相关推荐
心易行者几秒前
代码写好了,然后呢?——手把手教你把Python脚本变成能赚钱的Web应用
开发语言·前端·python
程序员蓝莓几秒前
别再花钱买HTTPS证书了!永久免费自动更新证书-Let's Encrypt。三步无脑安装。
前端
LinHan2 分钟前
功能区代码块一直不能优雅折叠?2026年,我终于用这个 VS Code 插件解决了
前端
kongba0073 分钟前
2026年4月19日 kimi记忆备份
java·前端·数据库
青天诀3 分钟前
mobile-bridge-mcp,实现AI远程操控手机上的web页面
前端
舒一笑33 分钟前
Windows 下执行 pnpm install 报 EBUSY: resource busy or locked,我最后用这一招解决了
前端·windows·程序员
龙月35 分钟前
Gitlab迁移与升级技术方案
前端·后端
用户2235862182036 分钟前
核心三角-Command Agent Skill - claude_0x02
前端
竹林8181 小时前
在NFT项目中集成IPFS:从Pinata上传到前端展示的完整踩坑指南
前端·javascript
吴声子夜歌1 小时前
Vue3——渲染函数
前端·vue.js·vue·es6