Set class = "sortable" on the table label and the table ID.
Two rows are added to the table. Code It is written by jquery. It is used to color the even row.
Copy code The Code is as follows: addevent (window, "LOAD", sortables_init );
VaR sort_column_index;
Function sortables_init (){
// Find all tables with class sortable and make them sortable
If (! Document. getelementsbytagname) return;
Tsung = Document. getelementsbytagname ("table ");
For (Ti = 0; Ti <tstrap. length; Ti ++ ){
Thistbl = Tsung [Ti];
If (''+ thistbl. classname +''). indexof ("sortable ")! =-1) & (thistbl. ID )){
// Inittable (thistbl. ID );
Ts_makesortable (thistbl );
}
}
}
Function ts_makesortable (table ){
If (table. Rows & table. Rows. length> 0 ){
VaR firstrow = table. Rows [0];
}
If (! Firstrow) return;
// We have a first row: assume it's the header, and make its contents clickable links
For (VAR I = 0; I <firstrow. cells. length; I ++ ){
VaR cell = firstrow. cells [I];
VaR TXT = ts_getinnertext (cell );
Cell. innerhtml = '<a href = "#" class = "sortheader"' +
'Onclick = "ts_resorttable (this, '+ I +'); Return false;"> '+
TXT + '<SPAN class = "sortarrow"> </span> </a> ';
}
}
Function ts_getinnertext (EL ){
If (typeof El = "string") return El;
If (typeof El = "undefined") {return El };
If (El. innertext) return El. innertext; // not needed but it is faster
VaR STR = "";
VaR cs = El. childnodes;
VaR L = cs. length;
For (VAR I = 0; I <L; I ++ ){
Switch (CS [I]. nodetype ){
Case 1: // element_node
STR + = ts_getinnertext (CS [I]);
Break;
Case 3: // text_node
STR + = cs [I]. nodevalue;
Break;
}
}
Return STR;
}
Function ts_resorttable (lnk, clid ){
// Get the span
VaR span;
For (var ci = 0; CI <lnk. childnodes. length; CI ++ ){
If (lnk. childnodes [CI]. tagname & lnk. childnodes [CI]. tagname. tolowercase () = 'span ') span = lnk. childnodes [CI];
}
VaR spantext = ts_getinnertext (SPAN );
VaR TD = lnk. parentnode;
VaR column = clid | TD. cellindex;
VaR table = getparent (TD, 'table ');
VaR tableid = $ (table). ATTR ("ID ");
// Work out a type for the column
If (table. Rows. Length <= 1) return;
VaR ITM = ts_getinnertext (table. Rows [1]. cells [column]);
Sortfn = ts_sort_caseinsensitive;
If (ITM. match (/^ \ D [\/-] \ D [\/-] \ D $/) sortfn = ts_sort_date;
If (ITM. Match (/^ \ D [\/-] \ D [\/-] \ D $/) sortfn = ts_sort_date;
If (ITM. Match (/^ [?] /) Sortfn = ts_sort_currency;
If (ITM. Match (/^ [\ D \.] + $/) sortfn = ts_sort_numeric;
Sort_column_index = column;
VaR firstrow = new array ();
VaR newrows = new array ();
For (I = 0; I <Table. Rows [0]. length; I ++) {firstrow [I] = table. Rows [0] [I];}
For (j = 1; j <Table. Rows. length; j ++) {newrows [J-1] = table. Rows [J];}
Newrows. Sort (sortfn );
If (span. getattribute ("sortdir") = 'low '){
Arrow = 'row ';
Newrows. Reverse ();
Span. setattribute ('sortdir', 'up ');
} Else {
Arrow = 'row ';
Span. setattribute ('sortdir', 'low ');
}
// We appendchild rows that already exist to the tbody, so it moves them rather than creating new ones
// Don't do sortbottom rows
For (I = 0; I <newrows. length; I ++) {If (! Newrows [I]. classname | (newrows [I]. classname & (newrows [I]. classname. indexof ('sortbottom ') =-1) table. tbodies [0]. appendchild (newrows [I]);}
// Do sortbottom rows only
For (I = 0; I <newrows. length; I ++) {If (newrows [I]. classname & (newrows [I]. classname. indexof ('sortbottom ')! =-1) Table. tbodies [0]. appendchild (newrows [I]);}
// Delete any other arrows there may be showing
VaR allspans = Document. getelementsbytagname ("span ");
For (var ci = 0; CI <allspans. length; CI ++ ){
If (allspans [CI]. classname = 'sortarrow '){
If (getparent (allspans [CI], "table") = getparent (lnk, "table") {// in the same table as us?
Allspans [CI]. innerhtml = '';
}
}
}
Span. innerhtml = arrow;
$ ('#' + Tableid + 'tr: GT (0): even'mirror.css ("background-color", "fff7e8 ");
$ ('#' + Tableid + 'tr: GT (0): odd'hangzhou.css ("background-color", "# cce8cf ");
}
Function getparent (El, ptagname ){
If (El = NULL) return NULL;
Else if (El. nodetype = 1 & El. tagname. tolowercase () = ptagname. tolowercase () // gecko bug, supposed to be uppercase
Return El;
Else
Return getparent (El. parentnode, ptagname );
}
Function ts_sort_date (a, B ){
// Y2K notes: Two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19xx
AA = ts_getinnertext (A. cells [sort_column_index]);
BB = ts_getinnertext (B. cells [sort_column_index]);
If (AA. Length = 10 ){
Dt1 = AA. substr (6, 4) + AA. substr (3, 2) + AA. substr (0, 2 );
} Else {
Yr = AA. substr (6, 2 );
If (parseint (yr) <50) {yr = '20' + yr;} else {yr = '19' + yr ;}
Dt1 = yr + AA. substr (3, 2) + AA. substr (0, 2 );
}
If (BB. Length = 10 ){
Dt2 = BB. substr (6, 4) + BB. substr (3, 2) + BB. substr (0, 2 );
} Else {
Yr = BB. substr (6, 2 );
If (parseint (yr) <50) {yr = '20' + yr;} else {yr = '19' + yr ;}
Dt2 = yr + BB. substr (3, 2) + BB. substr (0, 2 );
}
If (dt1 = dt2) return 0;
If (dt1 <dt2) Return-1;
Return 1;
}
Function ts_sort_currency (a, B ){
AA = ts_getinnertext (A. cells [sort_column_index]). Replace (/[^ 0-9.]/g ,'');
BB = ts_getinnertext (B. cells [sort_column_index]). Replace (/[^ 0-9.]/g ,'');
Return parsefloat (AA)-parsefloat (bb );
}
Function ts_sort_numeric (a, B ){
AA = parsefloat (ts_getinnertext (A. cells [sort_column_index]);
If (isnan (AA) AA = 0;
BB = parsefloat (ts_getinnertext (B. cells [sort_column_index]);
If (isnan (bb) BB = 0;
Return AA-BB;
}
Function ts_sort_caseinsensitive (a, B ){
AA = ts_getinnertext (A. cells [sort_column_index]). tolowercase ();
BB = ts_getinnertext (B. cells [sort_column_index]). tolowercase ();
If (AA = bb) return 0;
If (AA <bb) Return-1;
Return 1;
}
Function ts_sort_default (a, B ){
AA = ts_getinnertext (A. cells [sort_column_index]);
BB = ts_getinnertext (B. cells [sort_column_index]);
If (AA = bb) return 0;
If (AA <bb) Return-1;
Return 1;
}
Function addevent (ELM, evtype, FN, usecapture)
// Addevent and removeevent
// Cross-browser event handling for ie5 +, ns6 and Mozilla
// By Scott Andrew
{
If (ELM. addeventlistener ){
Elm. addeventlistener (evtype, FN, usecapture );
Return true;
} Else if (ELM. attachevent ){
VaR r = Elm. attachevent ("On" + evtype, FN );
Return R;
} Else {
Alert ("handler cocould not be removed ");
}
}