forked from johnpolacek/stacktable.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacktable.js
executable file
·58 lines (55 loc) · 2.02 KB
/
stacktable.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
/**
* stacktable.js
* Author & copyright (c) 2012: John Polacek
* Dual MIT & GPL license
*
* Page: http://johnpolacek.github.com/stacktable.js
* Repo: https://github.com/johnpolacek/stacktable.js/
*
* jQuery plugin for stacking tables on small screens
*
*/
;(function($) {
$.fn.stacktable = function(options) {
var $tables = this,
defaults = {id:'stacktable',hideOriginal:false},
settings = $.extend({}, defaults, options);
return $tables.each(function() {
var $stacktable = $('<table class="'+settings.id+'"><tbody></tbody></table>');
if (typeof settings.myClass !== undefined) $stacktable.addClass(settings.myClass);
var markup = '';
$table = $(this);
$topRow = $table.find('tr').eq(0);
$table.find('tr').each(function(index,value) {
markup += '<tr>';
// for the first row, top left table cell is the head of the table
if (index===0) {
markup += '<tr><th class="st-head-row st-head-row-main" colspan="2">'+$(this).find('th,td').eq(0).html()+'</th></tr>';
}
// for the other rows, put the left table cell as the head for that row
// then iterate through the key/values
else {
$(this).find('td').each(function(index,value) {
if (index===0) {
markup += '<tr><th class="st-head-row" colspan="2">'+$(this).html()+'</th></tr>';
} else {
if ($(this).html() !== ''){
markup += '<tr>';
if ($topRow.find('td,th').eq(index).html()){
markup += '<td class="st-key">'+$topRow.find('td,th').eq(index).html()+'</td>';
} else {
markup += '<td class="st-key"></td>';
}
markup += '<td class="st-val">'+$(this).html()+'</td>';
markup += '</tr>';
}
}
});
}
});
$stacktable.append($(markup));
$table.before($stacktable);
if (settings.hideOriginal) $table.hide();
});
};
}(jQuery));