Sorting of dates works only for rendering
Sorting of dates works only for rendering
Murphy013
Posts: 15Questions: 5Answers: 1
http://live.datatables.net/jedelezu/125/edit
For filtering I wanna fill my dropdowns with column values.
Therefore I used column.data().unique().sort().each( function ( d, j ) {
Unfortunately that doesn't work for date-, currency- and cell with hypertext columns.
So I implemented an own sorting for those columns
initComplete: function () {
this.api().columns().every( function () {
var column = this;
var arr = [];
var i, txt;
var select = $('<select data-placeholder="select entry to filter" multiple class="select2" <option value=""></option></select>')
.appendTo( $(column.header()) )
.on( 'change', function () {
mySearch(column, $(this).val());
});
arr = [];
column.data().unique().sort().each( function ( d, j ) {
if (d.indexOf('<') > -1) {
txt = $(d).text();
} else {
txt = d;
}
arr.push(txt);
} );
if (column.settings()[0].aoColumns[column.index()].sType == 'date') {
arr.sort((date1, date2) => new Date(date1) - new Date(date2));
} else if (column.settings()[0].aoColumns[column.index()].sType == 'html') {
arr.sort();
} else if (column.settings()[0].aoColumns[column.index()].sType == 'num-fmt') {
arr.sort(function(a,b){
a = parseFloat(a.replace(/[\D]/ig,''));
b = parseFloat(b.replace(/[\D]/ig,''));
return a-b;
});
}
for (i = 0; i < arr.length; i++) {
select.append( '<option value="' + arr[i] + '">' + arr[i] + '</option>' );
}
} );
}
Is there a way to use datatables functionality? Maybe i use column.data().unique().sort()
in the wrong way.
This question has an accepted answers - jump to answer
Answers
Not yet. The
sort
function in Javascript is just a string or numeric sort, unless you provide a callback function as you have done. DataTables doesn't yet provide its sorting methods in a way that can be used with the API directly. It is something I've cursed a good few times myself and it is on the cards for future development.Allan
Hello Allan,
thanks for your response.
Regards