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
  }
}
相关推荐
沛沛老爹16 分钟前
Web转AI架构篇:Agent Skills vs MCP-混合架构设计模式实战指南
java·前端·人工智能·架构·llm·rag
张张努力变强24 分钟前
C++类和对象(一):inline函数、nullptr、类的定义深度解析
开发语言·前端·jvm·数据结构·c++·算法
Elcker1 小时前
JAVA-Web 项目研发中如何保持团队研发风格的统一
java·前端·javascript
selectDele1 小时前
Solid.js和React的比较
前端·javascript·react.js·solid.js
摘星编程2 小时前
React Native for OpenHarmony 实战:I18n 国际化详解
javascript·react native·react.js
小旋风012342 小时前
前端对接豆包AI(vue2版本)
前端·人工智能
—Qeyser2 小时前
Flutter AppBar 导航栏组件完全指南
前端·javascript·flutter
摘星编程2 小时前
React Native for OpenHarmony 实战:Localization 本地化详解
javascript·react native·react.js
Amumu121382 小时前
React扩展(一)
前端·javascript·react.js
cypking2 小时前
三、前端规范化 项目代码规范
前端·代码规范