-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.webks-responsive-table.js
340 lines (317 loc) · 11.3 KB
/
jquery.webks-responsive-table.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* @file Contains the jQuery "webks Responsive Table" Plugin.
*
* @version 1.0.0
* @since 2012-08-20
* @see Project home:
* @category responsive webdesign, jquery
* @author webks:websolutions kept simple - Julian Pustkuchen & Thomas Frobieter
* GbR | http://www.webks.de
* @copyright webks:websolutions kept simple - Julian Pustkuchen & Thomas
* Frobieter GbR | http://www.webks.de
*/
(function($) {
/*
* Usage Examples:
* -- Simple: -- Make all tables responsible using the default settings.
* $('table').responsiveTable();
* -- Custom configuration example 1 (Disable manual switch): --
* $('table').responsiveTable({ showSwitch: false });
* -- Custom configuration example 2 (Use different selectors): --
* $('table').responsiveTable({ headerSelector: 'tr th', bodyRowSelector:
* 'tr', });
* -- Custom configuration example 3 (Use different screensize in dynamic
* mode): -- $('table').responsiveTable({ displayResponsiveCallback:
* function() { return $(document).width() < 500; // Show responsive if screen
* width < 500px }, });
* -- Custom configuration example 4 (Make ALL tables responsive - regardless
* of screensize): -- $('table').responsiveTable({ dynamic: false });
*/
/**
* jQuery "webks Responsive Table" plugin transforms less mobile compliant
* default HTML Tables into a flexible responsive format. Furthermore it
* provides some nice configuration options to optimize it for your special
* needs.
*
* Technically the selected tables are being transformed into a list of
* (definition) lists. The table header columns are used as title for each
* value.
*
* Functionality: - Select tables easily by jQuery selector. - Provide custom
* rules (by callback function) for transformation into tables mobile version. -
* Hard or dynamic switching for selected tables. - Use custom header and
* content rows selectors. - Provides an optional, customizable link to
* default table layout. - (Optionally) preserves most of the table elements
* class attributes. - Decide if the original table is kept in DOM and set
* invisible or completely removed. - Update display type (Table / Responsive &
* re-calculate dynamic switch) by easily calling
* .responsiveTableUpdate()-function.
*
* Functionality may be applied to all DOM table elements. See examples above.
* Please ensure that the settings match your requirements and your table
* structure is compliant.
*/
$.fn.responsiveTable = function(options) {
return $(this).each(function(){
$(this).responsiveTableInit(options);
});
};
/**
* Initializes the responsive tables. Expects to be executed on DOM table
* elements only. These are being transformed into responsive tables like
* configured.
*
* @param options
* Optional JSON list of settings.
*/
$.fn.responsiveTableInit = function(options) {
var settings = $.extend({
/**
* Keep table components classes as far as possible for the responsive
* output.
*/
preserveClasses : true,
/**
* true: Toggle table style if settings.dynamicSwitch() returns true.
* false: Only convert to mobile (one way)
*/
dynamic : true,
/**
* (Only used if dynamic!) If this function returns true, the responsive
* version is shown, else displays the default table. Might be used to set
* a switch based on orientation, screen size, ... for dynamic switching!
*
* @return boolean
*/
displayResponsiveCallback : function() {
return $(document).width() < 960;
},
/**
* (Only used if dynamic!) Display a link to switch back from responsive
* version to original table version.
*/
showSwitch : true,
/**
* (Only used if showSwitch: true!) The title of the switch link.
*/
switchTitle : 'Switch to default table view.',
// Selectors
/**
* The header columns selector.
* Default: 'thead td, thead th';
* other examples: 'tr th', ...
*/
headerSelector : 'thead td, thead th',
/**
* The body rows selector.
* Default: 'tbody tr';
* Other examples: 'tr', ...
*/
bodyRowSelector : 'tbody tr',
// Elements
/**
* The responsive rows container
* element.
* Default: '<dl></dl>';
* Other examples: '<ul></ul>'.
*/
responsiveRowElement : '<dl></dl>',
/**
* The responsive column title
* container element.
* Default: '<dt></dt>';
* Other examples: '<li></li>'.
*/
responsiveColumnTitleElement : '<dt></dt>',
/**
* The responsive column value container element.
* Default: '<dd></dd>';
* Other examples: '<li></li>'.
*/
responsiveColumnValueElement : '<dd></dd>'
}, options);
return this.each(function() {
// $this = The table (each).
var $this = $(this);
// Ensure that the element this is being executed on a table!
$this._responsiveTableCheckElement(false);
if ($this.data('webks-responsive-table-processed')) {
// Only update if already processed.
$this.responsiveTableUpdate();
return true;
}
// General
var result = $('<div></div>');
result.addClass('webks-responsive-table');
if (settings.preserveClasses) {
result.addClass($this.attr('class'));
}
// Head
// Iterate head - extract titles
var titles = new Array();
$this.find(settings.headerSelector).each(function(i, e) {
var title = $(this).html();
titles[i] = title;
});
// Body
// Iterate body
$this.find(settings.bodyRowSelector).each(function(i, e) {
// Row
var row = $(settings.responsiveRowElement);
row.addClass('row row-' + i);
if (settings.preserveClasses) {
row.addClass($(this).attr('class'));
}
// Column
$(this).children('td').each(function(ii, ee) {
var dt = $(settings.responsiveColumnTitleElement);
if (settings.preserveClasses) {
dt.addClass($(this).attr('class'));
}
dt.addClass('title col-' + ii);
dt.html(titles[ii]);
var dd = $(settings.responsiveColumnValueElement);
if (settings.preserveClasses) {
dd.addClass($(this).attr('class'));
}
dd.addClass('value col-' + ii);
dd.html($(this).html());
// Set empty class if value is empty.
if ($.trim($(this).html()) == '') {
dd.addClass('empty');
dt.addClass('empty');
}
row.append(dt).append(dd);
});
result.append(row);
});
// Display responsive version after table.
$this.after(result);
// Further + what shell we do with the processed table now?
if (settings.dynamic) {
if (settings.showSwitch) {
var switchBtn = $('<a>');
switchBtn.html(settings.switchTitle);
switchBtn.addClass('switchBtn btn');
switchBtn.attr('href', '#');
$('div.webks-responsive-table a.switchBtn').live('click',
function(e) {
$this.responsiveTableShowTable();
e.preventDefault();
return false;
});
result.prepend(switchBtn);
}
// Connect result to table
$this.data('webks-responsive-table', result);
$this.data('webks-responsive-table-processed', true);
// Connect table to result.
result.data('table', $this);
result.data('settings', settings);
$this.data('webks-responsive-table-processed', true);
// Hide table. We might need it again!
$this.hide();
// Run check to display right display version (table or responsive)
$this.responsiveTableUpdate();
} else {
// Remove table entirely.
$this.remove();
}
});
};
/**
* Re-Check the .displayResponsiveCallback() and display table according to
* its result. Only available if settings.dynamic is true.
*
* May be called on Window resize, Orientation Change, ... Must be executed on
* already processed DOM table elements.
*/
$.fn.responsiveTableUpdate = function() {
return this.each(function() {
// $this = The table (each).
var $this = $(this);
// Ensure that the element this is being executed on must be a table!
$this._responsiveTableCheckElement(true);
var responsiveTable = $this.data('webks-responsive-table');
if (responsiveTable != undefined) {
var settings = responsiveTable.data('settings');
if (settings != undefined) {
// Check preconditions!
if (settings.dynamic) {
// Is dynamic!
if (!settings.displayResponsiveCallback()) {
// NOT matching defined responsive conditions!
// Show original table and skip!
$this.responsiveTableShowTable();
} else {
$this.responsiveTableShowResponsive();
}
}
}
}
});
};
/**
* Displays the default table style and hides the responsive layout.
*
* Only available if settings.dynamic is true. Does nothing if the current
* display is already as wished.
*/
$.fn.responsiveTableShowTable = function() {
return this.each(function() {
// $this = The table (each).
var $this = $(this);
// Ensure that the element this is being executed on must be a table!
$this._responsiveTableCheckElement(true);
var responsiveTable = $this.data('webks-responsive-table');
if (responsiveTable.length > 0) {
$this.show();
responsiveTable.hide();
}
});
};
/**
* Displays the responsive style and hides the default table layout.
*
* Only available if settings.dynamic is true. Does nothing if the current
* display is already as wished.
*/
$.fn.responsiveTableShowResponsive = function() {
return this.each(function() {
// $this = The table (each).
var $this = $(this);
// Ensure that the element this is being executed on must be a table!
$this._responsiveTableCheckElement();
var responsiveTable = $this.data('webks-responsive-table');
if (responsiveTable.length > 0) {
$this.hide();
responsiveTable.show();
}
});
};
/**
* Checks the general preconditions for elements that this Plugin is being
* executed on.
*
* @throws Exception
* if the given DOM element is not a table.
* @throws Exception
* if a helper method is directly called on a not yet initialized
* table.
*/
$.fn._responsiveTableCheckElement = function(checkProcessed) {
if (checkProcessed === undefined) {
checkProcessed = true;
}
var $this = $(this);
if (!$this.is('table')) {
throw 'The selected DOM element may only be a table!';
}
if (checkProcessed
&& ($this.data('webks-responsive-table-processed') === undefined || !$this
.data('webks-responsive-table-processed'))) {
throw 'The selected DOM element has to be initialized by webks-responsive-table first.';
}
return $this;
};
})(jQuery);