Javascript code _ javascript skills for sorting table data in html

Source: Internet
Author: User
Recently, in response to project requirements, we need to sort the data in the table in ascending or descending order. So I studied js and hope it will be helpful to you. By the way, pay attention to the innerText and innerHTML

The Code is as follows:


Function sortCells (event ){
Var obj = event.tar get;
Var count = 0; count indicates the number of clicks, in ascending or descending order based on parity.
If (! Obj. getAttribute ("clickCount ")){
Obj. setAttribute ("clickCount", 0 );
} Else {
Count = parseInt (obj. getAttribute ("clickCount "));
Count ++;
Obj. setAttribute ("clickCount", count );
}
Var table = event.tar get. 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.tar get. 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;
}
}
// Local 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 ++;
}

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.