multiple columndefs with moment date format

multiple columndefs with moment date format

LapointeLapointe Posts: 430Questions: 81Answers: 4
edited November 2019 in Free community support

Hi @Allan, Hi @colin

I read a link sample here (from colin): http://live.datatables.net/nizanuzo/24/edit

    $(document).ready(function () {

        $.fn.dataTable.moment('M/D/YYYY h:mm a'); 
          
        $('#tblDataTable').DataTable({
            paging: false,
            columnDefs: [{targets: [0, 3, 5] }]
        });   
});

$.fn.dataTable.moment('M/D/YYYY h:mm a'); is not set explicitly for columns else using targets...

So, how to mix this columndefs target setting like

$('#tblDataTable').dataTable( {
  "columnDefs": [ {
      "targets": [ 2, 4 ],
      "orderable": false
    },{
      "targets": [0, 1, 2],
      "searchable": false
    }, {
       targets: [0, 3, 5]    
       //** to be linked with date moment setting (use sort plugins)**
       //** $.fn.dataTable.moment('M/D/YYYY h:mm a');**
    } ]   } );

Another one question...
target vs targets is usable in both cases?
In colin sample, target was used for columnsdefs, in documentation targets for columnsdefs and target for responsive details

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    Answer ✓

    target vs targets is usable in both cases?

    No. As you noted the columnDefs.targets requires the s meaning the option is targets. The example you linked to has a typo with target. But also note the type option is commented out.

    $.fn.dataTable.moment('M/D/YYYY h:mm a'); is not set explicitly for columns else using targets...

    Generally you don't need to set anything in the columns or columnDefs to apply the moment plugin. As described in the columns.type docs Datatables has some built in types that it will automatically detect. If the all the data in the column does not match one of these types it defaults to string. You can use the moment sorting plugin when your date formats doesn't match the builtin date formats.

    The format ('M/D/YYYY h:mm a') you use needs to match the format of the dates in the column. If it doesn't then the column will be sorted as a string. I took the example you linked to and changed one of the dates from 6/19/2018 11:57 am to 06/19/2018 11:57 am. This change doesn't match the defined format of `'M/D/YYYY h:mm a'. The column is now not sorted by dates but as a string. Here is the example:
    http://live.datatables.net/bayisaji/1/edit

    The short version is that 'M/D/YYYY h:mm a' is likely not matching the format in the column if its not sorting correctly by datetime. You will need to look at the moment docs to create the proper format.

    Kevin

  • LapointeLapointe Posts: 430Questions: 81Answers: 4

    thanks @kthorngren

    I did understand what you wrote else the fact that there is no reason to set targets for use moment sort option...

    Is it correct ?

This discussion has been closed.