Juan de Bourbon
Juan de Bourbon

Reputation: 3

Sorting the value zero in JQuery DataTables YADCF doesn't work

We have a big problem when using the JQuery library DataTables with jquery.dataTables.yadcf : sorting doesn't work when we want to select the value 'zero', for example in a column witch concern delay in second. But if we add another value, the two are sorted!

Someone know if it's normal or if we can find a solution to avoid it?

Upvotes: 0

Views: 25

Answers (1)

Atalı Web
Atalı Web

Reputation: 16

$('#example').DataTable({
    columnDefs: [
        {
            targets: 0, // Adjust the target index based on your column
            type: 'num' // Ensure the column is treated as numeric
        }
    ]
});



$.fn.dataTable.ext.order['num-asc'] = function (settings, col) {
    return this.api()
        .column(col, { order: 'index' })
        .data()
        .map(function (value) {
            return value === '0' ? -Infinity : parseFloat(value);
        });
};

$.fn.dataTable.ext.order['num-desc'] = function (settings, col) {
    return this.api()
        .column(col, { order: 'index' })
        .data()
        .map(function (value) {
            return value === '0' ? Infinity : parseFloat(value);
        });
};

$('#example').DataTable({
    columnDefs: [
        { targets: 0, orderDataType: 'num' }
    ]
});



yadcf.init($('#example').DataTable(), [
    {
        column_number: 0, // Set your correct column number
        filter_type: 'range_number' // Adjust filter type if needed
    }
]);

Upvotes: 0

Related Questions