/**
TableSorter class.
Original Author: Manos Batsis, mailto:xcircuit@yahoo.com
Hacked by Joe Fritz (mailto:jfritz@steptools.com) Dec 18, 2002
for the following changes:
Copy the entire DOM elements in the TD, and not just the text
Added numeric sort type, and modes date types to 3 and 4
moved initTable to this file, to enable simplified HTML code that uses
this JS.
Added SortableObject structure, to eliminate concatenating and
parsing the row from the sorted item.
Removed support for javascript arrays. It now only deals with DOM
tables.
main methods:
sortOn(column to base the sort, button/header matching the column)
setSortFuncs(array of inegers. used to set the sort function for each column: 0 for default, 1 for case-insensitive, 2 for numeric, 3 for date (US),
4 for date (european))
autoCalled: readTable(table object) reads a table
Simple interface:
Load the file:
Assign a TABLE element to a instance of TableSorter (named sorter) :
Use the table:
*/
function TableSorter(table, skipRowOne)
{
if(skipRowOne && skipRowOne == 1)
this.skipRowOne = 1;
else
this.skipRowOne = 0;
this.matrix = this.readTable(table);
if (!this.matrix)
return null;
this.lastColIndex = -1;
this.currentColIndex = -1;
this.arrSortFuncs = new Array();
this.arrowDirection = null;
if (table.nodeName != "TABLE") {
throw ("Element must be a table is "+table.nodeName);
}
this.element = table;
}
TableSorter.prototype.sortOn = function(iColIndex)
{
this.currentColIndex = iColIndex;
var temp = new Array(this.matrix.length);
for(var i=0;i strB) return 1;
else return 0;
}
TableSorter.prototype.noCaseFunc = function(a, b)
{
var strA = a.key.toLowerCase();
var strB = b.key.toLowerCase();
if(strA < strB) return -1;
else if(strA > strB) return 1;
else return 0;
}
TableSorter.prototype.numericFunc = function(a, b)
{
return a.key-b.key;
}
TableSorter.prototype.dateFunc = function(a, b)
{
var datA = new Date(a.key);
var datB = new Date(b.key);
if(datA < datB) return -1;
else if(datA > datB) return 1;
else return 0;
}
TableSorter.prototype.dateEUFunc = function(a, b)
{
var strA = a.key.split("/"),
strB = b.key.split("/"),
datA = new Date(strA[2], strA[1], strA[0]),
datB = new Date(strB[2], strB[1], strB[0]);
if(datA < datB) return -1;
else if(datA > datB) return 1;
else return 0;
}
TableSorter.prototype.readTable = function(elem)
{
if (!elem)
return null;
if(elem.nodeName != "tbody")
elem = elem.getElementsByTagName("tbody")[0];
if(!elem) {
throw ("No tbody element");
}
var iRows = elem.getElementsByTagName("tr");
var arrX = new Array();
for(var i=0; i+this.skipRowOne < iRows.length; i++)
{
arrX[i] = new Array();
var iCols = iRows[i+this.skipRowOne].getElementsByTagName("td");
for(var j=0; j < iCols.length; j++) {
arrX[i][j] = iCols[j].cloneNode(true);
}
}
return arrX;
}
TableSorter.prototype.updateTblCells = function(elem)
{
if(elem.nodeName != "tbody")
elem = elem.getElementsByTagName("tbody")[0];
if(!elem)
return
var iRows = elem.getElementsByTagName("tr");
for(var i=0; i+this.skipRowOne < iRows.length; i++)
{
var row = iRows[i+this.skipRowOne];
var iCols = row.getElementsByTagName("td");
for(var j=0; j < iCols.length; j++) {
replaceContent(iCols[j], this.matrix[i][j]);
}
}
}
//----------------------------------------------------------------
// The element of the array to be sorted
function SortableObject (key, idx) {
this.key = key;
this.idx = idx;
}
//----------------------------------------------------------------
// Utility functions
function textContent(node) {
var ret = "";
var kids = node.childNodes.length;
for (var i=0; i