欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

html中table數(shù)據(jù)排序的js代碼

 更新時(shí)間:2011年08月09日 23:33:22   作者:  
最近應(yīng)項(xiàng)目要求,需要對(duì)table中的數(shù)據(jù)進(jìn)行升序或降序排列,于是研究了一下js,希望對(duì)大家有幫助。
對(duì)了,注意那個(gè)innerText和innerHTML
復(fù)制代碼 代碼如下:

function sortCells(event) {
var obj = event.target;
var count = 0; count是記錄點(diǎn)擊次數(shù)的,根據(jù)奇偶進(jìn)行升序或降序
if(!obj.getAttribute("clickCount")){
obj.setAttribute("clickCount", 0);
} else {
count = parseInt(obj.getAttribute("clickCount"));
count++;
obj.setAttribute("clickCount", count);
}
var table = event.target.parentNode.parentNode;
if(table.nodeName.localeCompare("THEAD") == 0){
if(table.parentNode.nodeName.localeCompare("TABLE") == 0){
table = table.parentNode;
} else {
return;
}
} else if(table.nodeName.localeCompare("TBODY") == 0){
if(table.parentNode.nodeName.localeCompare("TABLE") == 0){
table = table.parentNode;
} else {
return;
}
} else if(table.nodeName.localeCompare("TABLE") == 0){
} else {
return;
}
var colNum;
for(x = 0; x < table.rows(1).cells.length; x++){
if(event.target.innerText.localeCompare(table.rows(0).cells[x].innerText) == 0){
colNum = x;
break;
}
}
var column = table.rows(1).cells.length;
var row = table.rows.length;
var Ar = new Array(row - 1);
for (x = 0; x < row - 1; x++) {
Ar[x] = new Array(column);
}
for (x = 1; x < row; x++) {
for (y = 0; y < column; y++) {
Ar[x - 1][y] = table.rows(x).cells(y).innerHTML;
}
}
     //這個(gè)可以對(duì)字符串進(jìn)行本地化排序
/* if((count%2) == 0){
Ar.sort(function(a, b) {
return b[colNum].localeCompare(a[colNum])
});
} else {
Ar.sort(function(a, b) {
return a[colNum].localeCompare(b[colNum])
});
} */
var temp;
for (x = 0; x < row - 1; x++) {
for (y = 1; y < row - 1; y++) {
temp = Ar[y - 1];
if((count % 2) == 0){
if (parseInt(Ar[y - 1][colNum]) >= parseInt(Ar[y][colNum])) {
Ar[y - 1] = Ar[y];
Ar[y] = temp;
}
} else {
if (parseInt(Ar[y - 1][colNum]) <= parseInt(Ar[y][colNum])) {
Ar[y - 1] = Ar[y];
Ar[y] = temp;
}
}
}
}
for (x = 1; x < row; x++) {
for (y = 0; y < column; y++) {
table.rows(x).cells(y).innerHTML = Ar[x - 1][y];
}
}
count++;
}

相關(guān)文章

最新評(píng)論