1

I have a table of data that is order by rank which can be entered through an input box.

The rank is a single digit number 1 through 9 or it could be blank. I want the blanks to always appear at the end of the list whenever I sort.

See the JSFiddle example: http://jsfiddle.net/afEHc/

If I use:

"aoColumns": [ { "sSortDataType": "dom-text" }, null ]

It works picks up the new values but the order is incorrect. That is the blanks need to always appear at the end.

If I use:

"aoColumns": [{"sType": "data-rank"}, null ]

It works correct initially but then breaks when either I sort by another column first or add a value to the rank column.

Any point or suggestions would be appreciated.

1 Answer 1

1

Yes, unfortunetely you cannot have a $.fn.dataTableExt.afnSortData['data-rank'], at least what I know of.

But you can use the dom-text as you have tried, and set the sType to be data-rank :

"aoColumns": [
   { "sSortDataType": "dom-text", "sType": "data-rank" },
    null
]

new custom sort function :

jQuery.extend( jQuery.fn.dataTableExt.oSort, { 
    "data-rank-asc": function ( a, b ) {
        a = (a==='') ? 99 : a; b = (b==='') ? 99 : b;    
        return ((a < b) ? -1 : ((a > b) ? 1 : 0));
    },
    "data-rank-desc": function ( a, b ) {
        a = (a==='') ? -1 : a; b = (b==='') ? -1 : b;    
        return ((a < b) ? 1 : ((a > b) ? -1 : 0));
    }
});

And now it works! Blanks always appears at the end, both ascending and descending, and also if you enter new values to the input fields.

forked fiddle : http://jsfiddle.net/s29aB/

To clarify the custom sort : I simply just set blanks / "" - values to be -1 on sorting ascending, 99 when sorting descending.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I overlooked the fact that sType and sSortDataType could both be used and the map was throughing me off.

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.