-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable-column-switcher.js
128 lines (91 loc) · 3.16 KB
/
table-column-switcher.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
var ColumnSwitcher = function(table)
{
// private vars -------------------------------------------------------------//
var _table = table,
_columns = _table.getAttribute('data-column-switch-cols').split(' '),
_currentColumn = _table.getAttribute('data-column-switch-default-col'),
_thead = _table.querySelector('thead'),
_tbody = _table.querySelector('tbody'),
_selectCell,
_select;
// private methods ----------------------------------------------------------//
var _update = function()
{
// hide all cells
var a = _table.querySelectorAll('td'),
i = a.length;
while(i--)
{
a[i].classList.add('column-switcher__hidden');
}
// show cells at current column index
a = _table.querySelectorAll('td:nth-child(' + _currentColumn + ')');
i = a.length;
while(i--)
{
a[i].classList.remove('column-switcher__hidden');
}
};
var _handleSelectChange = function()
{
_currentColumn = _select.value;
_update();
}
var _init = function()
{
// add styling hooks to the table so we can specifically style js-modified table-switchers
_table.classList.add('column-switcher');
// add an extra cell to the header row and stick a select element in it
_selectCell = _thead.querySelector('tr').appendChild(document.createElement('th'));
_selectCell.classList.add('column-switcher__select-cell')
_select = _selectCell.appendChild(document.createElement('select'));
// hide all the cells in the header row that we don’t need
_thead.querySelector('th')
for(var i=0; i<_columns.length; i++)
{
_thead.querySelector('th:nth-child(' + _columns[i] + ')').classList.add('column-switcher__hidden');
}
// build list of options for select
var option,
optionText,
optionValue;
headerCells = _thead.querySelectorAll('th');
for(var j=0; j<_columns.length; j++)
{
option = _select.appendChild(document.createElement('option'));
optionText = headerCells[_columns[j] - 1].textContent;
optionValue = _columns[j];
option.innerHTML = optionText;
option.value = optionValue;
if(_columns[j] == _currentColumn)
{
_select.selectedIndex = j;
}
}
// handle changes to the select value
_select.addEventListener('change', _handleSelectChange);
// run update once on init to set an initial state
_update();
}
// getter/setters ----------------------------------------------------------//
this.currentColumn = function(n)
{
if(n != parseFloat(n)) { throw new TypeError(); }
_currentColumn = parseInt(n);
console.log('_currentColumn = ' + _currentColumn);
_update();
}
// initialise this instance -------------------------------------------------//
_init();
}
// STATIC METHODS ------------------------------------------------------------//
ColumnSwitcher.init = function()
{
var tables = document.querySelectorAll('table[data-column-switch]');
var n = tables.length;
for(var i=0; i<n; i++)
{
var columnSwitcher = new ColumnSwitcher(tables[i]);
}
}
ColumnSwitcher.init();