element ui el-table 如何实现带斜线的双表头

实现思路

通过嵌套表头将两个标题设置在一个单元格内,再通过 CSS 样式增加斜线效果。

知识点:el-table、::before、transform

实现的代码

<el-table :data="tableData" border>
	<!--重点代码:采用嵌套的方式-->
   <el-table-column label="数据" align="right" width="150">
     <el-table-column prop="name" label="数据指标" width="150">
     </el-table-column>
   </el-table-column>
   <el-table-column
     v-for="(col, i) in columnList"
     :key="i"
     :prop="col.prop"
     :label="col.label"
     align="center"
   >
     <template v-if="col.children">
       <el-table-column
         v-for="(item, index) in col.children"
         :key="index"
         :prop="item.prop"
         :label="item.label"
       >
       </el-table-column>
     </template>
   </el-table-column>
 </el-table>
 <style scoped>
/*表头斜线*/

	/*::v-deep 这里主要的作用就是用来强制修改element默认的样式*/
	::v-deep  .el-table thead.is-group th {
        background: none;
        padding: 0px;
    }

    ::v-deep .el-table thead.is-group tr:first-of-type th:first-of-type {
        border-bottom: none;/*中间的横线去掉*/
    }

    ::v-deep .el-table thead.is-group tr:first-of-type th:first-of-type div.cell {
        text-align: right;/*上边文字靠右*/
    }

    ::v-deep .el-table thead.is-group tr:last-of-type th:first-of-type div.cell {
        text-align: left;/*下边文字靠左*/
    }
    
	/*核心代码*/
    ::v-deep .el-table thead.is-group tr:first-of-type th:first-of-type:before {
        content: "";
        position: absolute;
        width: 1px;
        height: 80px;/*斜线的长度*/
        top: 0;
        left: 0;
        background-color: #18449C;
        opacity: 1;
        display: block;
        transform: rotate(-61deg);/*调整斜线的角度*/
        -webkit-transform-origin: top;
        transform-origin: top;
    }
	
    ::v-deep .el-table thead.is-group tr:last-of-type th:first-of-type:before {
        content: "";
        position: absolute;
        width: 1px;
        height: 80px;/*斜线的长度*/
        bottom: 0;
        right: 0;
        background-color: #18449C;
        opacity: 1;
        display: block;
        transform: rotate(-62deg);/*调整斜线的角度*/
        -webkit-transform-origin: bottom;
        transform-origin: bottom;
    }
   
</style>
Element Plus 的 el-table-v2 组件支持自定义多级表头,可以通过 el-table-column 的 children 属性来实现。具体步骤如下: 1. 在 el-table-column 中设置 prop 和 label 属性,用于显示表头名称和对应的数据字段。 2. 如果需要设置多级表头,可以在 el-table-column 中设置 children 属性,并在其中嵌套新的 el-table-column 组件,重复步骤1。 3. 如果需要合并表头单元格,可以在 el-table-column 中设置 rowspan 和 colspan 属性。 4. 如果需要自定义表头样式,可以在 el-table-column 中设置 headerStyle 属性。 下面是一个示例代码,展示了如何使用 el-table-v2 自定义多级表头: ``` <template> <el-table :data="tableData"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column label="成绩"> <el-table-column prop="math" label="数学"></el-table-column> <el-table-column prop="english" label="英语"></el-table-column> <el-table-column prop="chinese" label="语文"></el-table-column> </el-table-column> <el-table-column prop="total" label="总分" rowspan="2"></el-table-column> </el-table> </template> <script> import { ref } from "vue"; import { ElTable, ElTableColumn } from "element-plus"; export default { components: { ElTable, ElTableColumn, }, setup() { const tableData = ref([ { name: "张三", math: 80, english: 90, chinese: 85, total: 255 }, { name: "李四", math: 75, english: 85, chinese: 90, total: 250 }, ]); return { tableData, }; }, }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

代码搬运媛

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

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

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

打赏作者

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

抵扣说明:

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

余额充值