forked from johnpolacek/stacktable.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacktable.js
More file actions
218 lines (188 loc) · 8.16 KB
/
stacktable.js
File metadata and controls
218 lines (188 loc) · 8.16 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
/**
* stacktable.js
* Author & copyright (c) 2012: John Polacek
* CardTable by: Justin McNally (2015)
* MIT license
*
* Page: http://johnpolacek.github.com/stacktable.js
* Repo: https://github.com/johnpolacek/stacktable.js/
*
* jQuery plugin for stacking tables on small screens
* Requires jQuery version 1.7 or above
*
*/
;(function($) {
$.fn.cardtable = function(options) {
var $tables = this,
defaults = {headIndex:0},
settings = $.extend({}, defaults, options),
headIndex;
// checking the "headIndex" option presence... or defaults it to 0
if(options && options.headIndex)
headIndex = options.headIndex;
else
headIndex = 0;
return $tables.each(function() {
var $table = $(this);
if ($table.hasClass('stacktable')) {
return;
}
var table_css = $(this).prop('class');
var $stacktable = $('<div></div>');
if (typeof settings.myClass !== 'undefined') $stacktable.addClass(settings.myClass);
var markup = '';
var $caption, $topRow, headMarkup, bodyMarkup, tr_class;
$table.addClass('stacktable large-only');
$caption = $table.find(">caption").clone();
$topRow = $table.find('>thead>tr,>tbody>tr,>tfoot>tr,>tr').eq(0);
// avoid duplication when paginating
$table.siblings().filter('.small-only').remove();
// using rowIndex and cellIndex in order to reduce ambiguity
$table.find('>tbody>tr').each(function() {
// declaring headMarkup and bodyMarkup, to be used for separately head and body of single records
headMarkup = '';
bodyMarkup = '';
tr_class = $(this).prop('class');
var $new_table = $('<table class="stacktable small-only"><tbody></tbody></table>').addClass(table_css);
// for the first row, "cellIndex" cell is the head of the table
// for the other rows, put the "cellIndex" cell as the head for that row
// then iterate through the key/values
$(this).find('>td,>th').each(function(cellIndex, cellElement) {
if ($(cellElement).html() !== '') {
$new_table.find('tbody').append(
$('<tr/>').addClass(tr_class).append(
$('<td class="st-key"/>').append(
$topRow.find('>td,>th').eq(cellIndex).contents().clone()
),
$('<td class="st-val"/>').addClass($(cellElement).prop('class')).append($(cellElement).contents().clone())
)
)
}
});
$stacktable.append($new_table);
});
$table.find('>tfoot>tr>td').each(function(cellIndex,cellElement) {
if ($.trim($(cellElement).text()) !== '') {
$stacktable.append(
$('<table class="stacktable small-only"><tbody><tr><td></td></tr></tbody></table>')
.addClass(table_css).find('td').append( $(cellElement).contents().clone() )).end();
}
});
$stacktable.prepend($caption);
$table.before($stacktable);
});
};
$.fn.stacktable = function(options) {
var $tables = this,
defaults = {headIndex:0,displayHeader:true},
settings = $.extend({}, defaults, options),
headIndex;
// checking the "headIndex" option presence... or defaults it to 0
if(options && options.headIndex)
headIndex = options.headIndex;
else
headIndex = 0;
return $tables.each(function() {
var table_css = $(this).prop('class');
var $stacktable = $('<table class="stacktable small-only"><tbody></tbody></table>').addClass(table_css);
if (typeof settings.myClass !== 'undefined') $stacktable.addClass(settings.myClass);
var markup = '';
var $table, $caption, $topRow, headMarkup, bodyMarkup, tr_class, displayHeader;
$table = $(this);
$table.addClass('stacktable large-only');
$caption = $table.find(">caption").clone();
$topRow = $table.find('>thead>tr,>tbody>tr,>tfoot>tr').eq(0);
displayHeader = $table.data('display-header') === undefined ? settings.displayHeader : $table.data('display-header');
// using rowIndex and cellIndex in order to reduce ambiguity
$table.find('>tbody>tr, >thead>tr').each(function(rowIndex, rowElement) {
// declaring headMarkup and bodyMarkup, to be used for separately head and body of single records
headMarkup = '';
bodyMarkup = '';
tr_class = $(this).prop('class');
// for the first row, "headIndex" cell is the head of the table
if (rowIndex === 0) {
// the main heading goes into the markup variable
if (displayHeader) {
$stacktable.find('tbody').append(
$('<tr/>').append(
$('<th class="st-head-row st-head-row-main" colspan="2"></th>')
.append($(rowElement).find('>th,>td').eq(headIndex).contents().clone())
)
);
}
} else {
// for the other rows, put the "headIndex" cell as the head for that row
// then iterate through the key/values
$(rowElement).find('>td,>th').each(function(cellIndex, cellElement) {
if (cellIndex === headIndex) {
$stacktable.find('tbody').append(
$('<tr><th class="st-head-row" colspan="2"></th></tr>').addClass(tr_class)
.find('th').append($(cellElement).contents().clone()).end()
);
} else {
if ($(cellElement).html() !== '') {
$stacktable.find('tbody').append(
$('<tr></tr>').addClass(tr_class)
.append( $('<td class="st-key"></td>').append($topRow.find('>td,>th').eq(cellIndex).contents().clone()) )
.append( $('<td class="st-val"></td>').addClass( $(cellElement).prop('class') ).append($(cellElement).contents().clone()) )
);
}
}
});
}
});
$stacktable.prepend($caption);
$table.before($stacktable);
});
};
$.fn.stackcolumns = function(options) {
var $tables = this,
defaults = {},
settings = $.extend({}, defaults, options);
return $tables.each(function() {
var $table = $(this);
var $caption = $table.find(">caption").clone();
var num_cols = $table.find('>thead>tr,>tbody>tr,>tfoot>tr').eq(0).find('>td,>th').length; //first table <tr> must not contain colspans, or add sum(colspan-1) here.
if(num_cols<3) //stackcolumns has no effect on tables with less than 3 columns
return;
var $stackcolumns = $('<table class="stacktable small-only"></table>');
if (typeof settings.myClass !== 'undefined') $stackcolumns.addClass(settings.myClass);
$table.addClass('stacktable large-only');
var tb = $('<tbody></tbody>');
var col_i = 1; //col index starts at 0 -> start copy at second column.
while (col_i < num_cols) {
$table.find('>thead>tr,>tbody>tr,>tfoot>tr').each(function(index) {
var tem = $('<tr></tr>'); // todo opt. copy styles of $this; todo check if parent is thead or tfoot to handle accordingly
if(index === 0) tem.addClass("st-head-row st-head-row-main");
var first = $(this).find('>td,>th').eq(0).clone().addClass("st-key");
var target = col_i;
// if colspan apply, recompute target for second cell.
if ($(this).find("*[colspan]").length) {
var i =0;
$(this).find('>td,>th').each(function() {
var cs = $(this).attr("colspan");
if (cs) {
cs = parseInt(cs, 10);
target -= cs-1;
if ((i+cs) > (col_i)) //out of current bounds
target += i + cs - col_i -1;
i += cs;
} else {
i++;
}
if (i > col_i)
return false; //target is set; break.
});
}
var second = $(this).find('>td,>th').eq(target).clone().addClass("st-val").removeAttr("colspan");
tem.append(first, second);
tb.append(tem);
});
++col_i;
}
$stackcolumns.append($(tb));
$stackcolumns.prepend($caption);
$table.before($stackcolumns);
});
};
}(jQuery));