I have a DataTables.js table with a column that has numeric values with decimal points. Currently, it's being ordered like "1", "1.1", "10", "2"
, but I would like to order it as "1", "1.1", "2", "10"
. Strangely enough, if I click the ordering arrow after the page has loaded, it does order it the way I want. Anyway, according to their documentation, something like this should work:
//Custom ordering function
DataTable.ext.type.order['numeric-decimal-pre'] = function (data) {
return parseFloat(data) || 0;
};
//Initialize datatable
let table = new DataTable("#@(Model.Id)", {
columnDefs: [
{
type: "numeric-decimal",
targets: 0,
},
],
order: [[0, "asc"]],
});
However, while no errors are popping up in the console, it's also not changing the ordering in any way.
That was interesting. I had a play and further research into the documentation, and it seems as though you were perhaps invoking order
incorrectly or in an older manner, this seems to work:
//Custom ordering function
DataTable.ext.type.order['numeric-decimal-pre'] = function(data) {
return parseFloat(data) || 0;
};
(() => {
//Initialize datatable
const table = new DataTable('#example', {
columnDefs: [{
type: "numeric-decimal",
targets: 6,
}, ],
order: [{
idx: 6,
dir: 'asc'
}]
});
})()
Working example here: https://jsfiddle.net/annoyingmouse/bokd3ftj/
However, after further play, you don't need a custom sort filter as this seems to work:
(() => {
//Initialize datatable
const table = new DataTable('#example', {
order: [{
idx: 6,
dir: 'asc'
}]
});
})()