2012-07-03 6 views
0

Voici le code ..Jquery Datatable Trier par date erreur

 $.fn.dataTableExt.oSort['us_date-asc'] = function (a, b) { 

     var x = new Date(a), 
     y = new Date(b); 
     return ((x < y) ? -1 : ((x > y) ? 1 : 0)); 
    }; 

    $.fn.dataTableExt.oSort['us_date-desc'] = function (a, b) { 
     var x = new Date(a), 
     y = new Date(b); 
     return ((x < y) ? 1 : ((x > y) ? -1 : 0)); 
    }; 

    var oTable = $('#example').dataTable({ 
     "bJQueryUI": true, 
     "sPaginationType": "full_numbers", 
     //"bSortClasses": false, 
     "aoColumns": [ 
     null, null, 
     { 
      "sType": "us_date" 
     }, 
     null, 
     { 
      "bSortable": false 
     }], 
     "aaSorting": [ 
      [2, "desc"] 
     ] 
    }); 

Am en utilisant ce code pour datatable faire la troisième colonne sortable. Je veux afficher la date au format jun-06-2012. Le tri fonctionne très bien lorsque j'utilise le format 06-06-2012 ... mais le tri ne fonctionne pas (ça marche en chrome mais pas dans les autres navigateurs) quand le mois est représenté alphabétiquement ... Comment puis-je m'y attaquer? Toute aide sera appréciée

Répondre

1
jQuery.fn.dataTableExt.oSort['shortdate-asc'] = function(x,y) { 
     var months = {}; 
       months["JAN"] = "01"; 
       months["FEB"] = "02"; 
       months["MAR"] = "03"; 
       months["APR"] = "04"; 
       months["MAY"] = "05"; 
       months["JUN"] = "06"; 
       months["JUL"] = "07"; 
       months["AUG"] = "08"; 
       months["SEP"] = "09"; 
       months["OCT"] = "10"; 
       months["NOV"] = "11"; 
       months["DEC"] = "12"; 

     x = (x=="")? 0 : x.split('-'); 
     y = (y=="")? 0 : y.split('-'); 

     if(x.length){ 
      x = x[2] + months[x[0].toUpperCase()] + x[1]; 
     } 

     if(y.length){ 
      y = y[2] + months[y[0].toUpperCase()] + y[1]; 
     } 



     return ((x < y) ? -1 : ((x > y) ? 1 : 0)); 
    }; 


    jQuery.fn.dataTableExt.oSort['shortdate-desc'] = function(x,y) { 

     var months = {}; 
       months["JAN"] = "01"; 
       months["FEB"] = "02"; 
       months["MAR"] = "03"; 
       months["APR"] = "04"; 
       months["MAY"] = "05"; 
       months["JUN"] = "06"; 
       months["JUL"] = "07"; 
       months["AUG"] = "08"; 
       months["SEP"] = "09"; 
       months["OCT"] = "10"; 
       months["NOV"] = "11"; 
       months["DEC"] = "12"; 

     x = (x=="")? 0 : x.split('-'); 
     y = (y=="")? 0 : y.split('-'); 

     if(x.length){ 
      x = x[2] + months[x[0].toUpperCase()] + x[1]; 
     } 

     if(y.length){ 
      y = y[2] + months[y[0].toUpperCase()] + y[1]; 
     } 

     return ((x < y) ? 1 : ((x > y) ? -1 : 0)); 
    }; 
+0

merci frère ... Ça marche ........... – Shanib

0

Vous pouvez utiliser une bibliothèque comme datejs pour convertir des chaînes Arbit aux objets réels de la date et de les comparer. L'idée est de séparer ce que vous affichez de ce que vous comparez réellement.

Exemple de code:

var x = Date.parse('06-jun-2012'); 
var y = Date.parse('06-jul-2012'); 
return Date.compare(x, y); 

Vous pouvez utiliser la date des objets toString() et spécifiez une chaîne de format personnalisé. Vérifiez la documentation http://code.google.com/p/datejs/wiki/APIDocumentation

Vous ne voudrez peut-être pas prendre en charge la dépendance d'une autre bibliothèque s'il s'agit d'une tâche ponctuelle. Si toutefois vous manipulez des dates partout dans votre application, vous voudrez utiliser cette bibliothèque.