elementui表格自定义表头的两种方法

本文涉及的产品
应用实时监控服务-可观测链路OpenTelemetry版,每月50GB免费额度
云原生网关 MSE Higress,422元/月
应用实时监控服务-用户体验监控,每月100OCU免费额度
简介: 这篇文章主要是总结了elementui-table表格怎么自定义表头,两种方式需求不一样,大家还有啥好的方法或者遇到的bug评论区留言大家一起解决。

表格自定义表头的方式

多选框表头换文字

请查看上篇博客

文字换按钮 render-header

参数

说明

类型

可选值

默认值

render-header

列标题 Label 区域渲染使用的 Function

Function(h, { column, $index })

全部代码(可复制)

<template>
    <div class="Box">
        <div>
            <!-- :header-cell-class-name="cellClass" -->
            <el-table ref="multipleTable" :data="tableData" :header-cell-class-name="cellClass" tooltip-effect="dark"
                style="width: 500px" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column label="日期" width="120">
                    <template slot-scope="scope">{{ scope.row.date }}</template>
                </el-table-column>
                <el-table-column label="姓名" prop="name" :render-header="(h, obj) => renderHeader(h, obj, '你的参数')"
                    width="120">
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">修改姓名</el-button>
                </el-table-column>
                <el-table-column prop="address" label="地址" show-overflow-tooltip>
                </el-table-column>
            </el-table>
        </div>
    </div>
</template>
<script>
export default {
    name: "list",
    data () {
        return {
            tableData: [{
                date: '2023-09-03',
                name: 'bug天选之子',
                address: 'https://blog.csdn.net/weixin_70245286?spm=1000.2115.3001.5343'
            }, {
                date: '2023-09-03',
                name: 'bug天选之子',
                address: 'https://blog.csdn.net/weixin_70245286?spm=1000.2115.3001.5343'
            }, {
                date: '2023-09-03',
                name: 'bug天选之子',
                address: 'https://blog.csdn.net/weixin_70245286?spm=1000.2115.3001.5343'
            }, {
                date: '2023-09-03',
                name: 'bug天选之子',
                address: 'https://blog.csdn.net/weixin_70245286?spm=1000.2115.3001.5343'
            },],
            multipleSelection: [],
        }
    },
    methods: {
        // 选中的项
        handleSelectionChange (val) {
            this.multipleSelection = val;
            console.log("选中的项:", this.multipleSelection);
        },
        // 修改多选框表头
        cellClass (row) {
            // 判断第几列
            if (row.columnIndex === 0) {
                return "disableSelection";
            }
        },
        // 自定义表头
        renderHeader (h, { column, $index }, type) {
            // console.log('列表加载就会触发', h, { column, $index }, type)
            console.log(column.label);
            let that = this
            // 逻辑是 h() 括号里包裹标签 第一个参数是标签名 第二个是属性  第三个是标签内容  如果是多个标签需要包裹数组
            return h(
                'div', [
                // 列名称
                // h('span', column.label),
                // 按钮
                h('el-button', {
                    props: {
                        type: 'primary',
                        size: 'small',
                    },
                    style: 'color:#fff;',
                    // class: "{ active: showSelectMore }",
                    // class: 'className',
                    on: {
                        // 各种事件触发
                        click: function () {
                            that.clickButton(type)
                        }
                    }
                }, '姓名')
            ],
            )
        },
        // 按钮点击事件
        clickButton (type) {
            console.log('我点击了' + type + '的列')
            // this.downLoad()
        },
        handleEdit (row) {
        }
    },
    mounted () {
    }
}
</script>
<style scoped>
.Box {
    display: flex;
    justify-content: center;
    align-items: center;
}
::v-deep.el-table .disableSelection .cell .el-checkbox__inner {
    display: none;
    position: relative;
}
::v-deep.el-table .disableSelection .cell::before {
    content: "选项";
    position: absolute;
    right: 15px;
}
::v-deep.el-table {
    border: 1px solid blue;
}
</style>

效果图

9-4-1.png

  关键代码总结

// 在要修改的那一列加render-header属性
<el-table-column label="姓名" prop="name" :render-header="(h, obj) => renderHeader(h, obj, '你的参数')"
</el-table-column>
// methods中写方法
renderHeader (h, { column, $index }, type) {
            // console.log('列表加载就会触发', h, { column, $index }, type)
            console.log(column.label);
            let that = this
            // 逻辑是 h() 括号里包裹标签 第一个参数是标签名 第二个是属性  第三个是标签内容  如果是多个标签需要包裹数组
            return h(
                'div', [
                // 列名称
                // h('span', column.label),
                // 按钮
                h('el-button', {
                    props: {
                        type: 'primary',
                        size: 'small',
                    },
                    style: 'color:#fff;',
                    // class: "{ active: showSelectMore }",
                    // class: 'className',
                    on: {
                        // 各种事件触发
                        click: function () {
                            that.clickButton(type)
                        }
                    }
                }, '姓名')
            ],
            )
        },


相关文章
|
6月前
|
数据格式
使用小技巧实现el-table组件的合并行功能,ElementUI和ElementPlus都适用
本文介绍了在ElementUI和ElementPlus中使用`el-table`组件实现合并行功能的技巧,包括多列合并和单列合并的方法,并提供了相应的示例代码和运行效果。
4730 1
使用小技巧实现el-table组件的合并行功能,ElementUI和ElementPlus都适用
|
JavaScript
VUE element-ui之table表格表头下拉筛选功能
VUE element-ui之table表格表头下拉筛选功能
1093 0
VUE element-ui之table表格表头下拉筛选功能
element-ui table排序sortable三种状态,怎么去掉默认状态
在 element-ui 中,也定义了 sort-orders 有三种状态: ascending、descending、null,这三种状态形成一个循环切换。
2833 0
|
9月前
Element - ui :el-input 输入只能是数字并且小数点后只能是1-2位
Element - ui :el-input 输入只能是数字并且小数点后只能是1-2位
3417 1
element-使用el-date-picker 选择日期后返回周几(整理)
element-使用el-date-picker 选择日期后返回周几(整理)
Element UI - el-table el-table-column 表头自定义
Element UI - el-table el-table-column 表头自定义
737 0
Element UI - el-table el-table-column 表头自定义
|
5月前
|
JavaScript
element-ui table表格多选后再打开默认选中
element-ui table表格多选后再打开默认选中
Echarts title标题配置项的使用 更改颜色 副标题
Echarts title标题配置项的使用 更改颜色 副标题
|
9月前
Echarts柱状图x轴刻度间隔显示不全/x轴文字倾斜
Echarts柱状图x轴刻度间隔显示不全/x轴文字倾斜
1387 0
|
JavaScript 前端开发
CSS进阶向--配合Vue动态样式实现“超炫酷”圆环菜单
CSS进阶向--配合Vue动态样式实现“超炫酷”圆环菜单
1020 2
CSS进阶向--配合Vue动态样式实现“超炫酷”圆环菜单