Home > Web Front-end > JS Tutorial > body text

js code for sorting table data in html_javascript skills

WBOY
Release: 2016-05-16 18:03:38
Original
1044 people have browsed it

By the way, pay attention to the innerText and innerHTML

Copy code The code is as follows:

function sortCells(event ) {
var obj = event.target;
var count = 0; count records the number of clicks, in ascending or descending order according to odd and even
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;
}
}
    ///This can perform localized sorting of strings
/* 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 ;
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!