0

I am using jquery datatable to display data. I display '--' when there is no data. Currently when the table sorts the data all the '--' comes in the beginning and the order looks like below:

--
--
10
20
400
800

But I need to make '--' to be displayed last when sorted in ascending order and should look something like below:

10
20
400
800
--
--

Please let me know how can we get this behavior in jquery datatable?

1

2 Answers 2

1

you can use an exstension

jQuery.extend(jQuery.fn.dataTableExt.oSort, {
    "myorder-pre": function (a) {

    },

    "myorder-asc": function (a, b) {
        if(a == '--' && b != '--')
            return 1;
        else if(b == '--' && a != '--')
            return -1;
        else if(b == '--'&& a == '--')
            return 0;
        else
        {
            a = parseFloat(a);
            b = parseFloat(b);
            return ((a < b) ? -1 : ((a > b) ? 1 : 0));

        }
    },

    "myorder-desc": function (a, b) {
        if(a == '--' && b != '--')
            return -1;
        else if(b == '--' && a != '--')
            return 1;
        else if(b == '--'&& a == '--')
            return 0;
        else
        {
            a = parseFloat(a);
            b = parseFloat(b);
            return ((a < b) ? 1 : ((a > b) ? -1 : 0));

        }
    }
});

myorder-pre is used before all the order call. myorder-asc when you order asc. Return number negative if a minor b, positive if a major b, 0 if equal. Desc work adverse

then in the definition of columns of datatable, use

"aoColumnDefs": [{ "sType": 'myorder'}]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This is exactly what I was looking for. I will update my comments once I incorporate your implementation. Thanks once again.
0

You can make use of the following code :

 $('#example').dataTable( {
    "aaSorting": [[ 4, "desc" ]]
} );

For the reference

1 Comment

No, this solution would not work as expected since we need to define the definition on asc and desc sort with regards to '--'. With the code snippet you have given it only sorts in descending order using the default behavior. Hope I am able to express clearly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.