本文实例讲述了JavaScript对表格或元素按文本,数字或日期排序的方法。分享给大家供大家参考。具体实现方法如下:
y) ? 1 : 0));
};
var DescText = function (a,b) { // sort() by .data
var x = a.data,yNum = parseFloat(y);
// check if each begin with a number..
if ( !isNaN(xNum) && !isNaN(yNum) && (yNum - xNum) )
return yNum - xNum;
return ((x > y) ? -1 : ((x < y) ? 1 : 0));
};
var AscNum = function (a,b) { // used with dates as well
return a.data - b.data;
};
var DescNum = function (a,b) {
return b.data - a.data;
};
if (parent.nodeName.toLowerCase() == 'table') {
if ( childTag == 'tr' ) {
sortBy = parent.rows[0].cells[colIndex].sortBy || 'text';
}
parent = parent.tBodies[0] || parent;
if ( parent.rows[0].cells[0].nodeName.toLowerCase() == 'th' ) {
startAt = 1;
}
}
cTags = parent.getElementsByTagName(childTag);
if ( typeof colIndex == 'undefined' ) {
sortBy = 'text'; // sort simple lists or paragraphs as text
}
for (i = startAt,childLen = cTags.length; i < childLen; i++) {
// ..go forward examining each child
aChild = cTags[i];
elem = (colTag) ? aChild.getElementsByTagName(colTag)[colIndex] : aChild;
if (elem) {
if ( !sortBy ) { // sorting non-table columns..
sortBy = (typeof elem.numberValue != 'undefined') ? 'number' :
((typeof elem.dateValue != 'undefined') ? 'date' : 'text');
}
switch (sortBy) {
// You can supply 'sort' attributes to enable sorting of numbers,etc.
// For example, for a date.
case 'text':
content = (elem.getAttribute('sort') ||
elem.firstChild.nodeValue).toLowerCase();
break;
case 'number':
content = elem.numberValue;
break;
case 'date':
content = elem.dateValue;
break;
default:
content = (elem.getAttribute('sort') ||
elem.firstChild.nodeValue).toLowerCase();
break;
}
j = elems.length;
if ( !aChild.id )
aChild.id = 'tempSortID' + j;
elems[j] = { data: content,tempID: aChild.id };
}
}
// The following will determine if the table/etc has already been sorted
// by the same column or tag. If so,it will sort in ascending or descending
// order. It creates custom element properties to the parent element to
// remember the prevIoUs sort details.
if ( typeof colIndex == 'undefined' ) colIndex = 0;
if ( parent.prevTag && parent.prevTag == ((typeof colTag == 'undefined') ?
childTag : colTag) ) {
if (parent.prevCol == colIndex) {
// sorting by the same column as prevIoUsly
parent.prevSort = (parent.prevSort == 'asc') ? 'desc' : 'asc';
} else { // sorting by any other column
parent.prevCol = colIndex;
parent.prevSort = 'asc';
}
} else {
// sorting for the 1st time or by a different tag
parent.prevTag = ((typeof colTag == 'undefined') ? childTag : colTag);
parent.prevCol = colIndex;
parent.prevSort = 'asc';
}
if ( parent.prevSort === 'desc' ) {
// 'desc' WILL BE the prevIoUs sort order..
switch (sortBy) {
case 'text': elems.sort(DescText); break;
case 'number': elems.sort(DescNum); break;
case 'date': elems.sort(DescNum); break;
default: elems.sort(DescText); break;
}
} else {
switch (sortBy) {
case 'text': elems.sort(AscText); break;
case 'number': elems.sort(AscNum); break;
case 'date': elems.sort(AscNum); break;
default: elems.sort(AscText); break;
}
}
frag = document.createDocumentFragment();
for (i = 0,sortedLen = elems.length; i < sortedLen; i++) {
elem = document.getElementById(elems[i].tempID);
frag.appendChild(elem);
if ( (elem.id).substr(0,10) == 'tempSortID' )
elem.removeAttribute('id');
}
parent.appendChild(frag);
elems = null;
return parent.prevSort; // not currently used
};
var AddSortToTables = function () {
// ..if table has class-name 'sortIt'
var tables = document.getElementsByTagName('table'),i,tblLen,tbl,hdrs,hdrsLen;
function PreserveSortScope(a,b,c,d) {
return function () {
// assign the SortElements fn. to a table header
SortElements(a,d);
}
}
// add sorting to table headers
for ( i = 0,tblLen = tables.length; i < tblLen; i++ ) {
tbl = tables[i];
if ( tbl.className.indexOf('sortIt') + 1) {
hdrs = tbl.getElementsByTagName('th');
if ( hdrs ) {
for ( j = 0,hdrsLen = hdrs.length; j < hdrsLen; j++ ) {
AddEvent(hdrs[j],'click',PreserveSortScope(tbl,j));
// if there's no title already,add "Click to sort"
if ( !hdrs[j].title ) hdrs[j].setAttribute('title','Click to sort');
}
}
}
}
};
var AddSortByDate = function (tbl,dateMask) {
// Input: the table name (or object),a column index (or array)
// and a date mask ('dd-mmm-yy')
// Adds a sortBy = 'date' property to the first row
// will ignore the first row,assuming it is a header row
var i,rLen,cell;
while ( col.length ) AddSortByDate(tbl,col.pop(),dateMask);
if ((col instanceof Array) || isNaN(col)) return;
var tbl = (typeof tbl === 'string') ? document.getElementById(tbl) : tbl;
tbl.rows[0].cells[col].sortBy = 'date';
AddSortByDate2(tbl,dateMask);
};
var AddSortByDate2 = function (parentEl,dateMask) {
var kids,cell;
var parent = (typeof parentEl === 'string') ?
document.getElementById(parentEl) : parentEl;
if ( parent.nodeName.toLowerCase() == 'table' ) {
parent = parent.tBodies[0] || parent;
startAt = ( parent.rows[0].cells[0].nodeName.toLowerCase() == 'th' ) * 1;
}
kids = parent.getElementsByTagName(childTag);
for ( i = startAt,rLen = kids.length; i < rLen; i++) {
cell = kids[i].getElementsByTagName(colTag)[colIndex];
if (cell) {
if ( typeof cell.numberValue != 'undefined' ) delete cell.numberValue;
// the above enables switching from a number to a date sort
// (although v. unlikely)
if (cell.getAttribute('sort')) {
// use sort attribute if present
cell.dateValue = new Date(cell.getAttribute('sort'));
} else {
cell.dateValue = new Date(StringToDate(cell.firstChild.nodeValue,dateMask));
}
if (cell.dateValue.toString() == "NaN" || cell.dateValue.toString() ==
"Invalid Date") {
cell.dateValue = 0;
}
}
}
};
var AddSortByNumber = function (tbl,col) {
// col is a column index or array of indices
// will ignore the first row,cell,tempNum;
while ( col.length ) AddSortByNumber(tbl,col.pop());
if ((col instanceof Array) || isNaN(col)) return;
tbl = (typeof tbl === 'string') ? document.getElementById(tbl) : tbl;
tbl.rows[0].cells[col].sortBy = 'number';
AddSortByNumber2(tbl,col);
};
var AddSortByNumber2 = function (parentEl,colIndex) {
var kids,tempNum;
var parent = (typeof parentEl === 'string') ?
document.getElementById(parentEl) : parentEl;
if ( parent.nodeName.toLowerCase() == 'table' ) {
parent = parent.tBodies[0] || parent;
startAt = (parent.rows[0].cells[0].nodeName.toLowerCase() == 'th') * 1;
}
kids = parent.getElementsByTagName(childTag);
for (i = startAt,rLen = kids.length; i < rLen; i++) {
cell = kids[i].getElementsByTagName(colTag)[colIndex];
if (cell) {
if ( typeof cell.dateValue != 'undefined' ) delete cell.dateValue;
// the above enables switching from a date to a number sort
// (although v. unlikely)
tempNum = cell.getAttribute('sort') || cell.firstChild.nodeValue;
tempNum = tempNum.replace(/[^0-9.-]/g,'');
cell.numberValue = parseFloat(tempNum);
if (isNaN(cell.numberValue))
cell.numberValue = 0.0;
}
}
};
var StringToDate = function (sDate,sFormat,cutOff) {
// Input: a date value as a string,it's format as a string e.g. 'dd-mmm-yy'
// Optional: a cutoff (integer) for 2 digit years.
// If no 'd' appears in the format string then the 1st of the month is assumed.
// If the year is 20 and the cut-off is 30 then the value will be converted
// to 2020; if the year is 40 then this will be converted to 1940.
// If no cut-off is supplied then '20' will be pre-pended to the year (YY).
// Output: a string in the format 'YYYY/MM/DD' or ''
// Will not attempt to convert certain combinations e.g. DMM,MDD,DDM,YYYYD.
var sParsed,fndSingle;
// sParsed will be constructed in the format 'YYYY/MM/DD'
sDate = sDate.toString().toUpperCase();
sFormat = sFormat.toUpperCase();
if (sFormat.search(/MMMM|MMM/) + 1) { // replace Mar/March with 03,etc.
sDate = sDate.replace(new RegExp('(' + ShortMths.join('|') + ')[A-Z]*','gi'),function (m) {
var i = ShortMths.indexOf(m.charAt(0).toUpperCase() +
m.substr(1,2).toLowerCase()) + 1;
return ((i < 10) ? "0" + i : "" + i).toString();
});
sFormat = sFormat.replace(/MMMM|MMM/g,'MM');
}
if (sFormat.search(/DDDD|DDD/) + 1) { // replace Tue/Tuesday,etc. with ''
sDate = sDate.replace(new RegExp('(' + ShortDays.join('|') + ')[A-Z]*','');
sFormat = sFormat.replace(/DDDD|DDD/g,'');
}
sDate = sDate.replace(/(^|\D)(\d)(?=\D|$)/g,function($0,$1,$2) {
// single digits 2 with 02
return $1 + '0' + $2;
});
sFormat = sFormat.replace(/(^|[^DMY])(D|M)(?=[^DMY]|$)/g,$2){
return $1 + $2 + $2; // replace D or M with DD and MM
});
// are there still single Ds or Ms?
fndSingle = sFormat.search(/(^|[^D])D([^D]|$)|(^|[^M])M([^M]|$)/)+1;
if ( fndSingle ) return ''; // do not attempt to parse,for example,'DMM'
sFormat = sFormat.replace(/(^|[^Y])(YY)(?=[^Y]|$)/g,$2,index) {
var tempDate = sDate.substr(0,index + 1);
tempDate += (cutOff) ? ((parseInt(sDate.substr(index + 1,2),10) > cutOff) ?
'19' : '20') : '20';
tempDate += sDate.substr(index + 1);
sDate = tempDate;
return $1 + $2 + $2;
});
sParsed = ('YYYY/MM/DD').replace(/YYYY|MM|DD/g,function(m){
return (sFormat.indexOf(m) + 1) ?
sDate.substr(sFormat.indexOf(m),m.length) : '';
});
if (sParsed.charAt(0) == '/') {
// if no year specified,assume the current year
sParsed = (new Date().getFullYear()) + sParsed;
}
if (sParsed.charAt(sParsed.length - 1) == '/') {
// if no date,assume the 1st of the month
sParsed += '01';
}
// should end up with 10 characters..
return ( sParsed.length == 10 ) ? sParsed : '';
};
希望本文所述对大家的javascript程序设计有所帮助。