<template>
<div class="main">
<el-table :data="tableData" :span-method="objectSpanMethod" style="width: 100%" border>
<el-table-column v-for="(item, index) in tableColumn" :key="index" :prop="item.prop" :label="item.label" />
</el-table>
<aSad />
</div>
</template>
<script>
import { getMergeCells } from "@/views/mydemo/baseurl";
export default {
data() {
return {
tableColumn: [
{ prop: "id", label: "序号" },
{ prop: "School", label: "学校" },
{ prop: "Grade", label: "年级" },
{ prop: "Class", label: "班级" },
{ prop: "Name", label: "姓名" },
{ prop: "Chinese", label: "中文" },
{ prop: "Math", label: "数学" },
{ prop: "English", label: "英文" }
],
tableData: [
{ id: 1, School: "第一小学", Grade: "1年级", Class: "1班", Name: "张三", Chinese: "90", Math: "100", English: "80" },
{ id: 2, School: "第一小学", Grade: "1年级", Class: "1班", Name: "张伟", Chinese: "90", Math: "99", English: "89" },
{ id: 3, School: "第一小学", Grade: "1年级", Class: "1班", Name: "李四", Chinese: "90", Math: "85", English: "80" },
{ id: 4, School: "第一小学", Grade: "1年级", Class: "2班", Name: "李四2", Chinese: "90", Math: "85", English: "80" },
{ id: 5, School: "第一小学", Grade: "1年级", Class: "3班", Name: "王五", Chinese: "920", Math: "100", English: "80" },
{ id: 6, School: "第一小学", Grade: "2年级", Class: "1班", Name: "赵六", Chinese: "9210", Math: "100", English: "80" },
{ id: 7, School: "第一小学", Grade: "2年级", Class: "2班", Name: "钱八", Chinese: "982", Math: "85", English: "80" },
{ id: 8, School: "第一小学", Grade: "2年级", Class: "3班", Name: "陈九", Chinese: "79", Math: "100", English: "100" },
{ id: 9, School: "第一小学", Grade: "3年级", Class: "1班", Name: "黄十", Chinese: "91", Math: "88", English: "80" },
{ id: 10, School: "第一小学", Grade: "3年级", Class: "2班", Name: "魏一", Chinese: "90", Math: "86", English: "87" },
{ id: 11, School: "第一小学", Grade: "3年级", Class: "3班", Name: "杨二", Chinese: "79", Math: "99", English: "80" },
{ id: 12, School: "第二小学", Grade: "3年级", Class: "3班", Name: "袁零", Chinese: "79", Math: "99", English: "80" }
]
};
},
computed: {
spanArr() {
if (!this.tableColumn.length) return [];
const mergeCols = ["School", "Class", "Grade", "Chinese"];
return getMergeCells(this.tableData, this.tableColumn, mergeCols);
}
},
methods: {
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
return this.spanArr[rowIndex][columnIndex];
}
}
};
</script>
<style lang="scss" scoped>
.main {
width: 100%;
height: 100%;
}
</style>
export const getMergeCells = (tableData = [], tableColumn = [], mergeCols = []) => {
const fields = tableColumn?.map((v) => v.prop);
const array = [];
if (!tableData?.length || !tableColumn?.length || !mergeCols?.length) return;
for (let row = tableData.length - 1; row >= 0; row--) {
array[row] = [];
for (let col = 0; col < fields.length; col++) {
if (row === tableData.length - 1 || !mergeCols.includes(fields[col]) || !tableData[row][fields[col]]) {
array[row][col] = [1, 1];
continue;
}
const parentFields = mergeCols.slice(0, col);
if (mergeCols.includes(fields[col]) && parentFields?.includes(fields[col - 1])) {
const currentParents = parentFields.map((field) => tableData[row][field]);
const nextRowParents = parentFields.map((field) => tableData[row + 1][field]);
if (currentParents?.toString() !== nextRowParents?.toString()) {
array[row][col] = [1, 1];
continue;
}
}
if (tableData[row][fields[col]] === tableData[row + 1][fields[col]]) {
const beforeCell = array[row + 1][col];
array[row][col] = [1 + beforeCell[0], 1];
beforeCell[0] = 0;
beforeCell[1] = 0;
} else {
array[row][col] = [1, 1];
}
}
}
return array;
};