-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtablecellmerger.js
More file actions
147 lines (123 loc) · 4.96 KB
/
tablecellmerger.js
File metadata and controls
147 lines (123 loc) · 4.96 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/********************************************************************************
* Class: TableCellMerger
* Description: Merges similar cells in given row or column in supplied table.
* Usage: Supply id of the table you want to merge.
* xmerge() - Merges similar cells lining in a row (affects all rows).
* ymerge() - Merges similar cells lining in a column (affects all columns).
*
* You may provide options if you want to merge cells in specific rows(columns).
* There are two types of options: include and exclude.
*
* xmerge('include: 1, 2, 3') => Merges similar cells lining in 1,2,3rd rows.
* xmerge('exclude: 1, 2, 3') => Merges similar cells lining in all rows except cells in 1,2,3rd rows.
*
* ymerge('include: 5, 6') => Merges similar cells lining in 1,2,3rd columns.
* ymerge('exclude: 8, 9') => Merges similar cells lining in all rows except cells in 1,2,3rd columns.
*
* merge() => Calls ymerge() then calls xmerge().
*
*
* Example: new TableCellMerger('id_of_table_to_be_merged').merge();
* new TableCellMerger('id_of_table_to_be_merged').ymerge();
* new TableCellMerger('id_of_table_to_be_merged').xmerge();
*
* new TableCellMerger('id_of_table_to_be_merged').xmerge('include: 1, 2');
* new TableCellMerger('id_of_table_to_be_merged').ymerge('exclude: 3, 4');
*
* new TableCellMerger('id_of_table_to_be_merged').xmerge('include: 1, 2').ymerge('exclude: 3, 4');
* OR
* new TableCellMerger('id_of_table_to_be_merged').ymerge('exclude: 3, 4').xmerge('include: 1, 2');
*
********************************************************************************
*
* Copyright (c) 2009 Battur Sanchin
*
* {
* :homepage => 'http://battur.blogspot.com',
* :email => 'batturjapan@gmail.com',
* :flickr => 'http://flickr.com/photos/battur'
* }
*
* ------------------------------------------------------------------------------
*
* License: MIT License
*
********************************************************************************
*/
function TableCellMerger(tableId){
this.incArr = new Array();
this.excArr = new Array();
this.tr = document.getElementById(tableId)
.getElementsByTagName("tbody")[0]
.getElementsByTagName("tr");
this.loadOption=function(option){
if(option != null && option.match(/^(inc|exc)lude:( ?\d+,)* ?\d+$/)){
var optionVal = option.slice(8);
if(option.match(/^include/))
// add -1 front, otherwise it will create n elements of
// empty array if you put single 'n' option
eval("this.incArr = new Array(-1," + optionVal + ");")
else
eval("this.excArr = new Array(-1," + optionVal + ");")
} else if(option != null)
throw("Option is invalid: '" + option + "'");
}
this.ymerge = function(option){
// load supplied option here.
this.loadOption(option);
var tr = this.tr;
var numCols = tr[tr.length - 1].cells.length;
for(var j = 0; j < numCols; j++){
var spanningCell = null;
var comparingCell = null;
if((this.incArr.length > 0 && this.incArr.indexOf(j + 1) == -1)
|| (this.excArr.length > 0 && this.excArr.indexOf(j + 1) != -1))
continue;
for(i = 0; i < tr.length - 1; i++){
td = tr[i].getElementsByTagName("td");
spanningCell = (spanningCell == null ? td[j] : spanningCell);
comparingCell = tr[i+1].getElementsByTagName("td")[j];
if(spanningCell != null
&& spanningCell.innerHTML == comparingCell.innerHTML
&& spanningCell.colSpan == comparingCell.colSpan){
comparingCell.style.display = "none";
spanningCell.rowSpan += 1;
} else
spanningCell = comparingCell;
}
}
return this;
}
this.xmerge = function(option){
// load supplied option here.
this.loadOption(option);
var tr = this.tr;
var numCols = tr[tr.length - 1].cells.length;
for(i = 0; i < tr.length; i++){
var spanningCell = null;
var comparingCell = null;
if((this.incArr.length > 0 && this.incArr.indexOf(i + 1) == -1)
|| (this.excArr.length > 0 && this.excArr.indexOf(i + 1) != -1))
continue;
for(var j = 0; j < numCols - 1; j++){
td = tr[i].getElementsByTagName("td");
spanningCell = (spanningCell == null ? td[j] : spanningCell);
comparingCell = tr[i].getElementsByTagName("td")[j+1];
if(spanningCell != null
&& spanningCell.innerHTML == comparingCell.innerHTML
&& spanningCell.rowSpan == comparingCell.rowSpan){
comparingCell.style.display = "none";
spanningCell.colSpan += 1;
} else
spanningCell = comparingCell;
}
}
return this;
}
this.merge = function(){
this.ymerge(null);
this.xmerge(null);
return this;
}
return this;
}