Custom Sorting - Advice

Custom Sorting - Advice

jrichviewjrichview Posts: 36Questions: 7Answers: 0

I'm interested in using the HTML5 data attrs to implement some custom sorting, but my sort requirements are kind of complex. The grid has two text columns then a bunch of numeric (percentages for the sake of discussion). Key requirements when sorting by a numeric column:
1) always secondarily sort by the name column (one of the text columns)
2) If a row contains "N/A" rather than a percentage, ALWAYS sort that to the bottom regardless of asc/desc
3) There can be a row or two that define averages, and those rows must always be at the top, again regardless of asc/desc.

It does not appear that I could render a data-order attribute in such a way that it would always sort top or bottom, correct? So, I gather that means I need to write my own custom sort code, and the jquery.fn.dataTableExt.oSort approach won't work as it only compares the values from a single column, whereas I would need to check multple column values.

Anyone able to point me up the right tree?

Answers

  • jrichviewjrichview Posts: 36Questions: 7Answers: 0
    edited June 2014

    Let me add that the sort on the text column referred to in 1) needs to always be ascending. So, can't really use "orderData" as the second column always sorts in the same order as the first column.

  • jrichviewjrichview Posts: 36Questions: 7Answers: 0
    edited June 2014

    Since I've had no takers, let me ask a more specific question.

    I'm creating my "numeric" columns with:

                    "orderDataType": "hedisMeasure",
    

    Then I have a sort function:

                   $.fn.dataTable.ext.order['hedisMeasure'] = function (oSettings, iCol, visICol) {
    

    In the sort function, I return as follows:
    IF it's a straight up rating or percentage, return that number (ratings 1-5, percentage 1-100)
    ELSE IF it's "N/A" return either 1000 or -1 depending whether it's ASC sort
    ELSE if this is a statewide average row, return -1 or 1000 depending ASC sort

    I have verified the array being returned looks like the right numbers. However, it's not sorting as if the values are numeric.

    So, I figured I'd try creating functions for sorting. Example:

        jQuery.fn.dataTableExt.oSort['hedisMeasure-asc'] = function (x, y) {
    
        jQuery.fn.dataTableExt.oSort['hedisMeasure-desc'] = function (x, y) {
    

    Apparently this method is not called if I have an "order" function assigned. If I remove that these get called.

    Anyone know how to make this sort work?

  • WuigiWuigi Posts: 10Questions: 2Answers: 0

    Well you just solved my problem by understanding why the -asc / -desc functions were not called anymore in the new version (which I guess is a bug?) ; the solution to your problem is probably in the column.render() function, which kind of replaces the "order" function (this is how I do it now and it works fine).

    You may have solved your problem already since you posted long ago, but if you haven't and want more detail, let me know and I'll elaborate!

  • jrichviewjrichview Posts: 36Questions: 7Answers: 0

    Actually, in the interest of deadlines I backed out all the sorting capabilities and implemented it server side. So DataTables is being used on our site in a very limited capacity at present. Clients turned out not to want the paging capabilities, and they didn't seem to care about filtering a whole lot. It's possible I may remove it entirely, depending which direction they go and which direction the code goes.

    Thanks for your response, though.

This discussion has been closed.