Sort tables using JavaScript

Source: Internet
Author: User

<Script language = "JavaScript">
Var dom = (document. getElementsByTagName )? True: false;
Var ie5 = (document. getElementsByTagName & document. all )? True: false;
Var arrowUp, arrowDown;
If (ie5 | dom)
InitSortTable ();
Function initSortTable (){
ArrowUp = document. createElement ("SPAN ");
Var tn = document. createTextNode ("success ");
ArrowUp. appendChild (tn );
ArrowUp. className = "arrow ";
ArrowDown = document. createElement ("SPAN ");
Var tn = document. createTextNode ("success ");
ArrowDown. appendChild (tn );
ArrowDown. className = "arrow ";
}
Function sortTable (tableNode, nCol, bDesc, sType ){
Var tBody = tableNode. tBodies [0];
Var trs = tBody. rows;
Var trl = trs. length;
Var a = new Array ();
For (var I = 0; I <trl; I ++ ){
A [I] = trs [I];
}
Var start = new Date;
Window. status = "Sorting data ...";
A. sort (compareByColumn (nCol, bDesc, sType ));
Window. status = "Sorting data done ";
For (var I = 0; I <trl; I ++ ){
TBody. appendChild (a [I]);
Window. status = "Updating row" + (I + 1) + "of" + trl +
"(Time spent:" + (new Date-start) + "ms )";
}
// Check for onsort
If (typeof tableNode. onsort = "string ")
TableNode. onsort = new Function ("", tableNode. onsort );
If (typeof tableNode. onsort = "function ")
TableNode. onsort ();
}
Function CaseInsensitiveString (s ){
Return String (s). toUpperCase ();
}
Function parseDate (s ){
Return Date. parse (s. replace (/-/g ,/));
}
Function toNumber (s ){
Return Number (s. replace (/[^ 0-9.]/g ,""));
}
Function Percent (s ){
Return Number (s. replace ("% ",""));
}
Function compareByColumn (nCol, bDescending, sType ){
Var c = nCol;
Var d = bDescending;
Var fTypeCast = String;
If (sType = "Number ")
FTypeCast = Number;
Else if (sType = "Date ")
FTypeCast = parseDate;
Else if (sType = "CaseInsensitiveString ")
FTypeCast = CaseInsensitiveString;
Else if (sType = "Percent ")
FTypeCast = Percent;
Return function (n1, n2 ){
If (fTypeCast (getInnerText (n1.cells [c]) <fTypeCast (getInnerText (n2.cells [c])
Return d? -1: + 1;
If (fTypeCast (getInnerText (n1.cells [c])> fTypeCast (getInnerText (n2.cells [c])
Return d? + 1:-1;
Return 0;
};
}
Function sortColumnWithHold (e ){
Var el = ie5? E. srcElement: e.tar get;
Var table = getParent (el, "TABLE ");
Var oldCursor = table. style. cursor;
Var oldClick = table. onclick;
Table. style. cursor = "wait ";
Table. onclick = null;
Var fakeEvent = {srcElement: e. srcElement, target: e.tar get };
Window. setTimeout (function (){
SortColumn (fakeEvent );
Table. style. cursor = oldCursor;
Table. onclick = oldClick;
},100 );
}
Function sortColumn (e ){
Var tmp = e.tar get? E.tar get: e. srcElement;
Var tHeadParent = getParent (tmp, "THEAD ");
Var el = getParent (tmp, "TD ");
If (tHeadParent = null)
Ret

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.