element中el-table表头通过header-row-style设置样式

文章目录

一、知识点

有些时候需要给element-ui表头设置不同样式,比如居中、背景色、字体大小等等,这时就可以用到本文要说的属性header-row-style。官网说明如下所示:

二、设置全部表头

2.1、方式一

html 复制代码
<el-table :header-cell-style="{'text-align': 'center'}" />

2.2、方式二

vue 复制代码
<template>
	<el-table :header-cell-style="tableHeaderColor" />
</template>
<script>
export default {
	methods: {
		tableHeaderColor ({row, column, rowIndex, columnIndex}) {
			return 'text-align: center;'
		}
	}
}
</script>

三、设置某个表头

vue 复制代码
<template>
	<el-table :header-cell-style="tableHeaderColor" />
</template>
<script>
export default {
	methods: {
		// 设置表头的颜色
		tableHeaderColor({ row, column, rowIndex, columnIndex }) {
			console.log(row, column, rowIndex, columnIndex);
			if (rowIndex === 0 && columnIndex === 1) {
				return 'background-color: #afccfd; color:#000000;'; //蓝色
			} else if (rowIndex === 0 && columnIndex === 2) {
				return 'background-color: #c0e33c; color:#000000;';//绿色
			} else if (rowIndex === 0 && columnIndex === 3) {
				return 'background-color: #fbc57b; color:#000000;';//橙色
			} else {
				return 'color:#000000; background: #ffffff;';
			}
		}
	}
}
</script>

效果如下所示:

四、最后

本人每篇文章都是一字一句码出来,希望大佬们多提提意见。顺手来个三连击,点赞👍收藏💖关注✨。创作不易,给我打打气,加加油☕

相关推荐
小李子呢02115 分钟前
前端八股CSS(3)---水平垂直居中的实现方法
前端·css·css3
M ? A8 分钟前
Vue3 转 React:组件透传 Attributes 与 useAttrs 使用详解|VuReact 实战
前端·javascript·vue.js·经验分享·react.js·开源·vureact
火山引擎开发者社区8 分钟前
方舟 Coding Plan 支持 Embedding 模型,让 AI Agent “找得更准、记得更久”
前端·javascript·人工智能
whuhewei14 分钟前
微前端之模块联邦
前端·架构
We་ct14 分钟前
JS手撕:手写Koa中间件与Promise核心特性
开发语言·前端·javascript·中间件·node.js·koa·co
欧阳天风15 分钟前
vue3的组件优化
前端·vue.js·性能优化
打瞌睡的朱尤16 分钟前
蓝桥杯复习大纲
前端·javascript·vue.js
许彰午17 分钟前
# Excel转PDF合并单元格边框错乱?jxl+iText逐格解析样式,政务报表精准还原方案
前端·javascript·pdf
观无22 分钟前
html+nginx实现看板
前端·nginx·html
bcbobo21cn26 分钟前
Web 3D 正方体贴图
前端·3d·贴图·mesh