Search code examples
yadcf

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?


Solution

  • $('#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
        }
    ]);