1 var webdeveloper_currentTableSortColumn
= 0;
2 var webdeveloper_previousTableSortColumn
= 0;
3 var webdeveloper_sortAscending
= false;
5 // Makes all tables on the page with a sortable class sortable
6 function webdeveloper_makeTablesSortable()
8 var tableHeaderLength
= null;
9 var tableHeaderList
= null;
10 var tableList
= webdeveloper_evaluateXPath(document
, "//table[@class='sortable']");
11 var tableLength
= tableList
.length
;
13 // Loop through the tables
14 for(var i
= 0; i
< tableLength
; i
++)
16 tableHeaderList
= tableList
[i
].getElementsByTagName("th");
17 tableHeaderLength
= tableHeaderList
.length
;
19 // Loop through the table headers
20 for(var j
= 0; j
< tableHeaderLength
; j
++)
22 tableHeaderList
[j
].addEventListener("click", webdeveloper_sortTable
, false);
28 function webdeveloper_sortTable(event
)
30 // If the event is set
33 var newTableRowList
= new Array();
34 var tableHeader
= event
.target
;
35 var node
= tableHeader
;
36 var styleSheet
= document
.getElementById("webdeveloper-generated-content-stylesheet");
37 var tableHeaderRow
= tableHeader
.parentNode
;
38 var table
= tableHeaderRow
.parentNode
;
40 var tableRowList
= table
.childNodes
;
41 var tableRowLength
= tableRowList
.length
;
43 webdeveloper_currentTableSortColumn
= 0;
45 // Loop through the previous siblings
46 while((node
= node
.previousSibling
) != null)
48 webdeveloper_currentTableSortColumn
++;
51 // If the current sort column is the same as the previous sort column
52 if(webdeveloper_currentTableSortColumn
== webdeveloper_previousTableSortColumn
)
54 webdeveloper_sortAscending
= !webdeveloper_sortAscending
;
58 var previousTableHeader
= tableHeaderRow
.cells
[webdeveloper_previousTableSortColumn
];
60 webdeveloper_previousTableSortColumn
= webdeveloper_currentTableSortColumn
;
61 webdeveloper_sortAscending
= true;
63 // If the previous table header has a class attribute
64 if(previousTableHeader
.hasAttribute("class"))
66 previousTableHeader
.removeAttribute("class");
70 // If sorting in ascending order
71 if(webdeveloper_sortAscending
)
73 tableHeader
.setAttribute("class", "sort-ascending");
77 tableHeader
.setAttribute("class", "sort-descending");
80 // Loop through the table rows excluding the header row
81 for(var i
= 1; i
< tableRowLength
; i
++)
83 newTableRowList
[i
- 1] = tableRowList
[i
];
86 newTableRowList
.sort(webdeveloper_sortTableRows
);
87 webdeveloper_removeAllChildElements(table
);
88 table
.appendChild(tableHeaderRow
);
90 // Loop through the sorted table rows
91 for(i
= 0; i
< newTableRowList
.length
; i
++)
93 tableRow
= newTableRowList
[i
];
95 // If this is an even row
98 // If this row has a class attribute
99 if(tableRow
.hasAttribute("class"))
101 tableRow
.removeAttribute("class");
106 tableRow
.setAttribute("class", "shaded");
109 table
.appendChild(tableRow
);
112 // Disable and enable the style sheet to force the styles to reapply
115 styleSheet
.disabled
= true;
116 styleSheet
.disabled
= false;
121 // Sorts two table rows
122 function webdeveloper_sortTableRows(rowOne
, rowTwo
)
126 // If row one and row two are set
129 var columnOne
= webdeveloper_getElementText(rowOne
.cells
[webdeveloper_currentTableSortColumn
]);
130 var columnTwo
= webdeveloper_getElementText(rowTwo
.cells
[webdeveloper_currentTableSortColumn
]);
132 // If the columns are equal
133 if(columnOne
== columnTwo
)
137 else if(columnOne
< columnTwo
)
143 // If sorting in descending order
144 if(!webdeveloper_sortAscending
)
152 webdeveloper_makeTablesSortable();