在网上找了一些JQuery合并列的例子,但是都是用.hide()的方式,这样导致了在导出Word的时候表格严重变形

自己写了一个用.remove()方式的合并列

 1 function arrangeTable(tableId, colNum, norowspan) {
 2 
 3             for (var i = colNum; i > 0; i--) {
 4                 var isRowspan = true;
 5                 $(norowspan).each(function () {
 6                     if (i == this)
 7                         isRowspan = false;
 8                 });
 9                 if (isRowspan)
10                     _onRowspan(i);
11             }
12 
13             function _onRowspan(_colNum) {
14                 var _TDs = $(tableId + " tr td:nth-child(" + _colNum + ")");
15                 var _firstTD = null;
16                 var _rowspanCount = 0;
17                 var _curTD = null;
18 
19                 _TDs.each(function (Idx) {
20                     if (Idx == 0) {
21                         _firstTD = $(this); 
22                         _rowspanCount = 1;
23                     } else {
24                         _curTD = $(this);
25                         if (_firstTD.text() == _curTD.text()) {
26                             _rowspanCount++;
27                             _curTD.remove();
28                             _firstTD.attr('rowSpan', _rowspanCount);
29                         } else {
30                             _firstTD = $(this);
31                             _rowspanCount = 1;
32                         }
33                     }
34                 });
35 
36             }
37 
38         }
合并列的JS代码

 调用方法如下:

1 <script type='text/javascript' language='javascript'>
2 //第一个参数:table的id
3 //第二个参数:该table共有几列
4 //第三个参数:哪些列不需要合并
5 //从1开始计数
6 arrangeTable('#Table1_0',5,[2,4]);
7 </script>
调用方法

 

posted on 2013-09-17 15:21  love7hp  阅读(460)  评论(0)    收藏  举报