Sort in column take original data instead of returned data

Sort in column take original data instead of returned data

MausinoMausino Posts: 61Questions: 19Answers: 0
edited May 2023 in DataTables

Hi, one question to non serverside ordering.

https://datatables.net/reference/option/columns.type

if raw data in cell is for example = "" or "x" or 123 (mixed types)

{ targets: 1, data: 'ID', title: 'Id', orderData: [1, 2], orderable: true, className: 'dt-center', type: 'num',
                render: function (data, type, row, meta, title) {

                    if ( type === 'display' || type === 'filter' ) {

                        let numbersOnly = parseInt(data.replace(/[^0-9]/g, ''), 10);
                        numbersOnly = isNaN(numbersOnly) ? 0 : numbersOnly;
                    }
                    console.log('data ID', data);

                    return data;
                }
            },

So now i return still only numbers.... but Datatables still take original data (which is mix of empty strings, numbers, strings) but no returned data which is only numbers for sorting

How can i fix that sorting will in this column works only by return values which is now only numbers?

Regards
Mikolaj

Answers

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    edited May 2023

    Add sort as described in the Orthogonal data docs. See if this works:

    if ( type === 'display' || type === 'filter'  || type === 'sort' ) {
    

    Or possibly remove the if statement altogether, like this:

                    render: function (data, type, row, meta, title) {
                        let numbersOnly = parseInt(data.replace(/[^0-9]/g, ''), 10);
                        numbersOnly = isNaN(numbersOnly) ? 0 : numbersOnly;
                        console.log('data ID', data);
     
                        return data;
                    }
    

    Kevin

  • MausinoMausino Posts: 61Questions: 19Answers: 0

    Hi, @kthorngren

    I tried both your suggestion.

    The results was for both same. If i clicked on column name to sort column... i saw only arrow change position but records stayed in same order.

    Datatables for sort somehow in core take raw data for each cell in column and use it as default, no matter if you have render function on your columnDefs.

    I tried use insead of type: 'num' this, but withou any success.

                    type: function(data) {
    
                        let numbersOnly = parseInt(data.replace(/[^0-9]/g, ''), 10);
                        numbersOnly = isNaN(numbersOnly) ? 0 : numbersOnly;
                        data = numbersOnly;
    
                        return data;
    
                    },
    

    Regards
    Mikolaj

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    The columns.type supports only a string not a function. Use the type option of columns.render to set the type. See the Orthogonal data docs for more details.

    Datatables does automatic type detection. Forcing the type by setting the columns.type for sorting is not typically going to work and not recommended for this purpose.

    Please build a simple test case that has an example of your data so we can take a look and offer suggestions.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

Sign In or Register to comment.