JQuery Table 合并单元格-解决Bug版本
2024-09-09 12:19:30
网络中提供的方法是:
<script type="text/javascript">
function _w_table_rowspan(_w_table_id, _w_table_colnum) {
_w_table_firsttd = "";
_w_table_currenttd = "";
_w_table_SpanNum = 0;
_w_table_Obj = $(_w_table_id + " tr td:nth-child(" + _w_table_colnum + ")");
_w_table_Obj.each(function (i) {
if (i == 0) {
_w_table_firsttd = $(this);
_w_table_SpanNum = 1;
} else {
_w_table_currenttd = $(this);
if (_w_table_firsttd.text() == _w_table_currenttd.text()) {
_w_table_SpanNum++;
_w_table_currenttd.hide(); //remove();
_w_table_firsttd.attr("rowSpan", _w_table_SpanNum);
} else {
_w_table_firsttd = $(this);
_w_table_SpanNum = 1;
}
}
});
}
$(document).ready(function () {
_w_table_rowspan("#HZ", 1);
_w_table_rowspan("#HZ", 2);
_w_table_rowspan("#HZ", 3);
});
</script>
但这个方式存在一个bug,纯单元格的合并,不管是不是一组的,例如下面的表格,合并出来就有问题:
1 | A | E | ||
1 | A | E | ||
1 | A | E | ||
2 | B | E | ||
2 | B | E | ||
2 | B | E | ||
3 | B | E | ||
3 | C | E |
如果用上面的代码合并就变成这个样子了:
1 | A | E | ||
2 | B | |||
3 | ||||
C |
我们希望是:
1 | A | E | ||
B | ||||
2 | B | E | ||
3 | B | E | ||
C |
所以只需要做少许变更便可达到目的,在合并的时候判断上下单元格值得时候携带左边的单元格一起判断:
修改后的代码如下:
function _w_table_rowspan(_w_table_id, _w_table_colnum) {
_w_table_firsttd = "";
_w_table_currenttd = "";
_w_table_SpanNum = 0;
_w_table_Obj = $(_w_table_id + " tr td:nth-child(" + _w_table_colnum + ")");
_w_table_PreObj = $(_w_table_id + " tr td:nth-child(" + (_w_table_colnum == 1 ? 1 : _w_table_colnum - 1) + ")");
_w_table_Prefirsttd = "";
_w_table_Obj.each(function (i) {
if (i == 0) {
_w_table_firsttd = $(this);
_w_table_Prefirsttd = $(_w_table_PreObj[0]);
_w_table_SpanNum = 1;
} else {
_w_table_currenttd = $(this);
if (_w_table_Prefirsttd.text() + "-" + _w_table_firsttd.text() == $(_w_table_PreObj[i]).text() + "-" + _w_table_currenttd.text()) {
if ($(_w_table_PreObj[i]).text() + "-" + _w_table_currenttd.text() != "-") {
_w_table_SpanNum++;
_w_table_currenttd.hide(); //remove();
_w_table_firsttd.attr("rowSpan", _w_table_SpanNum);
}
} else {
_w_table_firsttd = $(this);
_w_table_Prefirsttd = $(_w_table_PreObj[i]);
_w_table_SpanNum = 1;
}
}
});
}
最新文章
- Leetcode 285. Inorder Successor in BST
- Python之路-python(Queue队列、进程、Gevent协程、Select\Poll\Epoll异步IO与事件驱动)
- Node.js下基于Express + Socket.io 搭建一个基本的在线聊天室
- 论那些年我们讨论过的Bank系统!
- 与众不同 windows phone (43) - 8.0 相机和照片: 镜头的可扩展性, 图片的可扩展性, 图片的自动上传扩展
- cocos2d回忆
- 1012 最小公倍数LCM
- [转]Chrome 控制台console的用法
- WPF基础学习第二天(高级控件)
- Java源码初学_LinkedHashMap
- BLE开发的各种坑
- C语言中for循环的使用
- PHP获取文件夹中的所有文件(包括子目录)
- MySQL的JOIN(一):用法
- github总结(3)--怎样在github上面实现页面托管
- loadrunner11迭代录制注册账号
- 增加nginx虚拟主机配置文件(conf.d)
- 蓝桥杯-k倍区间
- Python在ubuntu下常用开发包名称
- Smarty带来的神秘的数字1
热门文章
- 【转载】java前后端 动静分离,JavaWeb项目为什么我们要放弃jsp?
- JAVA中Set集合--HashSet的使用
- MySql计算两个日期的时间差函数
- nginx 有关防盗链的设置
- php分享三十一:编程中的一些特殊写法
- 【Unity】8.3 布局模式(GUILayout)
- 数据库存储引擎 show engines 修改引擎
- 36氪首发 | 「myShape」完成千万级人民币 Pre-A轮融资,推出 AI 智能健身私教
- Mathematica .nb程序运行不下去的原因
- Up and running with Apache Spark on Apache Kudu