elementUI非常规数据格式渲染复杂表格(副表头、合并单元格)

效果

数据源

前端代码 (展示以及表格处理/数据处理)

复制代码
标签
<el-table :data="dataList" style="width: 100%" :span-method="objectSpanMethod">
                <template v-for="(item, index) in headers">
                    <el-table-column prop="" :label="item" align="center">
                        <template slot-scope="scope">
                            <div>
                                {{ scope.row[index] }}
                            </div>
                        </template>
                        <el-table-column prop="" label="姓名" v-if="index >= 3" align="center">
                            <template slot-scope="scope">
                                {{ scope.row[index + index - 3] }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="" label="手机号" v-if="index >= 3" align="center">
                            <template slot-scope="scope">
                                {{ scope.row[index + index - 2] }}
                            </template>
                        </el-table-column>
                    </el-table-column>
                </template>
            </el-table>

JS代码

data
 dataList: [],
 headers: [],
 spanArr: [],

methods
 getSpanArr(data) {
            for (var i = 0; i < data.length; i++) {
                if (i === 0) {
                    this.spanArr.push(1);
                    this.pos = 0
                } else {
                    // 判断当前元素与上一个元素是否相同
                    if (data[i][0] === data[i - 1][0]) {
                        this.spanArr[this.pos] += 1;
                        this.spanArr.push(0);
                    } else {
                        this.spanArr.push(1);
                        this.pos = i;
                    }
                }
            }
        },

// 合并单元格
        objectSpanMethod({ row, column, rowIndex, columnIndex }) {
            if (columnIndex === 0 || columnIndex === 1 ) {
                const _row = this.spanArr[rowIndex];
                const _col = _row > 0 ? 1 : 0;
                return {
                    rowspan: _row,
                    colspan: _col
                }
            }

        }
相关推荐
lally.1 分钟前
2025御网杯wp(web,misc,crypto)
前端·ctf
海绵不是宝宝8177 分钟前
React+Springboot项目部署ESC服务器
前端·react.js·前端框架
前端小崔10 分钟前
从零开始学习three.js(15):一文详解three.js中的纹理映射UV
前端·javascript·学习·3d·webgl·数据可视化·uv
belldeep15 分钟前
vite:npm 安装 pdfjs-dist , PDF.js View 预览功能示例
javascript·pdf·pdfjs-dist·pdf.worker
ZHOU_WUYI24 分钟前
React 实现 JWT 登录验证的最小可运行示例
前端·react.js·前端框架
一只程序熊32 分钟前
【uniapp】errMsg: “navigateTo:fail timeout“
服务器·前端·uni-app
Lysun0011 小时前
electron 结合 react(cra创建的) 创建桌面应用和打包桌面应用
javascript·react.js·electron
TE-茶叶蛋2 小时前
Nodejs核心机制
前端
pink大呲花2 小时前
动态路由实现原理及前端控制与后端控制的核心差异
前端
Hopebearer_2 小时前
什么是CacheStorage?
前端·javascript·web