Jquery datatable datetime-moment plugin not sortin

2019-08-09 03:05发布

问题:

I have the following large html table: https://gist.github.com/hbizira/21016ff3df67fdae3969

And I'm using the datetime-moment datatables plugin. At the end of the above file, I have the following javascript code:

$(document).ready(function() {
    $.fn.dataTable.moment( 'MM/DD/YYYY hh:mm A');
    window.dataTable = $('#leads').dataTable({
        displayLength: 25,
        lengthChange: false,
        statesave: false
    }).columnFilter([
            { column_number: 1, filter_type: "range_date", filter_container_id: "leads_filter_date_range", filter_default_label: [ "From", "To" ] },
            { column_number: 11,
              filter_type: 'custom_func',
              custom_func: customStatusFilter,
              data: [
                  { value: 'pending', label: 'Pending' }, 
                  { value: 'qualified', label: 'Qualified' }, 
                  { value: 'pending_and_qualified', label: 'Both Pending & Qualified' }
              ],
              filter_container_id: "leads_filter_status",
              filter_default_label: ""
            },
        ]
    );

});

However, the "last action taken" column doesn't seem to sort properly at all. There are entries from 2015 that don't show up when I attempt to sort by most recent entry in that column.

http://recordit.co/1V8gzsqrso

回答1:

Since the date strings moment.js returns is Date.parse()able' you can do this :

columnDefs : [
    { type : 'date', targets : [13] }
],  

and now the column is sorted correctly.

demo with (most) of your table from github -> http://jsfiddle.net/t6snpgkf/

I believe, but have not tested it in this case, that dataTables determines that the column is of type string since it contain empty values - thus you must force the date type.