Jquery tablesorter. js supports improved sorting of Chinese tables

Source: Internet
Author: User
Tags jquery tablesorter

Code : It is written like a netizen's code. I don't remember where it came from. It can be changed to support Chinese sorting and control value sorting. No way, limited capabilities, only... Copy code The Code is as follows: // converter, which converts the field type of the column to the sortable type: String, Int, float
Function convert (svalue, sdatatype)
{
Switch (sdatatype)
{
Case "int ":
Return parseint (svalue );
Case "float ":
Return parsefloat (svalue );
Case "date ":
Return new date (date. parse (svalue ));
Default:
Return svalue. tostring ();
}
}
// Chinese Character sorting method
Function chrcomp (A, B)
{
Return A. localecompare (B );
}
// Sorting Function Generator
Function generatecomparetrs (icol, sdatatype, isinput, SDEC)
{
Return function comparetrs (otr1, otr2)
{
If (isinput = 1)
{
VaR vvalue1 = convert (otr1.getelementsbytagname ("input") [icol]. value );
VaR vvalue2 = convert (otr2.getelementsbytagname ("input") [icol]. value );
}
Else
{
VaR vvalue1 = convert (otr1.cells [icol]. firstchild. nodevalue, sdatatype );
VaR vvalue2 = convert (otr2.cells [icol]. firstchild. nodevalue, sdatatype );
}
If (SDEC = 'desc ')
{
If (sdatatype = 'int ')
{
Return vvalue1 = vvalue2? 0 :( vvalue1-vvalue2 <0? 1:-1 );
}
Else if (sdatatype = 'cn ')
{
If (chrcomp (vvalue1, vvalue2)> 0)
{
Return-1;
}
Else if (chrcomp (vvalue1, vvalue2) <0)
{
Return 1;
}
Else
{
Return 0;
}
}
Else
{
If (vvalue1> vvalue2 ){
Return-1;
} Else if (vvalue1 <vvalue2 ){
Return 1;
} Else {
Return 0;
}
}
}
Else if (SDEC = 'asc ')
{
If (sdatatype = 'int ')
{
Return vvalue1 = vvalue2? 0 :( vvalue1-vvalue2> 0? 1:-1 );
}
Else if (sdatatype = 'cn ')
{
Return chrcomp (vvalue1, vvalue2 );
}
Else
{
If (vvalue1> vvalue2 ){
Return 1;
} Else if (vvalue1 <vvalue2 ){
Return-1;
} Else {
Return 0;
}
}
}
};
}
// Reset the cell classname
Function changeclsname (TR, num)
{
Num = num % 2? ;
Num. tostring ();
For (VAR I = 0; I <tr. childnodes. length; I ++)
{
Tr. childnodes [I]. classname = "row" + num
}
}
/* Sorting method (main function)
ID of the stableid table
Icol indicates the column Index
1. If it is not input type, icol indicates the nth Td of TR;
2. When the input type is used, icol indicates the number of inputs in this TR;
Sdatatype indicates the Data Type of the cell or the data type of the input value. The default value is string or Int, and float. CN is Chinese.
Isinput indicates whether the sorted content is input (1 is, 0 is not)
SDEC indicates reverse or reverse order (DESC, default order) to avoid direct reverse order when the input value is changed and then sorted.
*/
Function sorttable (stableid, icol, sdatatype, isinput, SDEC)
{
VaR otable = Document. getelementbyid (stableid );
VaR otbody = otable. tbodies [0];
VaR coldatarows = otbody. Rows;
VaR ATRS = new array;
// Put all columns into an array
For (VAR I = 0; I <coldatarows. length; I ++)
{
ATRS [I] = coldatarows [I];
}
ATRS. Sort (generatecomparetrs (icol, sdatatype, isinput, SDEC ));
VaR ofragment = Document. createdocumentfragment ();
For (VAR I = 0; I <ATRS. length; I ++)
{
Ofragment. appendchild (ATRS [I]);
Changeclsname (ATRS [I], I );
}
Otbody. appendchild (ofragment );
}

I finally have time to study jquery this week and have been eager to learn it. It is a pity that the company does not use it. In fact, after learning it, I still feel that it is used. After learning about jqery, I looked at other people's plug-ins. Jquery. tablesorter. JS is a powerful plug-in. I tried and found that Chinese characters are not supported. Let's look at the source code. Copy code The Code is as follows:/* sorting methods */
Function multisort (table, sortlist, cache ){
If (table. config. Debug) {var sorttime = new date ();}
VaR dynamicexp = "Var sortwrapper = function (a, B) {", L = sortlist. length;
For (VAR I = 0; I <L; I ++ ){
VaR c = sortlist [I] [0];
VaR order = sortlist [I] [1];
VaR S = (getcachedsorttype (table. config. parsers, c) = "text ")? (Order = 0 )? "Sorttext": "sorttextdesc"): (Order = 0 )? "Sortnumeric": "sortnumericdesc ");
VaR E = "E" + I;
Dynamicexp + = "Var" + E + "=" + S + "(a [" + C + "], B [" + C + "]);";
Dynamicexp + = "If (" + E + ") {return" + E + ";}";
Dynamicexp + = "else {";
}
For (VAR I = 0; I <L; I ++ ){
Dynamicexp + = "};";
}
Dynamicexp + = "Return 0 ;";
Dynamicexp + = "};";
Eval (dynamicexp );
Cache. Normalized. Sort (sortwrapper );
If (table. config. Debug) {benchmark ("sorting on" + sortlist. tostring () + "and Dir" + order + "time:", sorttime );}
Return cache;
};
Function sorttext (a, B ){
Return (A <B )? -1: (A> B )? 1: 0 ));
};
Function sorttextdesc (a, B ){
Return (B <)? -1: (B> )? 1: 0 ));
};
Function sortnumeric (a, B ){
Return A-B;
};
Function sortnumericdesc (a, B ){
Return B-;
};
Function getcachedsorttype (parsers, I ){
Return parsers [I]. type;
};

At first, I thought I would encounter some difficulties. Unexpectedly, I just need to change his sorting function to OK.

Copy code The Code is as follows: function sorttext (a, B ){
Return A. localecompare (B );
};
Function sorttextdesc (a, B ){
Return-A. localecompare (B );
};

Test it. Both Chinese and English, and empty columns in the middle.
Originally, this plug-in has a jquery. tablesorter. Pack. js compressed size of 9kb, but I will only change jquery. tablesorter. js (23kb ). I don't know how to convert it into jquery. tablesorter. Pack. js.

PS: of course, this plug-in also has many extension functions, such as multi-column sorting and various examples which can be referenced in documents. There should be a lot of online files and examples. If you have any children's shoes, you can leave me an email.

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.