How to sort js headers and js Headers

Source: Internet
Author: User

How to sort js headers and js Headers

This article describes how to sort js headers. Share it with you for your reference.

The specific implementation method is as follows:

Copy codeThe Code is as follows:
<Script type = "text/javascript">
// Whether to decrease the order
Var isDescending = true;
/*************************************** **
* The row to be sorted must be placed in the <tbody> </tbody> label.
* TableId: the ID of the sorting table.
* ColNo: Number of the column to be sorted, that is, the column number, starting from 0.
* StartRowNo: the starting line number of the sorting, starting from 0.
* SortLength: number of rows to be sorted,
* Type: the type of the sort column.
*/
Function sort (tableId, colNo, startRowNo, sortLength, type)
{
// If the number of rows to be sorted is 1 or 0, the sorting operation is not performed.
If (sortLength <= 1 ){
Return;
}
Var currTable = document. getElementById (tableId );
Var theHeader = currTable. outerHTML. substring (0, currTable. outerHTML. indexOf ('<TBODY>') + 7)
Var theFooter = currTable. outerHTML. substring (currTable. outerHTML. indexOf ('</TBODY>')-8 );
// The number of rows here is the number of rows that are removed from the header, header, and table.
Var theRows = new Array (sortLength );
// Loop the data in the table
For (I = startRowNo; I <sortLength + startRowNo; I ++)
{
TheRows [I-startRowNo] = new Array (currTable. rows [I]. cells [colNo]. innerText. toLowerCase (), currTable. rows [I]. outerHTML );
}
If (type. toUpperCase () = 'number ')
{
TheRows. sort (compareNumber );
}
Else if (type. toUpperCase () = 'date ')
TheRows. sort (compareDate );
Else if (type. toUpperCase () = 'string ')
TheRows. sort (compareString );
Var tableInfo =''
For (j = 0; j <theRows. length; j ++)
{
TableInfo + = theRows [j] [1];
}
IsDescending =! IsDescending;
CurrTable. outerHTML = theHeader + tableInfo + theFooter;
Return;
}
// Compare numbers
Function compareNumber (x, y)
{
// Convert the data in the currency format
A = x [0]. excludeChars (","). trim ();
B = y [0]. excludeChars (","). trim ();
 
If (a = "") {a = 0 ;}
If (B = "") {B = 0 ;}
If (isDescending)
{
Return parseFloat (B)-parseFloat ();
}
Else
{
Return parseFloat (a)-parseFloat (B );
}
}
// Compare strings
Function compareString (x, y)
{
If (isDescending)
{
If (x [0]> y [0]) return-1;
Else if (x [0] <y [0]) return 1;
Else return 0;
}
Else
{
If (x [0] <y [0]) return-1;
Else if (x [0]> y [0]) return 1;
Else return 0;
}
}
// Compare the time
Function compareDate (x, y ){
Var arr = x [0]. split ("-");
Var starttime = new Date (arr [0], arr [1], arr [2]);
Var starttimes = starttime. getTime ();
Var arrs = y [0]. split ("-");
Var lktime = new Date (arrs [0], arrs [1], arrs [2]);
Var lktimes = lktime. getTime ();

If (isDescending)
{
Return lktimes-starttimes;
}
Else
{
Return starttimes-lktimes;
}
}
// Remove all specified strings from the string
String. prototype. excludeChars = function (chars ){
Var matching = new RegExp (chars, "g ");
Return this. replace (matching ,'');
}
</Script>

I hope this article will help you design javascript programs.

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.