Tablesorter. js table sorting method (supports Chinese sorting) and tablesorter Chinese sorting

Source: Internet
Author: User

Tablesorter. js table sorting method (supports Chinese sorting) and tablesorter Chinese sorting

Recently, I have made some exploration on table sorting as needed by the project, as shown below:

1. First, you can download tablesorter. js from the official website, but it does not support Chinese sorting. Modify its source code:

Some source code:

function sortText(a, b) {  return ((a < b) ? -1 : ((a > b) ? 1 : 0));};function sortTextDesc(a, b) {  return ((b < a) ? -1 : ((b > a) ? 1 : 0));};

After modification:

function sortText(a,b) {    return a.localeCompare(b);  };  function sortTextDesc(a,b) {    return b.localeCompare(a);  };

After the modification, JavaScript can support sorting in Chinese.

2. Create a table in the following format:

<table>  <thead>    <tr><th></th></tr>  </thead>  <tr><td></td></tr></table>
<Html> 

Page display:

Click the header to sort.

Note:

1) if the fifth and sixth columns do not need to be sorted, the Code is as follows:

$("#mytable").tablesorter({headers:{4:{sorter:false},5:{sorter:false}}});

(Remove the style of the corresponding column by the way)

2) If the Fifth Column requires a custom sorting method, the sixth column is not sorted. The Code is as follows:

$. Tablesorter. addParser ({id: "grade", // specify a unique ID is: function (s) {return false;}, format: function (s) {return s. toLowerCase (). replace (/expired/, 1 ). replace (/to be reviewed/, 2 ). replace (/reviewed/, 3 ). replace (/Effective/, 4); // replace Chinese with numbers}, type: "numeric" // sort by value}); $ ("# mytable "). tablesorter ({headers: {4: {sorter: "grade"}, 5: {sorter: false }}});

For more examples, visit the official website.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.