-
Notifications
You must be signed in to change notification settings - Fork 44
/
jquery.restable.js
executable file
·138 lines (98 loc) · 4.04 KB
/
jquery.restable.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
(function ($, window, i) {
'use strict';
$.fn.ReStable = function (options) {
// Settings
var s = $.extend({
rowHeaders: true,
maxWidth: 480,
keepHtml: false
}, options);
// Build the responsive menu container and fill it with build_menu()
function create_responsive_table(element, i) {
var $cols = [],
$result = {},
$cols_header = $(element).find('tr').first().children('td,th'),
$row_number = 0,
$list;
if (s.rowHeaders) {
$cols_header = $cols_header.slice(1);
}
// Prende le intestazioni
$cols_header.each(function () {
$cols.push($(this).text());
});
// Costruisce un array con il contenuto: $result
$(element).find('tr').slice(1).each(function () {
var $row = $(this);
$row_number += 1;
$.each($cols, function (index, value) {
index += 1;
if (s.rowHeaders) {
if (!$result[value]) { $result[value] = {}; }
if(s.keepHtml) {
$result[value][$row.children('td:nth-child(1)').html()] = $row.children('td:nth-child(' + (index + 1) + ')').html();
} else {
$result[value][$row.children('td:nth-child(1)').text()] = $row.children('td:nth-child(' + (index + 1) + ')').text();
}
} else {
if (!$result[$row_number]) { $result[$row_number] = {}; }
if (s.keepHtml){
$result[$row_number][value] = $row.children('td:nth-child(' + index + ')').html();
} else {
$result[$row_number][value] = $row.children('td:nth-child(' + index + ')').text();
}
}
});
});
// Crea la lista
$list = $('<ul/>', {
class: 'tabletolist ' + ((s.rowHeaders) ? 'rh' : 'nrh'),
id: 'tabletolist' + i
}).insertBefore($(element));
$.each($result, function (index, value) {
var $myrow = $('<li/>', {
html: (s.rowHeaders) ? '<span class="titles">' + index + '</span>' : ''
}).appendTo($list),
$myrowul = $('<ul/>').appendTo($myrow);
$.each(value, function (index, value) {
$('<li/>', {
'class': 'cf',
html: '<span class="row_headers">' + index + '</span> <span class="row_data '+((s.keepHtml)?'html':'')+'">' + value + '</span>'
}).appendTo($myrowul);
});
});
return $list;
}
// Let's do it
this.each(function () {
var element = $(this),
responsive_table;
i += 1;
// The responsive menu is built if the page size is or goes under maxWidth
function handle_table() {
if ($(window).width() > parseInt(s.maxWidth, 10)) {
$(element).show();
if (responsive_table) {
$(responsive_table).hide();
}
} else {
$(element).hide();
if (responsive_table) {
$(responsive_table).show();
} else {
responsive_table = create_responsive_table(element, i);
}
}
}
// At first
handle_table();
// Then at the resizing of the page
$(window).resize(function () {
handle_table();
});
});
};
$.ReStable = function (options) {
$('table').ReStable(options);
};
})(jQuery, this, 0);