Juery _ jquery

Source: Internet
Author: User
This article describes how Juery solves the problem of tablesorter's Chinese sorting and character range. The example shows how jQuery solves tablesorter's Chinese sorting and character range, for more information about how Juery solves tablesorter's Chinese sorting and character range, see the following example. Share it with you for your reference. The specific analysis is as follows:

Tablesorter is an excellent table sorting plug-in jQuery. I believe everyone has used it or heard it. I will not introduce it here. For details, refer to the official website: http://tablesorter.com/docs/ (where the demo is relatively complete ).

In several projects developed using tablesorter, there are two types of sorting problems, as shown below:

The first problem is that Chinese characters cannot be sorted because the unicode value is used for character size comparison during character sorting. The Code is as follows:

Js 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));};function sortText(a,b) {  return ((a < b) ? -1 : ((a > b) ? 1 : 0));};function sortTextDesc(a,b) {  return ((b < a) ? -1 : ((b > a) ? 1 : 0));};

The result we want to get is sorted in Chinese pinyin order, so we can change the code to the following code:

Js Code

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

The localeCompare method is the built-in method of JS. Needless to say, we can see that this method compares the character size in the current region, but this method cannot process polyphonic words.

The second problem is that you cannot sort numeric data that exceeds the specified range. This is because the data value is distorted during Numerical Conversion. For example:

Js Code

alert(parseFloat('9999999999999999'));  // 10000000000000000alert(parseFloat('10000000000000001')); // 10000000000000000alert(parseFloat('10000000000000004')); // 10000000000000004alert(parseFloat('10000000000000005')); // 10000000000000004alert(parseFloat('10000000000000006')); // 10000000000000006alert(parseFloat('9999999999999999'));  // 10000000000000000alert(parseFloat('10000000000000001')); // 10000000000000000alert(parseFloat('10000000000000004')); // 10000000000000004alert(parseFloat('10000000000000005')); // 10000000000000004alert(parseFloat('10000000000000006')); // 10000000000000006

This deviation will make the sorting result inaccurate. To avoid this problem, we should not use the original value for comparison, but introduce the weight value, the value from left to right, the values corresponding to each digit decrease, and then the new values calculated based on the weights and the original values are used for comparison. Therefore, you only need to modify the formatFloat method to solve this problem.

Js Code

this.formatFloat = function(s) {      // TODO      var i = parseFloat(s);      return (isNaN(i)) ? 0 : i;    };

I hope this article will help you with jQuery programming.

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.