当前位置: 首页 > news >正文

实现表格合并

  <template><div class="table-merge-example"><h2 class="title">Vue2 表格数据合并示例</h2><div class="table-container"><h3>横向合并(列合并)</h3><table class="merge-table horizontal-merge"><thead><tr><th>数值</th></tr></thead><tbody><tr v-for="(item, index) in processedHorizontalData" :key="index"><td :colspan="item.span" :class="{ 'merged-cell': item.span > 1 }">{{ item.value }}</td></tr></tbody></table></div><div class="table-container"><h3>纵向合并(行合并)</h3><table class="merge-table vertical-merge"><thead><tr><th>序号</th><th>数值</th></tr></thead><tbody><tr v-for="(item, index) in originalData" :key="index"><td>{{ index + 1 }}</td><td :rowspan="verticalSpans[index]" :class="{ 'merged-cell': verticalSpans[index] > 1 }"v-if="verticalSpans[index] > 0">{{ item }}</td></tr></tbody></table></div></div></template><script>export default {data() {return {originalData: [200, 300, 300, 300, 400, 400, 500],processedHorizontalData: [],verticalSpans: []};},mounted() {this.processHorizontalMerge();this.processVerticalMerge();},methods: {// 处理横向合并(列合并)processHorizontalMerge() {if (this.originalData.length === 0) {return;}const result = [];let currentValue = this.originalData[0];let count = 1;// 从第二个元素开始遍历for (let i = 1; i < this.originalData.length; i++) {if (this.originalData[i] === currentValue) {count++;} else {// 保存当前组result.push({ value: currentValue, span: count });// 开始新的一组currentValue = this.originalData[i];count = 1;}}// 添加最后一组result.push({ value: currentValue, span: count });this.processedHorizontalData = result;},// 处理纵向合并(行合并)processVerticalMerge() {if (this.originalData.length === 0) {return;}const spans = new Array(this.originalData.length).fill(0);let currentValue = this.originalData[0];let count = 1;spans[0] = 1;// 从第二个元素开始遍历for (let i = 1; i < this.originalData.length; i++) {if (this.originalData[i] === currentValue) {count++;spans[i] = 0; // 被合并的单元格} else {// 设置上一组的合并数量spans[i - count] = count;// 开始新的一组currentValue = this.originalData[i];count = 1;spans[i] = 1;}}// 设置最后一组的合并数量spans[this.originalData.length - count] = count;this.verticalSpans = spans;}}};</script><style scoped>.table-merge-example {max-width: 800px;margin: 20px auto;padding: 20px;font-family: Arial, sans-serif;}.title {text-align: center;color: #333;margin-bottom: 30px;}.table-container {margin-bottom: 40px;border: 1px solid #e0e0e0;border-radius: 6px;padding: 15px;box-shadow: 0 2px 4px rgba(0,0,0,0.1);}.table-container h3 {color: #444;margin-top: 0;margin-bottom: 15px;padding-bottom: 10px;border-bottom: 1px solid #eee;}.merge-table {width: 100%;border-collapse: collapse;text-align: center;}.merge-table th,.merge-table td {padding: 12px 15px;border: 1px solid #ddd;}.merge-table th {background-color: #f5f5f5;font-weight: bold;color: #555;}.merge-table tr:nth-child(even) {background-color: #f9f9f9;}.merge-table tr:hover {background-color: #f0f7ff;}.merged-cell {background-color: #e6f7ff;font-weight: bold;color: #1890ff;}.horizontal-merge td {border-right: 2px solid #fff;}.vertical-merge td {border-bottom: 2px solid #fff;}</style>
http://www.jsqmd.com/news/37274/

相关文章:

  • 实用指南:Android studio 高效使用
  • 2025年11月中国抗衰老设备技术排行榜:前沿科技与健康衰老新选择
  • Windows 下安装 swoole 图文教程(php)
  • 2025.11.11
  • 银河麒麟高级服务器操作系统V10SP1(ARM)【auditd服务内存泄露】问题解决方法
  • 【2025-11-10】中年篮球
  • 基于ITIL流程的ITSM平台对比选型:如何选择最贴合自身需求、能真正落地ITIL价值的ITSM工具?
  • 树上的巧克力-树形DP
  • 支付宝对接问题归类
  • PRISMA 简介:系统综述和荟萃分析(meta分析)的首选报告项目
  • Topaz Video AI v1.0.5高级版:视频修复黑科技
  • 2025年重庆小程序服务商排名前十强:杰诚智享科技领跑行业
  • 详细介绍:.net AI MCP 入门 适用于模型上下文协议的 C# SDK 简介(MCP)
  • 2025年11月重庆互联网公司推荐排行榜:杰诚智享领跑榜单
  • 一根网线同时接网线和电话线方法
  • NGINX WEBUI Docker 容器化部署指南
  • 2025年重庆抖音推广公司口碑排行榜前十强发布
  • 供应商协同平台有哪些?主要特征与优势是什么?
  • codeql中java相关ql规则一些记录
  • 常见的文件摆渡系统及其安全性与效率分析
  • AI是风口还是泡沫?一个独立开发者的冷思考
  • 银河麒麟桌面操作系统V10SP1(全X86/ARM架构)【ukui-kwin-x11进程占用CPU内存较高】问题解决方法
  • 自动生成提示
  • C. Trinity
  • dongtai-java
  • 雷池 WAF 免费版实测:小白用 Nginx 搭环境,30 分钟护住跨境电商安全
  • Luogu P9128 [USACO23FEB] Fertilizing Pastures G 题解
  • Docker核心概念:镜像、容器、仓库的本质与关联
  • 【知识分享】怎么建立受控的内外网文件传输通道?
  • 什么是文件摆渡系统?数据跨网“桥梁”与安全“卫士”!