html中table数据排序的js代码_javascript技巧
程序员文章站
2022-05-30 16:16:57
...
对了,注意那个innerText和innerHTML
function sortCells(event) {
var obj = event.target;
var count = 0; count是记录点击次数的,根据奇偶进行升序或降序
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 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 Ar[x] = new Array(column);
}
for (x = 1; x for (y = 0; y Ar[x - 1][y] = table.rows(x).cells(y).innerHTML;
}
}
//这个可以对字符串进行本地化排序
/* 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 for (y = 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]) Ar[y - 1] = Ar[y];
Ar[y] = temp;
}
}
}
}
for (x = 1; x for (y = 0; y table.rows(x).cells(y).innerHTML = Ar[x - 1][y];
}
}
count++;
}
复制代码 代码如下:
function sortCells(event) {
var obj = event.target;
var count = 0; count是记录点击次数的,根据奇偶进行升序或降序
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 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 Ar[x] = new Array(column);
}
for (x = 1; x for (y = 0; y Ar[x - 1][y] = table.rows(x).cells(y).innerHTML;
}
}
//这个可以对字符串进行本地化排序
/* 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 for (y = 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]) Ar[y - 1] = Ar[y];
Ar[y] = temp;
}
}
}
}
for (x = 1; x for (y = 0; y table.rows(x).cells(y).innerHTML = Ar[x - 1][y];
}
}
count++;
}
上一篇: MySQL文件系统先睹为快(1)
推荐阅读
-
IE6中使用position导致页面变形的解决方案(js代码)_javascript技巧
-
JS中动态添加事件(绑定事件)的代码_javascript技巧
-
JS 实现Table相同行的单元格自动合并示例代码_javascript技巧
-
让多个输入框中的内容同时变化的js代码_javascript技巧
-
JS 实现Table相同行的单元格自动合并示例代码_javascript技巧
-
js 实现打印网页中定义的部分内容的代码_javascript技巧
-
js中访问html中iframe的文档对象的代码[IE6,IE7,IE8,FF]_javascript技巧
-
分享一个自定义的console类 让你不再纠结JS中的调试代码的兼容_javascript技巧
-
js 实现打印网页中定义的部分内容的代码_javascript技巧
-
js,jQuery 排序的实现代码,网页标签排序的实现,标签排序_javascript技巧