-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtablesort_js.js
More file actions
489 lines (445 loc) · 12.2 KB
/
Copy pathtablesort_js.js
File metadata and controls
489 lines (445 loc) · 12.2 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//using an HTML table as a limited database table, will sort rows by the column specified
//Judas Gutenberg Dec 04 2007
//NOTE: altered Aug 2019 to store last sort and allows it to be re-run in cases where a refresh-like AJAX operation has happened
//to use: first set the id of your table to sort to "idsorttable"
//second call NumberRows(intTDMinCount) (where intTDMinCount is the minimum number of columns for a table row to be in play)
//that will set consecutive ids for all the TR nodes in the table whose ID is idsorttable
//then create hyperlinks on the headings of the HTML table columns
//something like <a href=\"javascript: SortTable(3, 2)\">foreign PK</a>"
//which is labeled "foreign PK" and will, if clicked, sort column 3 (these columns are zero-based) starting with row #2
//This code is covered under the GNU General Public License
//info here: http://www.gnu.org/copyleft/gpl.html
//the digest is as follows: you cannot modify this code without
//publishing your source code under the same license
//contact the developer at gus@asecular.com http://asecular.com
var arrDirection = new Array(); //stores global sort directions for the columns so they can be reversed with next click
var bwlsortable = false;
var lastSort = new Array(); //so we can redo old sorts
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
return this.replace(/\s+$/,"");
}
function GetParent(thisnode)
{
var thisparent;
if(thisnode)
{
if (thisnode.parentNode)
{
thisparent=thisnode.parentNode
}
else
{
thisparent=thisnode.parentElement
}
return (thisparent);
}
}
function NumberRows(idsorttable, intTDMinCount)
{
//console.log("YEEE");
var strDirection = '';
var idprefix=idsorttable + 'idrow';
var thistable=document.getElementById(idsorttable);
var arrTRs=document.getElementsByTagName('tr');
var intTRCount=0;
var intsortable=0;
//alert( arrTRs.length);
for(i=0; i<arrTRs.length; i++)
{
thistr=arrTRs[i];
var thesechildren=thistr.childNodes;
//console.log(thesechildren);
var tdcount=0;
strDirection="";
var bwlSubsidiary=false;
var bwlSortAvoid=false;
//by naming a tr "sortavoid" it always avoids being sorted
//also, any tr with fewer td cells than intTDMinCount will not be sorted
var trname=thistr.getAttribute("name");
var trclass=thistr.getAttribute("class");
bwlHeaderFail=false;
if(trname)
{
if(trname.indexOf("sortavoid")>-1 || trclass.indexOf("sortavoid")>-1)
{
//alert("#");
bwlHeaderFail=true;
bwlSortAvoid=true;
}
}
for(j=0; j<thesechildren.length; j++)
{
//console.log(j);
var thischild=thesechildren[j];
if (thischild.nodeName.toLowerCase()=="td")
{
if(thischild.colSpan)
{
//this might not be a perfect test for all tables
//but generally a row with a colspan in a sortable nested table
//belongs to its immediate parent and should ride along through the sort
//this behavior can be overridden by naming the row with a string including "sortavoid"
if(thischild.colSpan>1)
{
//alert("$$" + thischild.id + " " + thischild.colSpan);
bwlSubsidiary=true;
}
}
tdcount++;
strDirection+="f ";
}
if (thischild.innerHTML)
{
if (thischild.innerHTML.indexOf("SortTable(")>-1)
{
bwlHeaderFail=true;
}
}
}
var NextTableUp=ClimbTreeToTagFromObj(thistr, "table");
//console.log(NextTableUp, thistable);
if (NextTableUp.id==thistable.id )
{
if(bwlSubsidiary && !bwlSortAvoid )
{
thistr.id=idprefix + (intTRCount-1) + "_sub";
}
else if(tdcount>intTDMinCount && !bwlHeaderFail )
{
intsortable++;
thistr.id=idprefix + intTRCount;
}
else
{
thistr.id=idprefix + "x" + intTRCount;
}
intTRCount++;
}
else
{
//idNextTableUp=NextTableUp.id;
//if(idNextTableUp.indexOf("sorttable")>0)
}
}
arrDirection=strDirection.split(" ");
//alert(intsortable);
if(intsortable>1)
{
bwlsortable=true;
}
}
function redoLastSort(tableId) {
if(!tableId) {
tableId = lastSort[0];
}
//console.log(tableId, lastSort[1], lastSort[2]);
SortTable(tableId, lastSort[1], lastSort[2], lastSort[3]);
}
function SortTable(idsorttable, intColumn, intStart, direction)
{
if(bwlsortable)//i've turned off this functionality if the number of sortable rows is less than 2
{
var out="";
var i;
var j;
if(!direction) {
direction = arrDirection[intColumn];
}
lastSort = [idsorttable, intColumn, intStart, direction]; //save in a global in case we want to redo;
var arrContainer=CreateColumnArray(idsorttable, intColumn, 0, direction);
var idprefix=idsorttable + "idrow";
var sortcontainer=document.createElement('tbody'); //for IE this has to be tbody.
var strClass1="", strClass2="" ;
//var idsorttable="idsorttable";
var thistable=document.getElementById(idsorttable);
var thistbody=document.getElementById(idsorttable + "body");
if(intStart==undefined || intStart=="")
{
intStart=1;
}
if(arrDirection[intColumn]=="b")
{
if(direction = "b") {
arrDirection[intColumn]="f";
} else {
arrDirection[intColumn]="b";
}
}
else
{
if(direction = "b") {
arrDirection[intColumn]="b";
} else {
arrDirection[intColumn]="f";
}
}
//beginning rows that are not sorted
for(i=0; i<4; i++)
{
var thisid=idprefix + "x" + i;
var thistr=document.getElementById(thisid);
if(thistr)
{
sortcontainer.appendChild(thistr);
}
}
intMeatcount=0;
for(i=0; i<arrContainer.length; i++)
{
if(arrContainer[i])
{
var arrTemp=arrContainer[i].split("^");
var thisid=idprefix + arrTemp[1];
//alert("meat: " +thisid + " : " + arrTemp[0]);
var thistr=document.getElementById(thisid);
var thisClass=thistr.className;
if(strClass1=="" && thisClass!="" )
{
strClass1=thisClass;
}
else if(strClass2=="" && thisClass!=strClass1 && thisClass!="")
{
strClass2=thisClass;
}
if(thistr)
{
sortcontainer.appendChild(thistr);
//implementing the bringing along of subsidiary trs
//such trs are found by the NumberRows function
//and flagged with a _sub suffix - they stand out for their colspans
var additionaltrid=idprefix + arrTemp[1] +"_sub";
var additionaltr=document.getElementById(additionaltrid);
if(additionaltr)
{
//alert("DD");
sortcontainer.appendChild(additionaltr)
}
//end implement of subsid
out+=thisid;
}
intMeatcount++;
}
}
//any final rows that need to be part of the table
//but are not sorted
for(i=arrContainer.length; i<arrContainer.length+4; i++)
{
var thisid=idprefix + "x" + i;
var thistr=document.getElementById(thisid);
if(thistr)
{
sortcontainer.appendChild(thistr);
}
}
//alert(strClass2 + " " + strClass1);
//recolor stripes!
//this might be a little over-the-top but i felt like having some perfection in my miserable life
var thesechildren=sortcontainer.childNodes;
if(intMeatcount>1 )
{
var linecount=0;
for(i=0; i<thesechildren.length; i++)
{
if(thesechildren[i].id.indexOf(idprefix + "x")<0 && thesechildren[i].id.indexOf("_sub")<0)
{
linecount++;
if(linecount/2==parseInt(linecount/2))
{
thesechildren[i].className=strClass2;
}
else
{
thesechildren[i].className=strClass1;
}
}
else
{
//alert(thesechildren[i].id +"\n" + thesechildren[i].id.indexOf("_sub") + "\n" + thesechildren[i].id.indexOf("b"));
}
}
}
//replace the unsorted table rows with the sorted rows
MurderAllTheChildren(thistable);
if(thistable)
{
thistable.appendChild(sortcontainer);
}
}
}
function MurderAllTheChildren(obj)
{
if(obj)
{
var thesechildren=obj.childNodes;
for(i=0; i<thesechildren.length; i++)
{
var thischild=thesechildren[i];
obj.removeChild(thischild) ;
}
}
}
function CreateColumnArray(idsorttable, intColumn, intStart, strDirection)
{
//creates a sorted array of data in the specified column of a table,
//starting with TR intStart
//does a reverse sort if strDirection=="b"
//Judas Gutenberg Dec 04 2007
var i, j, k;
var idprefix=idsorttable + "idrow";
//var idsorttable="idsorttable";
var arrContainers=new Array();
var thistable=document.getElementById(idsorttable);
var arrTRs=document.getElementsByTagName('tr');
var thiscontent="";
var out="";
//var testron="";
//start at one to skip header
for(i=intStart; i<(arrTRs.length); i++)
{
var tdcount=0;
var thistr=document.getElementById(idprefix + i);
if(thistr)
{
var thesechildren=thistr.childNodes;
//alert(thesechildren.length);
for(j=0; j<thesechildren.length; j++)
{
var thischild=thesechildren[j];
if (thischild.nodeName.toLowerCase()=="td")
{
if(tdcount==intColumn)
{
//alert(thischild.nodeName);
thiscontent=thischild.innerHTML.trim();
var tdchildren=thischild.childNodes;
for(k=0; k<tdchildren.length; k++)
{
var thistdchild=tdchildren[k];
if(thistdchild.nodeName)
{
//alert(thistdchild.nodeName);
if (thistdchild.nodeName.toLowerCase()=="a")
{
//this allows my code to sort by the anchor text inside
//an anchor link
//if that's what it finds in the table cell
thiscontent=thistdchild.innerHTML.trim();
//alert(thiscontent);
}
else if (thistdchild.nodeName.toLowerCase()=="input")
{
//this allows my code to sort by the value of a textinput
//if that's what it finds in the table cell
//thiscontent=thistdchild.getAttribute("value");
thiscontent=thistdchild.value; //for some reason the preceding method doesn't read dynamic data
//alert(thiscontent);
//testron +=thiscontent + "\n";
}
else if (thistdchild.nodeName.toLowerCase()=="select")
{
//this allows my code to sort by the value of a textinput
//if that's what it finds in the table cell
thiscontent=thistdchild[thistdchild.selectedIndex].value;
}
thiscontent=thiscontent.toLowerCase();
}
}
if(thiscontent=="")
{
thiscontent="0";
}
}
tdcount++;
}
}
//handle the sorting of numeric columns:
if(thiscontent.indexOf(".")>-1)//possibly a decimal number
{
var arrContent=thiscontent.split(".");
if (arrContent.length==2)
{
if(parseInt(arrContent[0])==arrContent[0] && parseInt(arrContent[1])==arrContent[1])
{
thiscontent= pad(arrContent[0], 9, "0", 1) + "." + pad(arrContent[0], 9, "0", 2);
}
}
}
else if(parseInt(thiscontent)==thiscontent)
{
thiscontent= pad(thiscontent, 9, "0", 1) ;
}
arrContainers[i]=thiscontent + "^" + i;
out+=thiscontent + "\n";
}
}
arrContainers.sort();
if(strDirection=="b")
{
arrContainers.reverse();
}
for(i=0; i<arrContainers.length; i++)
{
thiscontent=arrContainers[i];
out+=thiscontent + "\n";
}
//alert(out);
//alert(testron);
return arrContainers;
}
function pad(str, len, pad, dir)
{
//pads str to len with pad string. works on the left with dir=1, middle with 3, and right with 2
if (typeof(len) == "undefined")
{
var len = 0;
}
if (typeof(pad) == "undefined")
{
var pad = ' ';
}
if (typeof(dir) == "undefined")
{ var dir = STR_PAD_RIGHT;
}
if (len + 1 >= str.length)
{
switch (dir)
{
case 1:
str = Array(len + 1 - str.length).join(pad) + str;
break;
case 3:
var right = Math.ceil((padlen = len - str.length) / 2);
var left = padlen - right;
str = Array(left+1).join(pad) + str + Array(right+1).join(pad);
break;
default:
str = str + Array(len + 1 - str.length).join(pad);
break;
}
}
return str;
}
function ClimbTreeToTagFromObj(thisItem, tag)
//returns the node of the nearest specified HTML tag above our element described by thisId
//judas gutenberg june 12 2006
{
var i=0;
//we loop through a max of ten times and if there's no tr by then there's not gonna be one
for(i=0; i<10; i++)
{
thisItem= GetParent(thisItem);
if (thisItem)
{
if (thisItem.nodeName.toLowerCase()==tag)
{
return thisItem;
}
}
}
}