2013-02-14 5 views
1

j'ai commencé ma recherche à mettre en œuvre « NaturalSort » dans jqGrid, j'ai le code Javascript pour NaturalSort comme ci-dessous:Comment mettre en œuvre « NaturalSort » dans 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; 
}; 

J'ai essayé mais n'a pas trouvé un moyen de consommer au-dessus code via jqGrid. Quelqu'un peut-il me guider comment nous pouvons atteindre «NaturalSort» en utilisant le code ci-dessus ou d'une autre manière.

Toute aide à cet égard sera grandement appréciée!

+0

déposé cette question sous repo github: https://github.com/tonytomov/jqGrid/issues/430. –

+0

Une nouvelle fonctionnalité a été ajoutée dans jquGrid et sera disponible dans une version ultérieure. Voici la nouvelle fonctionnalité: https://github.com/tonytomov/jqGrid/commit/6131d8a464243d1120278f99a9cdf053246b518f. J'ajoute une réponse pour résoudre ce problème et clôturer cette discussion. –

Répondre

1

Tony - créateur de jqGrid a été ajouté nouvelle fonctionnalité pour gérer le tri personnalisée ici: https://github.com/tonytomov/jqGrid/commit/6131d8a464243d1120278f99a9cdf053246b518f

Il serait disponible dans les versions à venir de jqGrid. Voici ci-dessous le code modifié par Tony pour gérer NaturalSort commande:

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

Notez que la fonction de tri, vous devrez passer 3 paramaters, où les 3 paramètres est l'ordre de tri 1 pour monter et -1 pour la descente. Dans ce cas, votre fonction naturalSort doit avoir 3 paramètres. Voici le code modifié de moi.

/* 
* 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; 
} 

Pour plus de détails de l'émission connecté, visitez ce lien: https://github.com/tonytomov/jqGrid/issues/430

naturalSort function results

+0

Nouvelle version de jqgrid v4.6.0 qui contient de nombreux correctifs et fonctionnalités: http://www.trirand.com/jqgridwiki/doku.php?id=wiki:changetwo#jqgrid_4.6.0_changes_and_fixes. Le téléchargement est disponible ici: http://www.trirand.com/blog/?page_id=6 –

Questions connexes