el-table表格合并同类项(指定列)

<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];
        },

此文章仅为记录一下当时做表格合并同类项 的方法,如需参考可详细看一下代码,这里不做过多解释

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值