sort table
点击标题排序
name
salary
extension
start date
bloggs, fred
$12000.00
1353
18/08/2003
turvey, kevin
$191200.00
2342
02/05/1979
mbogo, arnold
$32010.12
2755
09/08/1998
shakespeare, bill
$122000.00
3211
12/11/1961
shakespeare, hamnet
$9000
9005
01/01/2002
fitz, marvin
$3300
5554
22/05/1995
alpha's blog
复制代码 代码如下:
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;
tbls = document.getelementsbytagname(table);
for (ti=0;ti thistbl = tbls[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 var cell = firstrow.cells[i];
var txt = ts_getinnertext(cell);
cell.innerhtml = ' 'onclick=ts_resorttable(this, '+i+');return false;>' +
txt+'';
}
}
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 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 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');
// work out a type for the column
if (table.rows.length var itm = ts_getinnertext(table.rows[1].cells[column]);
sortfn = ts_sort_caseinsensitive;
if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = ts_sort_date;
if (itm.match(/^\d\d[\/-]\d\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 for (j=1;j
newrows.sort(sortfn);
if (span.getattribute(sortdir) == 'down') {
arrow = '↑';
newrows.reverse();
span.setattribute('sortdir','up');
} else {
arrow = '↓';
span.setattribute('sortdir','down');
}
// 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 // do sortbottom rows only
for (i=0;i
// delete any other arrows there may be showing
var allspans = document.getelementsbytagname(span);
for (var ci=0;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;
}
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) 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) dt2 = yr+bb.substr(3,2)+bb.substr(0,2);
}
if (dt1==dt2) return 0;
if (dt1 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 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 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 could not be removed);
}
}