How can I have select inputs sorted according to datetime-moment plugin?
How can I have select inputs sorted according to datetime-moment plugin?
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
Hi @gbmapo ,
The trick there is how you create the array, you need to use a custom
sort()
functionCheers,
Colin
Thanks @colin
I finally made it work using
But your solution is much more elegant ;-)
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 :
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
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.
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
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).
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
Yes I saw this function above ->
But I don't know where do I have to put it in my code for it to work ?