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

表格自定义表头的方式

        多选框表头换文字

                请查看上篇博客:http://t.csdn.cn/69De2

        文字换按钮 render-header
                render-header方法详情
Table-column Attributes
参数说明类型可选值默认值
render-header列标题 Label 区域渲染使用的 FunctionFunction(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>
        效果图

        关键代码总结

        

// 在要修改的那一列加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)
                        }
                    }
                }, '姓名')
            ],
            )


        },

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bug天选之子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值