This repository has been archived by the owner on May 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflexi_tables.js
65 lines (53 loc) · 2.11 KB
/
flexi_tables.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
(function ($) {
$.fn.flexiTables = function() {
categories = [];
//Cycle through tables marked with data-flexi-table
return this.filter('table[data-flexi-table]').each(function(i){
$this = $(this);
dataDeliniator = $this.data('flexi-deliniator')
dataSetTitleDeliniator = '<span class="flexiTableDeliniator">' + dataDeliniator + '</span>';
dataTitles = $this.data('flexi-table');
//If flexi-table has value then add to dataTitles[]
if (dataTitles.length > 0 ) { dataTitles = $this.data('flexi-table').split(','); }
//Collect labels from thead and add to categories[]
$this.find("th").each(function(i){
var label = $(this).text();
categories.push(label);
});
$this.find("tr").each(function(i){
thisTr = $(this);
//Add labels to each <td>
thisTr.find("td").each(function(i){
$(this).before('<th class="flexiTableLabel">' + categories[i] + '</th>');
});
//If dataTitles has value.
if (dataTitles.length > 0 ) {
var newHeaderText = "";
/* For each unique array elem add value associated with the key with defined
deliniators and prepend to header of each data-set. */
$.each(unique(dataTitles), function(key, value) {
//Check if deliniators are defined, if they are then add deliniators
if (typeof dataDeliniator === 'undefined' ) {
newHeaderText += thisTr.find('td').eq(value).html() + " ";
} else {
newHeaderText += thisTr.find('td').eq(value).html() + " " + dataSetTitleDeliniator + " ";
}
//Add a class to hide duplicate elem's
thisTr.find('td').eq(value).addClass('flexiTable_hideSmall');
thisTr.find('th').eq(value).addClass('flexiTable_hideSmall');
});
var newHeader = '<th class="flexiTableTitle">' + newHeaderText + '</th>';
thisTr.prepend(newHeader);
}
});
dataTitles.length = 0;
categories.length = 0;
});
//Make sure that there are no duplicate array elem's in dataTitles
function unique(dataTitles) {
return $.grep(dataTitles, function(el, index) {
return index == $.inArray(el, dataTitles);
});
}
};
} (jQuery));