fixed_table_rc.js
6.87 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
/*
A jQuery plugin to convert a well formatted table into a table with fixed
rows and columns.
Copyright 2011-2015 Selvakumar Arumugam
http://meetselva.github.io/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function ($) {
$.fn.fxdHdrCol = function (o) {
var cfg = {
height: 0,
width: 0,
fixedCols: 0,
colModal: [],
tableTmpl: function () {
return '<table />';
},
sort: false
};
$.extend(cfg, o);
return this.each (function () {
var lc = {
ft_container: null,
ft_rel_container: null,
ft_wrapper: null,
ft_rc: null,
ft_r: null,
ft_c: null,
tableWidth: 0
};
var $this = $(this);
$this.addClass('ui-widget-header');
$this.find('tbody tr').addClass('ui-widget-content');
$this.wrap('<div class="ft_container" />');
lc.ft_container = $this.parent().css({width: cfg.width, height: cfg.height});
var $ths = $('thead tr', $this).first().find('th');
if (cfg.sort && sorttable && cfg.fixedCols == 0) {
$ths.addClass('fx_sort_bg');
}
var $thFirst = $ths.first();
var thSpace = parseInt($thFirst.css('paddingLeft'), 10) + parseInt($thFirst.css('paddingRight'), 10);
/* set width and textAlign from colModal */
var ct = 0;
$ths.each(function (i, el) {
var calcWidth = 0;
for (var j = 0; j < el.colSpan; j++) {
calcWidth += cfg.colModal[ct].width;
ct++;
}
$(el).css({width: calcWidth, textAlign: cfg.colModal[ct-1].align});
lc.tableWidth += calcWidth + thSpace + ((i == 0)?2:1);
});
$('tbody', $this).find('tr').each(function (i, el) {
$('td', el).each(function (i, tdel) {
tdel.style.textAlign = cfg.colModal[i].align;
});
});
$this.width(lc.tableWidth);
//add relative container
$this.wrap('<div class="ft_rel_container" />');
lc.ft_rel_container = $this.parent();
//add wrapper to base table which will have the scrollbars
$this.wrap('<div class="ft_scroller" />');
lc.ft_wrapper = $this.parent().css('width', cfg.width - 5);
var theadTr = $('thead', $this);
//clone the thead->tr
var theadTrClone = theadTr.clone();
//construct fixed row (full row)
lc.ft_rel_container
.prepend($(cfg.tableTmpl(), {'class': 'ft_r ui-widget-header'})
.append(theadTrClone));
//an instance of fixed row
lc.ft_r = $('.ft_r', lc.ft_rel_container);
lc.ft_r.wrap($('<div />', {'class': 'ft_rwrapper'}));
lc.ft_r.width(lc.tableWidth);
if (cfg.fixedCols > 0) {
//clone the thead again to construct the
theadTrClone = theadTr.clone();
//calculate the actual column's count (support for colspan)
var r1c1ColSpan = 0;
for (var i = 0; i < cfg.fixedCols; i++ ) {
r1c1ColSpan += this.rows[0].cells[i].colSpan;
}
//prepare rows/cols for fixed row col section
$('tr', theadTrClone).each(function () {
var tdct = 0;
$(this).find('th').filter(function() {
tdct += this.colSpan;
return tdct > r1c1ColSpan;
}).remove();
});
//add fixed row col section
lc.ft_rel_container
.prepend($(cfg.tableTmpl(), {'class': 'ft_rc ui-widget-header'})
.append(theadTrClone));
//an instance of fixed row column
lc.ft_rc = $('.ft_rc', lc.ft_rel_container);
//now clone the fixed row column and append tbody for the remaining rows
lc.ft_c = lc.ft_rc.clone();
lc.ft_c[0].className = 'ft_c';
//append tbody
lc.ft_c.append('<tbody />');
//append row by row while just keeping the frozen cols
var ftc_tbody = lc.ft_c.find('tbody');
$.each ($this.find('tbody > tr'), function (idx, el) {
var tr = $(el).clone();
tdct = 0;
tr.find('td').filter(function (){
tdct += this.colSpan;
return tdct > r1c1ColSpan;
}).remove();
ftc_tbody.append(tr);
});
lc.ft_rc.after(lc.ft_c);
lc.ft_c.wrap($('<div />', {'class': 'ft_cwrapper'}));
var tw = 0;
for (var i = 0; i < cfg.fixedCols; i++) {
tw += $(this.rows[0].cells[i]).outerWidth(true);
}
lc.ft_c.add(lc.ft_rc).width(tw);
lc.ft_c.height($this.outerHeight(true));
//set height of fixed_rc and fixed_c
for (var i = 0; i < this.rows.length; i++) {
var ch = $(this.rows[i]).outerHeight();
var fch = $(lc.ft_c[0].rows[i]).outerHeight(true);
ch = (ch>fch)?ch:fch;
if (i < lc.ft_rc[0].rows.length) {
$(lc.ft_r[0].rows[i])
.add(lc.ft_rc[0].rows[i])
.height(ch);
}
$(lc.ft_c[0].rows[i])
.add(this.rows[i])
.height(ch);
}
lc.ft_c
.parent()
.css({height: lc.ft_container.height() - 17})
.width(lc.ft_rc.outerWidth(true) + 1);
}
lc.ft_r
.parent()
.css({width: lc.ft_wrapper.width()- 17});
//events (scroll and resize)
lc.ft_wrapper.scroll(function () {
if (cfg.fixedCols > 0) {
lc.ft_c.css('top', ($(this).scrollTop()*-1));
}
lc.ft_r.css('left', ($(this).scrollLeft()*-1));
});
/*$(window).on('resize', function () {
lc.ft_r
.parent()
.css({width: lc.ft_rel_container.width()- 17});
});*/
if (cfg.sort && sorttable && cfg.fixedCols == 0) {
$('table', lc.ft_container).addClass('sorttable');
sorttable.makeSortable(this);
var $sortableTh = $('.fx_sort_bg', lc.ft_rel_container);
$sortableTh.click (function () {
var $this = $(this);
var isAscSort = $this.hasClass('fx_sort_asc');
$sortableTh.removeClass('fx_sort_asc fx_sort_desc');
if (isAscSort) {
$this.addClass('fx_sort_desc').removeClass('fx_sort_asc');
} else {
$this.addClass('fx_sort_asc').removeClass('fx_sort_desc');
}
var idx = $(this).index();
sorttable.innerSortFunction.apply(lc.ft_wrapper.find('th').get(idx), []);
});
}
});
};
})(jQuery);