【项目经验】:elementui表格中表头的多选框换成文字

一.项目需求

表格可以多选,表头都是汉字。。。。类似于这种

二.实现功能

用到的方法

|------------------------|-----------------------------------------------------------|-------------------------------------------------------|-----|-----|
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
| header-cell-class-name | 表头单元格的 className 的回调方法,也可以使用字符串为所有表头单元格设置一个固定的 className。 | Function({row, column, rowIndex, columnIndex})/String | --- | --- |
[Table Attributes]

实现代码(可复制直接跑)

HTML部分
html 复制代码
<template>
    <div class="Box">
        <div>
            <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 500px"
                @selection-change="handleSelectionChange" :header-cell-class-name="cellClass">
                <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 prop="name" label="姓名" width="120">
                </el-table-column>
                <el-table-column prop="address" label="地址" show-overflow-tooltip>
                </el-table-column>
            </el-table>
        </div>
    </div>
</template>
js部分
javascript 复制代码
<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";
            }
        }

    },
    mounted () {

    }
}
</script>
css部分
css 复制代码
<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>
实现后的效果图

三.总结

关键代码

javascript 复制代码
// 在表格上绑定header-cell-class-name属性
<el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 500px"
                @selection-change="handleSelectionChange" :header-cell-class-name="cellClass">
// 在methods中判断确定是第一列然后给对应的class名
cellClass (row) {
            // 判断第几列
            if (row.columnIndex === 0) {
                return "disableSelection";
            }
        }
// css做对应修改

// 隐藏多选框表头
::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;
}

大家有啥更好的方法评论区留言

相关推荐
爱吃的小肥羊2 小时前
比 Claude Code 便宜一半!Codex 国内部署使用教程,三种方法任选一!
前端
IT_陈寒3 小时前
SpringBoot项目启动慢?5个技巧让你的应用秒级响应!
前端·人工智能·后端
树上有只程序猿4 小时前
2026低代码选型指南,主流低代码开发平台排名出炉
前端·后端
橙某人4 小时前
LogicFlow 小地图性能优化:从「实时克隆」到「占位缩略块」!🚀
前端·javascript·vue.js
高端章鱼哥4 小时前
为什么说用OpenClaw对打工人来说“不划算”
前端·后端
大脸怪4 小时前
告别 F12!前端开发者必备:一键管理 localStorage / Cookie / SessionStorage 神器
前端·后端·浏览器
Mr_Mao4 小时前
我受够了混乱的 API 代码,所以我写了个框架
前端·api
小徐_23334 小时前
向日葵 x AI:把远程控制封装成 MCP,让 AI 替我远程控制设备
前端·人工智能
boooooooom4 小时前
讲清 Proxy + effect + track/trigger 流程
javascript·vue.js·面试
冴羽4 小时前
来自顶级大佬 TypeScript 之父的 7 个启示
前端·typescript