2012-04-05 5 views
1

Below-Code zu verwenden ist entnommen aus: http://datatables.net/forums/discussion/2467/need-help-for-sorting-date-with-ddmmyyyy-format/p1Unable Datentabellen Datum Sortierer jQuery-Plugin

Hier ist, wie ich den Aufruf der Funktionalität:

$('#tab').dataTable({ 
     'aoColumns': [null, null, null, null, { "sType": "date-euro" }] 

});

Ich habe den folgenden Code zu einer separaten .js-Datei hinzugefügt. Wie rufe ich die Sortierfunktionalität auf?

function trim(str) { 
    str = str.replace(/^\s+/, ''); 
    for (var i = str.length - 1; i >= 0; i--) { 
     if (/\S/.test(str.charAt(i))) { 
      str = str.substring(0, i + 1); 
      break; 
     } 
    } 
    return str; 
} 

(function($) { 
     $.fn.dataTableExt.oSort['date-euro-asc'] = function(a, b) { 
    if (trim(a) != '') { 
     var frDatea = trim(a).split(' '); 
     var frTimea = frDatea[1].split(':'); 
     var frDatea2 = frDatea[0].split('/'); 
     var x = (frDatea2[2] + frDatea2[1] + frDatea2[0] + frTimea[0] + frTimea[1] + frTimea[2]) * 1; 
    } else { 
     var x = 10000000000000; // = l'an 1000 ... 
    } 

    if (trim(b) != '') { 
     var frDateb = trim(b).split(' '); 
     var frTimeb = frDateb[1].split(':'); 
     frDateb = frDateb[0].split('/'); 
     var y = (frDateb[2] + frDateb[1] + frDateb[0] + frTimeb[0] + frTimeb[1] + frTimeb[2]) * 1;      
    } else { 
     var y = 10000000000000;      
    } 
    var z = ((x < y) ? -1 : ((x > y) ? 1 : 0)); 
    return z; 
}; 
})(jQuery); 

(function($) { 
     $.fn.dataTableExt.oSort['date-euro-desc'] = function(a, b) { 
    if (trim(a) != '') { 
     var frDatea = trim(a).split(' '); 
     var frTimea = frDatea[1].split(':'); 
     var frDatea2 = frDatea[0].split('/'); 
     var x = (frDatea2[2] + frDatea2[1] + frDatea2[0] + frTimea[0] + frTimea[1] + frTimea[2]) * 1;      
    } else { 
     var x = 10000000000000;      
    } 

    if (trim(b) != '') { 
     var frDateb = trim(b).split(' '); 
     var frTimeb = frDateb[1].split(':'); 
     frDateb = frDateb[0].split('/'); 
     var y = (frDateb[2] + frDateb[1] + frDateb[0] + frTimeb[0] + frTimeb[1] + frTimeb[2]) * 1;      
    } else { 
     var y = 10000000000000;      
    }     
    var z = ((x < y) ? 1 : ((x > y) ? -1 : 0));     
    return z; 
}; 
})(jQuery); 
+0

verwenden Ist das Ihre eigentliche Code? Sie vermissen ein '' 'hier:' $ ('# tab) .dataTable ({...}); '. Sind Sie sicher, dass jQuery geladen wird, bevor Sie DataTables initialisieren? –

+0

Ich habe die fehlende Frage hinzugefügt, die ich beim Versenden der Frage versehentlich entfernt habe. jQuery wird geladen, bevor ich DataTables initialisiere, andere dataTables-Funktionen funktionieren wie erwartet. – user701254

Antwort

0

Wenn Sie eine Tabelle mit vier Spalten haben, sollten Sie

$('#tab').dataTable({ 
     'aoColumns': [null, null, null, { "sType": "date-euro" }] 
}); 
+0

Ich habe eine Tabelle mit fünf Spalten, das Datum wird der fünften Spalte hinzugefügt. – user701254