forked from th3uiguy/jquery-scrolltable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.scrolltable.js
More file actions
137 lines (119 loc) · 4.23 KB
/
jquery.scrolltable.js
File metadata and controls
137 lines (119 loc) · 4.23 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
/**
* jQuery Scroll Table
*
* @fileoverview
* @link https://github.com/th3uiguy/jquery-scrolltable
* @author Spencer Neese
* @version 0.5.2
* @requires jQuery UI 1.7+ and jQuery 1.3.2+
* @license jQuery Scroll Table Plug-in
*
* Copyright 2012, Spencer Neese
* Dual licensed under the MIT or GPL Version 2 licenses.
* <https://raw.github.com/th3uiguy/jquery-scrolltable/master/GPL-LICENSE.txt>
* <https://raw.github.com/th3uiguy/jquery-scrolltable/master/MIT-LICENSE.txt>
*/
(function($) {
$.widget( "ui.scrolltable", {
options: {
height: 'auto',
maxHeight: 300,
stripe: false,
setWidths: true,
oddClass: "st-tr-odd",
evenClass: "st-tr-even",
firstClass: "st-tr-first",
lastClass: "st-tr-last"
},
_create: function(){
var self = this;
var $self = $(this.element);
var opts = this.options;
this._convertTable($self);
if(opts.stripe === true) this._stripe($self);
var padding = $self.outerWidth() - $self.find('.st-body-table').outerWidth();
$self.find('.st-head').css('padding-right', padding + 'px');
},
destroy: function(){
var self = this;
var $self = $(this.element);
$self.removeClass('st-container')
.find('>thead').replaceWith($self.find('.st-head-table>thead')).end()
.find('>tbody').replaceWith($self.find('.st-body-table>tbody')).end()
.find('.st-head').closest('tr').remove().end()
.find('.st-body').closest('tr').remove();
$self.find('>thead th, >thead td')
.each(function(){
$(this).prop('colspan', $(this).data('colspan'));
})
.add($self.find('>tbody>tr').eq(0).find('td')).width(function(){
return $(this).data('prevWidth') || 'auto';
});
var opts = this.options;
$self.find('tr')
.removeClass(opts.oddClass)
.removeClass(opts.evenClass)
.removeClass(opts.firstClass)
.removeClass(opts.lastClass);
$.Widget.prototype.destroy.call(self);
},
_convertTable: function($container){
var opts = this.options;
if(opts.setWidths) this._setWidths($container);
var $head = $('<table class="st-head-table" cellpadding="0" cellspacing="0" border="0" />').append($container.find('>thead')).css('width', '100%');
var $body = $('<table class="st-body-table" cellpadding="0" cellspacing="0" border="0" />').append($container.find('>tbody')).css('width', '100%');
$container
.addClass('st-container')
.html('<thead><tr><td class="st-head"></td></tr></thead><tbody><tr><td class="st-body"><div class="st-body-scroll"></div></td></tr></tbody>')
.find('.st-head').css('padding', '0 20px 0 0').append($head).end()
.find('.st-body').css('padding', '0')
.find('.st-body-scroll').css('overflow-y', 'auto').append($body)
.find('tr')
.first().addClass(opts.firstClass).end()
.last().addClass(opts.lastClass);
if(isFinite(opts.height)){
$container.find('.st-body-scroll').css('height', opts.height + "px");
}
else if(isFinite(opts.maxHeight)){
$container.find('.st-body-scroll').css('max-height', opts.maxHeight + "px");
}
$container.find('.st-head thead th, .st-head thead td').each(function(){
$(this).data('colspan', $(this).prop('colspan')).removeProp('colspan');
});
},
_stripe: function($container){
var opts = this.options;
$container.find('.st-body-scroll>table>tbody>tr')
.filter(':odd').addClass(opts.oddClass).end()
.filter(':even').addClass(opts.evenClass);
},
_setWidths: function($container){
var total = 100;
var totalWidth = 0;
var $headCols = $container.find('thead th, thead td');
var $bodyCols = $container.find('tbody tr').eq(0).find('td');
var colCount = $headCols.size();
var $col, width, stop = colCount - 1;
for(var i = 0; i < stop; i++){
$col = $headCols.eq(i);
width = $col.prop('width') || $col[0].style.width;
$col.data('prevWidth', width);
if(typeof width === "string" && width.length === 0){
width = Math.floor($col.width()/$container.width()*100) + "%";
}
$col.css('width', width);
$bodyCols.eq(i).css('width', width);
totalWidth += parseFloat(width);
total -= parseFloat(width);
}
$col = $headCols.eq(stop);
width = $col.prop('width') || $col[0].style.width;
$col.data('prevWidth', width);
if(typeof width === "string" && width.length === 0){
width = total + "%";
}
$headCols.eq(stop).css('width', width);
$bodyCols.eq(stop).css('width', width);
}
});
})(jQuery);