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.