ElementUI 的 Table 组件提供了 header-cell-style
和 header-cell-class-name
属性来实现表头单元格的样式控制,结合这些可以实现跨行表头合并效果。
span-method
属性<el-table
:data="tableData"
:span-method="arraySpanMethod"
border
style="width: 100%">
<!-- 表格列定义 -->
</el-table>
methods: {
arraySpanMethod({ row, column, rowIndex, columnIndex }) {
if (rowIndex === 0) {
if (columnIndex === 0) {
return [1, 2]; // 第一行第一列跨2列
} else if (columnIndex === 1) {
return [0, 0]; // 第一行第二列隐藏
}
}
}
}
<el-table
:data="tableData"
:header-cell-style="headerCellStyle"
border
style="width: 100%">
<!-- 表格列定义 -->
</el-table>
methods: {
headerCellStyle({ row, column, rowIndex, columnIndex }) {
if (rowIndex === 0 && columnIndex === 0) {
return {
'text-align': 'center',
'position': 'relative',
'&::after': {
content: '""',
position: 'absolute',
right: '-1px',
top: '50%',
height: '1px',
width: '100%',
background: '#fff'
}
}
}
}
}
row-class-name
和 CSS<el-table
:data="tableData"
:row-class-name="tableRowClassName"
border
style="width: 100%">
<!-- 表格列定义 -->
</el-table>
methods: {
tableRowClassName({ row, rowIndex }) {
if (this.shouldHideRow(row, rowIndex)) {
return 'hidden-row';
}
return '';
},
shouldHideRow(row, rowIndex) {
// 根据业务逻辑判断是否需要隐藏该行
if (rowIndex > 0 && row.someField === this.tableData[rowIndex - 1].someField) {
return true;
}
return false;
}
}
// CSS
.hidden-row {
display: none;
}
computed: {
processedTableData() {
const result = [];
let lastRow = null;
this.tableData.forEach(row => {
if (!lastRow || !this.isDuplicateRow(row, lastRow)) {
result.push(row);
lastRow = row;
}
});
return result;
}
},
methods: {
isDuplicateRow(row1, row2) {
// 定义判断重复行的逻辑
return row1.field1 === row2.field1 && row1.field2 === row2.field2;
}
}
结合上述方法,可以实现一个既支持跨行表头又支持隐藏重复行的表格:
<template>
<el-table
:data="processedTableData"
:span-method="arraySpanMethod"
:row-class-name="tableRowClassName"
border
style="width: 100%">
<el-table-column prop="date" label="日期"></el-table-column>
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [
{ date: '2016-05-02', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄' },
{ date: '2016-05-02', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄' },
{ date: '2016-05-04', name: '李小龙', address: '上海市普陀区金沙江路 1518 弄' },
{ date: '2016-05-01', name: '张小三', address: '上海市普陀区金沙江路 1518 弄' },
{ date: '2016-05-01', name: '张小三', address: '上海市普陀区金沙江路 1518 弄' }
]
}
},
computed: {
processedTableData() {
const result = [];
let lastRow = null;
this.tableData.forEach(row => {
if (!lastRow || !this.isDuplicateRow(row, lastRow)) {
result.push(row);
lastRow = row;
}
});
return result;
}
},
methods: {
arraySpanMethod({ row, column, rowIndex, columnIndex }) {
// 实现跨行表头逻辑
if (rowIndex === 0 && columnIndex === 0) {
return [1, 2];
} else if (rowIndex === 0 && columnIndex === 1) {
return [0, 0];
}
},
tableRowClassName({ row, rowIndex }) {
// 如果需要额外的行隐藏逻辑
return '';
},
isDuplicateRow(row1, row2) {
// 定义判断重复行的逻辑
return row1.date === row2.date && row1.name === row2.name && row1.address === row2.address;
}
}
}
</script>
<style>
.hidden-row {
display: none;
}
</style>
span-method
方法时,隐藏的单元格需要返回 [0, 0]
el-table-column
嵌套) 来实现更复杂的效果希望这些方案能帮助你优雅地实现 ElementUI Table 组件的跨行表头合并和隐藏重复行功能。