<el-table :data="tableData" border :span-method="objectSpanMethod" style="width: 100%" height="450">
<el-table-column label="日期" prop="date" type="selection" width="55" align="center"></el-table-column>
<el-table-column prop="reportDate" label="日期" :width="flexColumnWidth('reportDate', tableData)">
<template slot-scope="scope">
<div class="table" style="color: #1677ff; cursor: pointer" @click="getDetail(scope.row)">{{
scope.row.reportDate }}</div>
</template>
</el-table-column>
<el-table-column prop="weather" label="天气" :width="flexColumnWidth('weather', tableData)">
<template slot-scope="scope">
<div class="table">{{ scope.row.weather }}</div>
</template>
</el-table-column>
<el-table-column prop="workContent" label="工作内容" :width="flexColumnWidth('workContent', tableData)">
<template slot-scope="scope">
<div class="table">{{ scope.row.workContent }}</div>
</template>
</el-table-column>
<el-table-column prop="urls" label="照片&视频" :width="flexColumnWidth('urls', tableData)">
<template slot-scope="scope">
<div class="table">{{ scope.row.urls }}</div>
</template>
</el-table-column>
<el-table-column prop="participantName" label="作业人员"
:width="flexColumnWidth('participantName', tableData)">
<template slot-scope="scope">
<div class="table">{{ scope.row.participantName }}</div>
</template>
</el-table-column>
<el-table-column prop="workHours" label="工时(h)" :width="flexColumnWidth('workHours', tableData)">
<template slot-scope="scope">
<div class="table">{{ scope.row.workHours }}</div>
</template>
</el-table-column>
<el-table-column prop="organization" label="参与组织" :width="flexColumnWidth('organization', tableData)">
<template slot-scope="scope">
<div class="table">{{ scope.row.organization }}</div>
</template>
</el-table-column>
<el-table-column prop="feedback" label="问题反馈" :width="flexColumnWidth('feedback', tableData)">
<template slot-scope="scope">
<div class="table">{{ scope.row.feedback }}</div>
</template>
</el-table-column>
<el-table-column prop="oss_ids" label="问题照片" :width="flexColumnWidth('oss_ids', tableData)">
<template slot-scope="scope">
<div class="table">{{ scope.row.oss_ids }}</div>
</template>
</el-table-column>
<el-table-column prop="plan" label="明日计划" :width="flexColumnWidth('plan', tableData)">
<template slot-scope="scope">
<div class="table">{{ scope.row.plan }}</div>
</template>
</el-table-column>
</el-table>
<pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
:limit.sync="queryParams.pageSize" @pagination="getListPagination" />
data中数据:
data中内容
// 存放所有的表头 一定要与tableData一致
colFields: [
"reportDate",
"weather",
"workContent",
"urls",
"participantName",
"workHours",
"organization",
"feedback",
"oss_ids",
"plan",
],
spanArr: [], //存储合并单元格的开始位置
js部分
getList() {
this.queryParams.json.taskId = this.taskForm.id
let queryParams = {
pageNum: this.queryParams.pageNum,
pageSize: this.queryParams.pageSize,
json: JSON.stringify(this.queryParams.json)
}
this.tableData = []
getServiceReportList(queryParams).then(res => {
if (res.rows.length) {
res.rows.forEach(item => {
this.tableData.push(item)
})
this.total = res.total
this.getSpanArr();
} else {
this.tableData = []
}
})
},
getSpanArr() {
for (let i = 0; i < this.tableData.length; i++) {
let row = i;
if (row === 0) {
// i 表示行 j表示列
for (let j = 0; j < this.colFields.length; j++) {
this.spanArr[i * this.colFields.length + j] = {
rowspan: 1,
colspan: 1,
};
}
} else {
for (let j = 0; j < this.colFields.length; j++) {
const field = this.colFields[j];
const currentValue = this.tableData[row][field];
const previousValue = this.tableData[row - 1][field];
const currentReportDate = this.tableData[row]["reportDate"];
const previousReportDate = this.tableData[row - 1]["reportDate"];
if (field === "reportDate") {
// 日期
if (currentValue !== previousValue) {
this.spanArr[row * this.colFields.length + j] = {
rowspan: 1,
colspan: 1,
};
} else {
let beforeItem = this.spanArr[(row - 1) * this.colFields.length + j];
this.spanArr[row * this.colFields.length + j] = {
rowspan: 1 + beforeItem.rowspan,
colspan: 1,
};
beforeItem.rowspan = 0;
beforeItem.colspan = 0;
}
} else if (field === "weather" || field === 'date') {
// 天气
if (currentValue !== previousValue || currentReportDate !== previousReportDate) {
this.spanArr[row * this.colFields.length + j] = {
rowspan: 1,
colspan: 1,
};
} else {
let beforeItem = this.spanArr[(row - 1) * this.colFields.length + j];
this.spanArr[row * this.colFields.length + j] = {
rowspan: 1 + beforeItem.rowspan,
colspan: 1,
};
beforeItem.rowspan = 0;
beforeItem.colspan = 0;
}
}
else if (field === "workContent") {
// 工作内容
if (currentValue !== previousValue || currentReportDate !== previousReportDate) {
this.spanArr[row * this.colFields.length + j] = {
rowspan: 1,
colspan: 1,
};
} else {
let beforeItem = this.spanArr[(row - 1) * this.colFields.length + j];
this.spanArr[row * this.colFields.length + j] = {
rowspan: 1 + beforeItem.rowspan,
colspan: 1,
};
beforeItem.rowspan = 0;
beforeItem.colspan = 0;
}
} else if (field === "urls" || field === "participantName") {
// 照片
if (currentValue !== previousValue || this.tableData[row]["workContent"] !== this.tableData[row - 1]["workContent"] || currentReportDate !== previousReportDate) {
this.spanArr[row * this.colFields.length + j] = {
rowspan: 1,
colspan: 1,
};
} else {
let beforeItem = this.spanArr[(row - 1) * this.colFields.length + j];
this.spanArr[row * this.colFields.length + j] = {
rowspan: 1 + beforeItem.rowspan,
colspan: 1,
};
beforeItem.rowspan = 0;
beforeItem.colspan = 0;
}
}
else if (field === "feedback" || field === "oss_ids" || field === "plan") {
// 问题反馈、问题照片、明日计划
if (currentValue !== previousValue || currentReportDate !== previousReportDate) {
this.spanArr[row * this.colFields.length + j] = {
rowspan: 1,
colspan: 1,
};
} else {
let beforeItem = this.spanArr[(row - 1) * this.colFields.length + j];
this.spanArr[row * this.colFields.length + j] = {
rowspan: 1 + beforeItem.rowspan,
colspan: 1,
};
beforeItem.rowspan = 0;
beforeItem.colspan = 0;
}
} else {
// 其他列不合并
this.spanArr[row * this.colFields.length + j] = {
rowspan: 1,
colspan: 1,
};
}
}
}
}
// 对数据进行倒序处理
let stack = [];
for (let i = 0; i < this.colFields.length; i++) {
for (let j = 0; j < this.tableData.length; j++) {
if (j === 0) {
if (this.spanArr[j * this.colFields.length + i].rowspan === 0) {
stack.push(this.spanArr[j * this.colFields.length + i]);
}
} else {
if (this.spanArr[j * this.colFields.length + i].rowspan === 0) {
stack.push(this.spanArr[j * this.colFields.length + i]);
} else {
stack.push(this.spanArr[j * this.colFields.length + i]);
while (stack.length > 0) {
let pop = stack.pop();
let len = stack.length;
this.spanArr[(j - len) * this.colFields.length + i] = pop;
}
}
}
}
}
},
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
return this.spanArr[rowIndex * this.colFields.length + columnIndex];
},
此文章仅为记录一下当时做表格合并同类项 的方法,如需参考可详细看一下代码,这里不做过多解释