forked from lai32290/TableHeadFixer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtableHeadFixer.js
More file actions
321 lines (253 loc) · 7.83 KB
/
tableHeadFixer.js
File metadata and controls
321 lines (253 loc) · 7.83 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
(function($) {
$.fn.tableHeadFixer = function(param) {
var defaults = {
head: true,
foot: false,
left: 0,
right: 0,
'z-index': 0
};
var settings = $.extend({}, defaults, param);
return this.each(function() {
settings.table = this;
settings.parent = $(settings.table).parent();
setParent();
if(settings.head == true)
fixHead();
if(settings.foot == true)
fixFoot();
if(settings.left > 0)
fixLeft();
if(settings.right > 0)
fixRight();
setCorner();
$(settings.parent).trigger("scroll");
$(window).resize(function() {
$(settings.parent).trigger("scroll");
});
});
/*
This function solver z-index problem in corner cell where fix row and column at the same time,
set corner cells z-index 1 more then other fixed cells
*/
function setCorner() {
var table = $(settings.table);
if(settings.head) {
if(settings.left > 0) {
var tr = table.find("thead tr");
tr.each(function(k, row) {
solverLeftColspan(row, function(cell) {
$(cell).css("z-index", settings['z-index'] + 1);
});
});
}
if(settings.right > 0) {
var tr = table.find("thead tr");
tr.each(function(k, row) {
solveRightColspan(row, function(cell) {
$(cell).css("z-index", settings['z-index'] + 1);
});
});
}
}
if(settings.foot) {
if(settings.left > 0) {
var tr = table.find("tfoot tr");
tr.each(function(k, row) {
solverLeftColspan(row, function(cell) {
$(cell).css("z-index", settings['z-index']);
});
});
}
if(settings.right > 0) {
var tr = table.find("tfoot tr");
tr.each(function(k, row) {
solveRightColspan(row, function(cell) {
$(cell).css("z-index", settings['z-index']);
});
});
}
}
}
// Set style of table parent
function setParent() {
var parent = $(settings.parent);
var table = $(settings.table);
parent.append(table);
parent
.css({
'overflow-x' : 'auto',
'overflow-y' : 'auto'
});
parent.scroll(function() {
var scrollWidth = parent[0].scrollWidth;
var clientWidth = parent[0].clientWidth;
var scrollHeight = parent[0].scrollHeight;
var clientHeight = parent[0].clientHeight;
var top = parent.scrollTop();
var left = parent.scrollLeft();
if(settings.head)
this.find("thead tr > *").css("top", top);
if(settings.foot)
this.find("tfoot tr > *").css("bottom", scrollHeight - clientHeight - top);
if(settings.left > 0)
settings.leftColumns.css("left", left);
if(settings.right > 0)
settings.rightColumns.css("right", scrollWidth - clientWidth - left);
}.bind(table));
}
// Set table head fixed
function fixHead () {
var thead = $(settings.table).find("thead");
var tr = thead.find("tr");
var cells = thead.find("tr > *");
setBackground(cells);
cells.css({
'position' : 'relative'
});
}
// Set table foot fixed
function fixFoot () {
var tfoot = $(settings.table).find("tfoot");
var tr = tfoot.find("tr");
var cells = tfoot.find("tr > *");
setBackground(cells);
cells.css({
'position' : 'relative'
});
}
// Set table left column fixed
function fixLeft () {
var table = $(settings.table);
// var fixColumn = settings.left;
settings.leftColumns = $();
var tr = table.find("tr");
tr.each(function(k, row) {
solverLeftColspan(row, function(cell) {
settings.leftColumns = settings.leftColumns.add(cell);
});
// var inc = 1;
// for(var i = 1; i <= fixColumn; i = i + inc) {
// var nth = inc > 1 ? i - 1 : i;
// var cell = $(row).find("*:nth-child(" + nth + ")");
// var colspan = cell.prop("colspan");
// settings.leftColumns = settings.leftColumns.add(cell);
// inc = colspan;
// }
});
var column = settings.leftColumns;
column.each(function(k, cell) {
var cell = $(cell);
setBackground(cell);
cell.css({
'position' : 'relative'
});
});
}
// Set table right column fixed
function fixRight () {
var table = $(settings.table);
var fixColumn = settings.right;
settings.rightColumns = $();
var tr = table.find("tr");
tr.each(function(k, row) {
solveRightColspan(row, function(cell) {
settings.rightColumns = settings.rightColumns.add(cell);
});
});
var column = settings.rightColumns;
column.each(function(k, cell) {
var cell = $(cell);
setBackground(cell);
cell.css({
'position' : 'relative'
});
});
}
// Set fixed cells backgrounds
function setBackground(elements) {
elements.each(function(k, element) {
var element = $(element);
var parent = $(element).parent();
var elementBackground = element.css("background-color");
elementBackground = (elementBackground == "transparent" || elementBackground == "rgba(0, 0, 0, 0)") ? null : elementBackground;
var parentBackground = parent.css("background-color");
parentBackground = (parentBackground == "transparent" || parentBackground == "rgba(0, 0, 0, 0)") ? null : parentBackground;
var background = parentBackground ? parentBackground : "white";
background = elementBackground ? elementBackground : background;
element.css("background-color", background);
});
}
function solverLeftColspan(row, action) {
var fixColumn = settings.left;
var inc = 1;
for(var i = 1; i <= fixColumn; i = i + inc) {
var nth = inc > 1 ? i - 1 : i;
var cell = $(row).find("> *:nth-child(" + nth + ")");
var colspan = cell.prop("colspan");
if (cell.cellPos().left < fixColumn) {
action(cell);
}
inc = colspan;
}
}
function solveRightColspan(row, action) {
var fixColumn = settings.right;
var inc = 1;
for(var i = 1; i <= fixColumn; i = i + inc) {
var nth = inc > 1 ? i - 1 : i;
var cell = $(row).find("> *:nth-last-child(" + nth + ")");
var colspan = cell.prop("colspan");
action(cell);
inc = colspan;
}
}
};
})(jQuery);
/* cellPos jQuery plugin
---------------------
Get visual position of cell in HTML table (or its block like thead).
Return value is object with "top" and "left" properties set to row and column index of top-left cell corner.
Example of use:
$("#myTable tbody td").each(function(){
$(this).text( $(this).cellPos().top +", "+ $(this).cellPos().left );
});
*/
(function($){
/* scan individual table and set "cellPos" data in the form { left: x-coord, top: y-coord } */
function scanTable( $table ) {
var m = [];
$table.children( "tr" ).each( function( y, row ) {
$( row ).children( "td, th" ).each( function( x, cell ) {
var $cell = $( cell ),
cspan = $cell.attr( "colspan" ) | 0,
rspan = $cell.attr( "rowspan" ) | 0,
tx, ty;
cspan = cspan ? cspan : 1;
rspan = rspan ? rspan : 1;
for( ; m[y] && m[y][x]; ++x ); //skip already occupied cells in current row
for( tx = x; tx < x + cspan; ++tx ) { //mark matrix elements occupied by current cell with true
for( ty = y; ty < y + rspan; ++ty ) {
if( !m[ty] ) { //fill missing rows
m[ty] = [];
}
m[ty][tx] = true;
}
}
var pos = { top: y, left: x };
$cell.data( "cellPos", pos );
} );
} );
};
/* plugin */
$.fn.cellPos = function( rescan ) {
var $cell = this.first(),
pos = $cell.data( "cellPos" );
if( !pos || rescan ) {
var $table = $cell.closest( "table, thead, tbody, tfoot" );
scanTable( $table );
}
pos = $cell.data( "cellPos" );
return pos;
}
})(jQuery);