How can I have select inputs sorted according to datetime-moment plugin?

How can I have select inputs sorted according to datetime-moment plugin?

gbmapogbmapo Posts: 17Questions: 6Answers: 0

Using this example https://datatables.net/examples/api/multi_filter_select.html and this example live.datatables.net/saqozowe/3/edit (thanks to kthorngren), I wrote this code

jQuery(document).ready(function ($) {

  $.fn.dataTable.moment( 'MM/YYYY' );

  $('#ListOfMembers').DataTable({
    "info": true,
    "paging": false,
    "columnDefs": [{
        "targets": [2, 3, 7],
        "orderable": false
    }],
    orderCellsTop: true,
    "dom": '<"top"if>',
    fixedHeader: {
        header: true,
        footer: false
    },
    initComplete: function () {
        this.api().columns([1, 4, 5]).every( function (i) {
            var column = this;
            var select = $('<select><option value=""></option></select>')
                .appendTo( $("#ListOfMembers thead tr:eq(1) th").eq(column.index()).empty() )
                .on( 'change', function () {
                    var val = $.fn.dataTable.util.escapeRegex(
                        $(this).val()
                    );
                    column
                        .search( val ? '^'+val+'$' : '', true, false )
                        .draw();
                } );
            column.data().unique().sort().each( function ( d, j ) {
                select.append( '<option value="'+d+'">'+d+'</option>' )
            } );
        } );
    }
  });

} );

resulting in this table:

Columns are sortable, except for 'Adresse', 'Téléphone' and 'Actions'.
'Début' and 'Fin' are dates (format MM/YYYY) and they are properly sorted when I click in the header, thanks to datetime-moment plugin.
But the content of the corresponding select input is not. It is sorted 'by string' (which is as expected, considering the code).

How can I sort these two select input with datetime-moment?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Hi @gbmapo ,

    The trick there is how you create the array, you need to use a custom sort() function

    column.data().unique().sort( function (a,b) {
      return moment(a, "MM/YYYY").unix() - moment(b, "MM/YYYY").unix();
    } )
    

    Cheers,

    Colin

  • gbmapogbmapo Posts: 17Questions: 6Answers: 0

    Thanks @colin
    I finally made it work using

                column.data().unique().sort(function(a, b) {
                  x = moment(a, "MM/YYYY");
                  y = moment(b, "MM/YYYY");
                  return ((x < y) ? -1 : ((x > y) ? 1 : 0));
                })
    

    But your solution is much more elegant ;-)

  • AsueAsue Posts: 4Questions: 0Answers: 0

    Hi, I have the same problem ! (except me it's "DD/MM/YYYY")

    I try your solution but I don't know where do I have to put it ?

    My code is like this :

    var noColonne = 0;
    
    $.fn.dataTable.moment('DD/MM/YYYY'); // the order works but not in the filter
        $('#table').DataTable({
            "paging": false,
            "bLengthChange": false,
            "bFilter": true,
            "bInfo": false,
            "searching" : true,
            "info": false,
            initComplete: function ()
            {
                this.api().columns().every( function ()
                {
                    var column = this;
                    var select = $('<select class="select" data-placeholder="  " multiple data-no-colonne="' + noColonne + '">  </select>')
                    .appendTo( $(column.footer()).empty() );
    
                    column.data().unique().each(function (d, j)
                    {
                        select.append( '<option value="'+d+'" data-no-colonne="' + noColonne + '" data-actions-box="true">'+d+'</option>' );
                    });
    
                    // CODE HERE BUT DOESN'T WORK
                    column.data().unique().sort( function (a,b) { 
                          return moment(a, "DD/MM/YYYY").unix() - moment(b, "DD/MM/YYYY").unix();
                    });
    
                    noColonne++;
                });
    
                $('.select').multiselect(
                    {
                        maxHeight: 280,
                        dropDown: true,
                        enableFiltering: true,
                        filterPlaceholder: "Search",
                        enableCaseInsensitiveFiltering: true,
    
                        onChange: function(element, checked)
                        {
    // things inside
                        }
                    }
                );
            }
        });
    
  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @Asue ,

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • AsueAsue Posts: 4Questions: 0Answers: 0
    edited March 2019

    Hi @colin , I don't know how to link the moment.js library so for the test case I just take a picture of my problem. The js code is just above.

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    You can embed a JS library on http://live.datatables.net just as you would on any html page. Indeed, there are already <script> tags shown in the default html.

    As Colin says, we'd need a link to a test case showing the issue to be able to help.

    Allan

  • AsueAsue Posts: 4Questions: 0Answers: 0

    Hi @allan , yes indeed I tried to add the library with the "add library" button :# ...

    This is the test case : http://live.datatables.net/cejizato/1/edit?html,css,js,console,output

    The sorting is good when I click on a column, but the values on the dropdown list are not sorted in the correct order (for the start date).

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @Asue ,

    You can add the sort(), like here, which will sort the values in each column. This will do a string search by default, but you can do a custom search with a function - you would need to create one of those for the date ordering.

    Cheers,

    Colin

  • AsueAsue Posts: 4Questions: 0Answers: 0
    edited March 2019

    Yes I saw this function above ->

    column.data().unique().sort( function (a,b) {
      return moment(a, "DD/MM/YYYY").unix() - moment(b, "DD/MM/YYYY").unix();
    } )
    

    But I don't know where do I have to put it in my code for it to work ?

This discussion has been closed.