-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmartable.js
287 lines (242 loc) · 10.5 KB
/
smartable.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*!
* Add header in table to filter and sort rows
* without modify the source <table>
*
* https://github.com/general03/smartable
*
* @author RIGAUDIE David
* @version 0.0.1
*/
(function() {
this.smartab = function() {
// Element table
this.table = null;
// Define option defaults
var defaults = {
tabId : 'table',
linkUpSort : '../img/up-sort.png',
linkDownSort : '../img/down-sort.png',
linkNoSort : '../img/sort.png',
widthFilter : 100,
widthCol : 170,
unsortCols : [1],
unfilterCols : [1]
}
// Create options by extending defaults with the passed in arguments
if (arguments[0] && typeof arguments[0] === "object")
{
// Merge options
this.options = extendDefaults(defaults, arguments[0]);
// Get the table with id selector
var elementNumberFounded = document.querySelectorAll(this.options.tabId);
if(elementNumberFounded.length > 1){
console.log(elementNumberFounded.length + " elements founded with selector : '" + this.options.tabId + "'");
console.log('Saved the first element ' + this.options.tabId + ' !');
}
this.table = document.querySelector(this.options.tabId);
if(this.table == null)
{
console.log("Table is not found with selector : "+this.options.tabId)
}
else
{
// Check right column number for unsortCols
var listUnsort = this.options.unsortCols.join().split(',');
var maxUnsort = Math.max.apply( Math, listUnsort);
if(maxUnsort > this.table.rows[0].cells.length-1)
console.log("'unsortCols' options has wrong value");
// Check right column number for unfilterCols
var listUnfilter = this.options.unfilterCols.join().split(',');
var maxUnfilter = Math.max.apply( Math, listUnfilter);
if(maxUnfilter > this.table.rows[0].cells.length-1)
console.log("'unfilterCols' options has wrong value");
}
}
}
/** Private methods **/
// Utility method to extend defaults with user options
function extendDefaults(source, properties) {
var property;
for (property in properties) {
if (properties.hasOwnProperty(property)) {
source[property] = properties[property];
}
}
return source;
}
// Need to do it in order to have right i
function callbackSortTable(instance, i ){
return function(){
instance.sortTable(i);
}
}
/** Public methods **/
// Sort
smartab.prototype.sortTable = function(column){
if(this.table == null)
{
console.log("Table is not found with selector : "+this.options.tabId)
}
else
{
var el = document.getElementById(this.options.tabId+"sort"+column);
// asc
var asc = 1;
switch(el.dataset.sort)
{
case 'desc' :
el.src= this.options.linkDownSort;
el.setAttribute('data-sort','asc');
break;
case 'asc' :
el.src=this.options.linkUpSort;
el.setAttribute('data-sort','desc');
// desc
var asc = -1;
break;
default:
el.src=this.options.linkDownSort;
el.setAttribute('data-sort','asc');
break;
}
// Add Sort on columns not in options
for(var i=0;i<this.table.rows[0].cells.length;i++){
if(i!=column && -1 == this.options.unsortCols.indexOf(i)){
document.getElementById(this.options.tabId+"sort"+i).src = this.options.linkNoSort;
document.getElementById(this.options.tabId+"sort"+i).setAttribute('data-sort', 'none');
}
}
var rows = this.table.rows, contentHtml = new Array(), arr = new Array(), arrayHidden = new Array(), i, j, cells, cellLength, rowLength = rows.length;
// fill the array with values from the table
for(i = 2; i < rowLength; i++){
cells = rows[i].cells;
cellLength = cells.length;
if(rows[i].style.display == 'none')
{
arrayHidden[i-2] = new Array();
}
else
{
arr[i-2] = new Array();
arr[i-2]['cols'] = new Array();
arr[i-2]['class'] = rows[i].className;
//arr[i-2]['classChildren'] = new Array();
}
for(j = 0; j < cellLength; j++)
{
if(rows[i].style.display == 'none')
{
arrayHidden[i-2][j] = cells[j].innerHTML;
}
else
{
arr[i-2]['cols'][j] = cells[j].innerHTML;
//arr[i-2]['classChildren'][j] = cells[j].className;
}
}
}
// sort the array by the specified column number (column) and order (asc)
arr.sort(function(a, b){
return (a['cols'][column] == b['cols'][column]) ? 0 : ((a['cols'][column] > b['cols'][column]) ? asc : -1*asc);
});
for(i = 0; i < arr.length; i++){
if(arr[i]['cols'] != undefined && arr[i]['cols'].length > 0)
contentHtml[i] = "<td>"+arr[i]['cols'].join("</td><td>")+"</td>";
}
// Add the <tr> hidden
if(arrayHidden.filter(Boolean).length > 0)
{
for(i = 0; i < arrayHidden.length; i++){
if(arrayHidden[i] != undefined && arrayHidden[i].length > 0)
arrayHidden[i] = "<td>"+arrayHidden[i].join("</td><td>")+"</td>";
}
}
// Delete all rows without header
while(this.table.rows[2] != undefined)
{
this.table.rows[2].remove();
}
// Create new content table sorted
var content = document.createElement('tbody');
var newContent = "";
// .filter(Boolean) => remove empty
contentHtml.filter(Boolean).forEach(function(element, index, array){
newContent += '<tr class="'+arr[index]['class']+'">'+element+'</tr>';
});
content.innerHTML = newContent;
content.innerHTML += '<tr style="display:none">'+arrayHidden.filter(Boolean).join('</tr><tr style="display:none">')+'</tr>';
// Add tbody to table
this.table.appendChild(content);
}
}
// Filter
smartab.prototype.filterTable = function(){
if(this.table == null)
{
console.log("Table is not found with selector : "+this.options.tabId)
}
else
{
var indexRowHeader = 1;
if(this.table.rows[1].getElementsByTagName('th').length != 0)
indexRowHeader = 2;
for(var i=indexRowHeader;i<this.table.rows.length;i++){
this.table.rows[i].style.display = 'table-row';
// Prevent the clic sort before apply filter text
if(this.table.rows[i].cells.length == 0 )
{
continue;
}
for(var j=0;j<this.table.rows[0].cells.length;j++){
// If filter is not permit
if(-1 != this.options.unfilterCols.indexOf(j))
continue;
// Check the value to filter
if(this.table.rows[i].cells[j].innerHTML.indexOf(this.table.rows[0].cells[j].childNodes[0].value) == -1){
// The text does not match the cell content
this.table.rows[i].style.display = 'none';
break;
}
}
}
}
}
// Add <input> to Sort and Filter
smartab.prototype.addHeader = function() {
if(this.table == null)
{
console.log("Table is not found with selector : "+this.options.tabId)
}
else
{
var _this = this;
var columns = this.table.rows[0].cells.length;
var newRow = this.table.insertRow(0);
for(var i=0; i<columns; i++){
var newcell = newRow.insertCell(i);
newcell.style.setProperty('width', this.options.widthCol+"px");
// Add Filter depending on options
if(-1 == this.options.unfilterCols.indexOf(i))
{
this.filterTableElement = document.createElement("input");
this.filterTableElement.type = "text";
this.filterTableElement.tabIndex = (i+1);
this.filterTableElement.onchange = function(){ _this.filterTable()};
this.filterTableElement.style.setProperty('display', 'inline');
this.filterTableElement.style.setProperty('width', this.options.widthFilter+"px");
newcell.appendChild(this.filterTableElement);
}
// Add Sort depending on options
if(-1 == this.options.unsortCols.indexOf(i))
{
var sortTableElement = document.createElement("img");
sortTableElement.id = this.options.tabId+'sort'+i;
sortTableElement.setAttribute('data-sort', 'none');
sortTableElement.src = this.options.linkNoSort;
sortTableElement.onclick = callbackSortTable(this, i);
newcell.appendChild(sortTableElement);
}
}
}
}
}());