This article mainly introduced the JS table header sort realizes the method, involves the numeral, the letter, the string comparison and the sorting and so on the operation skill, has certain reference value, the need friend may refer to under
-->
In this paper, we describe the method of scheduling JS table header. Share to everyone for your reference.
The implementation methods are as follows:
The code is as follows:
<script type= "Text/javascript" >
Whether to decrement the sort
var isdescending = true;
/*****************************************
* The rows to be sorted must be placed in the <tbody></tbody> label
* TableId: Sorted form ID
* Colno: Sorted column number, which is the first column, starting from 0
* Startrowno: Start line number of the sort, starting from 0
* Sortlength: The number of rows to sort,
* Type: Types of row sequences
*/
function sort (tableId, Colno, Startrowno, sortlength, type)
{
If the number of rows to sort is 1 or 0, it is not sorted
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 removed from the header header and the table-bit rows
var therows = new Array (sortlength);
Looping data in a 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;
}
To compare a number
function Comparenumber (x, y)
{
Conversion of data in 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 (a);
}
Else
{
Return Parsefloat (a)-parsefloat (b);
}
}
To compare a string
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;
}
}
Comparison of 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;
}
}
Removes all specified strings in a string
String.prototype.excludeChars = function (chars) {
var matching = new RegExp (chars, "G");
return This.replace (Matching, "");
}
</script>
I hope this article will help you with your JavaScript programming.