Sorting on percentage

Sorting on percentage

scoonscoon Posts: 16Questions: 0Answers: 0

I have percentage data to sort, but datatables sorts on the first number encountered reading left to right instead of the number value, for example 10%, 2%, 24%, versus the expected 2%, 10%, 24%.

I saw you have a plugin for handing percentages, but it is marked as deprecated with a note to see how to handle percentage sorting in the details on the page (but the details don't mention anything). When trying to implement the code from that plugin page, all datatable functionality breaks.

Do you guys have a solution for handling percentage data?

Thanks

Replies

  • scoonscoon Posts: 16Questions: 0Answers: 0

    I also tried some tips from here:
    https://datatables.net/reference/option/columns.type

    Modifying my datatables like so:

     $(document).ready( function () {
                            $('#postDeploymentTable').dataTable({
                                "columns": [
                                null,
                                null,
                                null,
                                null,
                                null,
                                null,
                                null,
                                { "type": "num-fmt" }
                                ]
                               dom: 'Blfrtip',
                               
                               buttons: [
                                   'copy', 'csv', 'excel', 'print'
                               ]
                            } );
                       } );
    

    Which broke the datatable functionality. Note that the table in this case has 8 columns, and I'm attempting to apply the format "num-fmt" from here:
    https://datatables.net/reference/option/columns.type

  • kthorngrenkthorngren Posts: 21,146Questions: 26Answers: 4,918

    My guess the reason the plugin is deprecated is because Datatables automatically determines the type for the column snd will automatically sort on that type. Looks like Datatables will automatically detect percentages as shown here:
    http://live.datatables.net/kivufave/1/edit

    If the column data is not consistent, having a non-percentage for example, in the column then type detection will break and it will be sorted as a string. Check to make sure all the data in that column is percentages.

    If you continue to have problems please post a link to your page or a test case (update mine if you want) replicating the issue.
    http://live.datatables.net/kivufave/1/edit

    Kevin

  • scoonscoon Posts: 16Questions: 0Answers: 0

    Yes, there are values of 'unknown' that the datasource returns. I thought that the sort type from the plugin would ignore html and sort on the number. I'll look into how we might parse the incoming data in the mean time.

    thanks

  • kthorngrenkthorngren Posts: 21,146Questions: 26Answers: 4,918

    You cold update that plugin code to handle unknown. You can update this section of code to replace unknown or other values with 0:

        "percent-pre": function ( a ) {
            var x = (a == "-") ? 0 : a.replace( /%/, "" );
            return parseFloat( x );
        },
    

    For example:
    http://live.datatables.net/hokovozi/1/edit

    Kevin

This discussion has been closed.