JavaScript table Data Sort code

Source: Internet
Author: User

Data sorting sounds like only php tutorials, ASP tutorials, SQL to achieve, today we provide a data can be sorted at the client JS code, the need for friends can refer to.

function Sortcells (event) {
var obj = Event.target;
var count = 0; Count is the number of clicks recorded, depending on the parity in ascending or descending order
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 + +) {
AR[X] = new Array (column);
}
for (x = 1; x < row; + +) {
for (y = 0; y < column; y++) {
Ar[x-1][y] = table.rows (x). Cells (y). InnerHTML;
}
}
This can be sorted to localize the string
/* 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 + +) {
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; + +) {
for (y = 0; y < column; y++) {
Table.rows (x). Cells (y). InnerHTML = Ar[x-1][y];
}
}
count++;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.