el-table 行合并 span-method 方法封装

复制代码
getspan({ row, column, rowIndex, columnIndex }, dataSource) {
                var getSpanCount = function (dataItems, index, condition, isStart) {
                    if (index < dataItems.length
                        && (index == 0 || isStart || condition(dataItems[index - 1], dataItems[index])))
                        return 1 + getSpanCount(dataItems, index + 1, condition, false);
                    return 0;
                };
                
                var getRowSpanCount = function (rowIndex, dataItems, condition) {
                    if (rowIndex == 0 || !condition(dataItems[rowIndex - 1], dataItems[rowIndex])) {
                        return getSpanCount(dataItems, rowIndex, condition, true);
                    }
                    return 0;
                };

                if (columnIndex == 0) {  //合并第一列
                    return { rowspan: getRowSpanCount(rowIndex, dataSource, (x, y) => x.checkTypeId == y.checkTypeId), colspan: 1 }; 
                }

                return { rowspan: 1, colspan: 1 }
            }
复制代码

this.result 为 el-table 绑定的数据集, 表格多行合并的条件是 x.checkTypeId == y.checkTypeId

只替换上图中三处红色地方的代码,即可实现行合并。

html:

<el-table :data="dataSource" 
          :span-method="(param)=>getspan(param,dataSource)"

因我这里的el-table个数是动态的,数据源也是动态的,所以必须加一个参数,把 table 绑定的 data 传进来,方法来自网友,这下这个方法就完美了

posted on   空明流光  阅读(887)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2017-03-31 webbrowser 响应关闭事件

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示