2013-02-14 23 views
1

我开始了我的搜索,以实现“NaturalSort”中的jqGrid实现“NaturalSort”,我有NaturalSort Javascript代码如下:如何在jqGrid的

this.naturalSort = function (a, b) { 
    var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi, 
     sre = /(^[ ]*|[ ]*$)/g, 
     dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/, 
     hre = /^0x[0-9a-f]+$/i, 
     ore = /^0/, 
     i = function (s) { return naturalSort.insensitive && ('' + s).toLowerCase() || '' + s }, 
    // convert all to strings strip whitespace 
     x = i(a).replace(sre, '') || '', 
     y = i(b).replace(sre, '') || '', 
    // chunk/tokenize 
     xN = x.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0'), 
     yN = y.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0'), 
    // numeric, hex or date detection 
     xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x)), 
     yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null, 
     oFxNcL, oFyNcL; 
    // first try and sort Hex codes or Dates 
    if (yD) 
     if (xD < yD) return -1; 
     else if (xD > yD) return 1; 
    // natural sorting through split numeric strings and default strings 
    for (var cLoc = 0, numS = Math.max(xN.length, yN.length); cLoc < numS; cLoc++) { 
     // find floats not starting with '0', string or 0 if not defined (Clint Priest) 
     oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0; 
     oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0; 
     // handle numeric vs string comparison - number < string - (Kyle Adams) 
     if (isNaN(oFxNcL) !== isNaN(oFyNcL)) { return (isNaN(oFxNcL)) ? 1 : -1; } 
     // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2' 
     else if (typeof oFxNcL !== typeof oFyNcL) { 
      oFxNcL += ''; 
      oFyNcL += ''; 
     } 
     if (oFxNcL < oFyNcL) return -1; 
     if (oFxNcL > oFyNcL) return 1; 
    } 
    return 0; 
}; 

我试过,但没有找到一个远高于消费代码通过jqGrid。任何人都可以引导我如何通过使用上述代码或其他方式来实现'NaturalSort'。

在这方面的任何帮助将不胜感激!

+0

下GitHub库提起这个问题:https://github.com/tonytomov/jqGrid/issues/430。 – 2013-08-11 08:24:43

+0

新功能已添加到jquGrid中,并将在未来的版本中提供。这是新功能:https://github.com/tonytomov/jqGrid/commit/6131d8a464243d1120278f99a9cdf053246b518f。我添加了解决此问题并结束讨论的答案。 – 2014-02-10 14:19:08

回答

1

托尼 - jqGrid的创建者已经增加了新的功能来处理这里定制排序:https://github.com/tonytomov/jqGrid/commit/6131d8a464243d1120278f99a9cdf053246b518f

这将是在未来的jqGrid的版本中提供。这里是由托尼以下修改代码来处理NaturalSort顺序:

...jqGrid({ 
... 
data: mydata, 
... 
colModel : [ 
... 
{"name": "field1",...,"sortfunc": naturalSort, ...}, 
... 
], 
... 
}); 

注意的排序功能,您将需要通过3个PARAMATERS,其中3个参数是升序排序顺序1和-1的下降。 在这种情况下,您的naturalSort函数应该有3个参数。以下是我修改后的代码。

/* 
* Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license 
* Author: Jim Palmer (based on chunking idea from Dave Koelle) 
*/ 
function naturalSort (a, b, d) { 
    if(d===undefined) { d=1; } 
    var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi, 
     sre = /(^[ ]*|[ ]*$)/g, 
     dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/, 
     hre = /^0x[0-9a-f]+$/i, 
     ore = /^0/, 
     i = function(s) { return naturalSort.insensitive && (''+s).toLowerCase() || ''+s }, 
     // convert all to strings strip whitespace 
     x = i(a).replace(sre, '') || '', 
     y = i(b).replace(sre, '') || '', 
     // chunk/tokenize 
     xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), 
     yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), 
     // numeric, hex or date detection 
     xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x)), 
     yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null, 
     oFxNcL, oFyNcL; 
    // first try and sort Hex codes or Dates 
    if (yD) 
     if (xD < yD) return -d; 
     else if (xD > yD) return d; 
    // natural sorting through split numeric strings and default strings 
    for(var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) { 
     // find floats not starting with '0', string or 0 if not defined (Clint Priest) 
     oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0; 
     oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0; 
     // handle numeric vs string comparison - number < string - (Kyle Adams) 
     if (isNaN(oFxNcL) !== isNaN(oFyNcL)) { return (isNaN(oFxNcL)) ? d : -d; } 
     // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2' 
     else if (typeof oFxNcL !== typeof oFyNcL) { 
      oFxNcL += ''; 
      oFyNcL += ''; 
     } 
     if (oFxNcL < oFyNcL) return -d; 
     if (oFxNcL > oFyNcL) return d; 
    } 
    return 0; 
} 

对于记录问题的详细信息,请访问此链接:https://github.com/tonytomov/jqGrid/issues/430

naturalSort function results

+0

新版本的jqgrid v4.6.0已经发布,其中包含许多错误修复和功能:http://www.trirand.com/jqgridwiki/doku.php?id=wiki:changetwo#jqgrid_4.6.0_changes_and_fixes。可以从这里下载:http://www.trirand.com/blog/?page_id=6 – 2014-02-21 06:50:09