-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.scrollTable.js
146 lines (126 loc) · 3.99 KB
/
jquery.scrollTable.js
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
/*
* scrollTable - jQuery scrollable table plugin
*
* Copyright (c) 2015 Matus Matula
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* http://www.github.com/Ciki/jquery.scrollTable
*
* Usage:
* $('.table-scrollable').scrollTable();
*
* you can also specify preferred width on th elements
* using html attribute width or inline style like:
* <th width="100"> or <th style="width:100px">
*
* This plugin stores some data on html elements
* using dataset with prefix `jst` === jquery scroll table
*
*/
/**
* Version 1.0
*
*
* @name scrollTable
* @type jQuery
* @usage $('.table-scrollable').scrollTable();
*/
(function ($) {
$.fn.scrollTable = function (options)
{
var $tables = this;
// adjust the width of cells when window resizes
$(window).resize(function () {
_makeTablesScrollable($tables);
}).resize(); // trigger resize handler
// enable jQuery chain
return this;
};
function _makeTablesScrollable($tables) {
$tables.each(function (i, table) {
var $table = $(table);
var $tbody = $table.find('tbody'),
tbodyW = parseInt($tbody.css('width')),
$tbodyRows = $tbody.find('tr'),
$trFirst = $tbodyRows.eq(0),
$tbodyCells = $trFirst.children(),
numberOfCells = $tbodyCells.length, // number of columns
numberOfModifiableWidthCells = numberOfCells, // number of columns with no preferred width defined
isInitialized = $table.data('jstInitialized') || false, // has scrollTable been initialized?
$theadCells = $table.find('thead tr').children(),
cellWidthAdjustment = 0, // int, how many pixels needs to be added to every cell to fill whole row
scrollbarWidth = 20; // int, in pixels
// initialize => set preferred widths if present
if (!isInitialized) {
$theadCells.each(function () {
var $el = $(this);
var preferredWidth = parseInt($el.prop('style').width || $el.attr('width'));
if (preferredWidth) {
numberOfModifiableWidthCells--;
}
$el.data('jstPreferredWidth', preferredWidth);
});
$table.data('jstInitialized', true);
$table.data('jstModifiableWidthColumnsNumber', numberOfModifiableWidthCells);
} else {
numberOfModifiableWidthCells = $table.data('jstModifiableWidthColumnsNumber');
}
// is tbody scrollable?
var bodyContentH = 0;
$tbodyRows.each(function () {
bodyContentH += $(this).height();
});
var isBodyScrollable = $tbody.height() < bodyContentH;
// reset both theady & tbody cell widths so widthDiff can be reliably computed
// use preferredWidth if specified, defaults to 'auto'
$tbodyCells.each(function (i, v) {
var $td = $(v),
$th = $theadCells.eq(i),
thPreferredW = $th.data('jstPreferredWidth'),
w = thPreferredW || 'auto';
$th.css('width', w);
$td.css('width', w);
});
// compute width difference between tbody & first row,
// used to adjust cell widths to fit 100% table width
var widthDiff = tbodyW - parseInt($trFirst.css('width'));
if (widthDiff) {
cellWidthAdjustment = Math.floor(widthDiff / numberOfModifiableWidthCells);
}
// Set the width of thead & tbody columns
var totalTdW = 0;
$tbodyCells.each(function (i, v) {
var $td = $(v),
tdW = parseInt($td.css('width')),
$th = $theadCells.eq(i),
thPreferredW = $th.data('jstPreferredWidth'),
tdNewW = 0,
thNewW = 0;
// compute width for last cell to fill whole row
if (i === numberOfCells - 1) {
if (thPreferredW) {
tdNewW = thNewW = thPreferredW;
} else {
tdNewW = thNewW = tbodyW - totalTdW;
}
// subtract scrollbarWidth from td:last width if isBodyScrollable
if (isBodyScrollable) {
tdNewW -= scrollbarWidth;
}
} else {
if (thPreferredW) {
tdNewW = thNewW = thPreferredW;
} else {
tdNewW = thNewW = tdW + cellWidthAdjustment;
}
}
totalTdW += tdNewW;
$td.css('width', tdNewW);
$th.css('width', thNewW);
});
});
}
})(jQuery);