WordPress.org

Ready to get started?Download WordPress

Forums

TablePress
[resolved] DataTables Sorting not working (12 posts)

  1. jackandthejuice
    Member
    Posted 8 months ago #

    Hi Tobias,

    at first, let me thank you for this wonderful and nice working plugin. Unfortunately I have a little problem with sorting numbers as also mentioned in following posts:

    - http://wordpress.org/support/topic/plugin-tablepress-sorting-by-column-for-values-with-leading-sign?replies=10
    - http://wordpress.org/support/topic/tablepress-not-sorting-columns-correctly
    - http://wordpress.org/support/topic/data-table-sorting-plugin-issue?replies=17

    Alright, let me tell you what's the problem: Sorting the table with numbers (in my case currencies) is not working properly. Clicking the cols head only works in chrome, but not in Safari or Firefox. In addition the sorting isn't correct: Three clicks on the cols title provide three different results and only the last one is correct.

    I use the latest version of Tablepress and the DataTables Sorting Extension. Also tried changing custom commands from "aoColumnDefs": [ { "sType": "numeric-comma", "aTargets": [ 4 ] } ] to "aoColumnDefs": [ { "sType": "num-html", "aTargets": [ 4 ] } ]
    - but no chance. Changing the values (row 4) from "xx Euro" to "xx €" or "xx€" had no effect too. But maybe you could find an error ...

    It would be very nice, if you'll take a look on the website. You've got mail with the link.

    Thanks a lot and have a nice weekend!
    jackandthejuice

    http://wordpress.org/plugins/tablepress/

  2. TobiasBg
    Member
    Plugin Author

    Posted 8 months ago #

    Hi,

    thanks for your post, and sorry for the trouble.

    The problem here is that there's no sorting algorithm at the moment (in the Extension) that can account for both the comma and the € symbol.

    Therefore, please try adding one to both of the JS files in the Extension:

    /**
     * Formatted numbers with a comma
     */
    
    "formatted-num-comma-pre": function ( a ) {
    		a = ( a == "-" ) ? 0 : a.replace( /,/, "." );
    		a = a.replace( /[^\d\-\.]/g, "" );
    		return parseFloat( a );
    },
    "formatted-num-comma-asc": function ( a, b ) {
    		return a - b;
    },
    "formatted-num-comma-desc": function ( a, b ) {
    		return b - a;
    },

    Put that right before the lines

    /**
     * Numeric Comma (numbers like 0,5), NOT detected automatically!
     */

    Then, please try this for the "Custom Commands":

    "aoColumnDefs": [ { "sType": "formatted-num-comma", "aTargets": [ 4 ] } ]

    This should then fix the sorting in the "effektive monatliche Kosten" column.

    Regards,
    Tobias

  3. jackandthejuice
    Member
    Posted 8 months ago #

    Hi,

    thanks for your very fast response. Unfortunately your workaround didn't fixed the problem. Now sorting isn't possible anymore as you can see.

    I added the lines above to jquery.datatables.sorting-plugins.js and also changed the custom command but it had no effect.

    Thanks for your help!

  4. TobiasBg
    Member
    Plugin Author

    Posted 8 months ago #

    Hi,

    you'll also have to add it to the minified version of the file. For now, it's probably the easiest to simply copy the modified content of the non-minified file into the minified file.

    Regards,
    Tobias

  5. jackandthejuice
    Member
    Posted 8 months ago #

    Hi,

    ok, thanks - not sure about that. I added the code also to the minified js-file but sorting is still impossible. Any further ideas?

    Thanks!

  6. TobiasBg
    Member
    Plugin Author

    Posted 8 months ago #

    Hi,

    just adding it at the end is not enough, it needs to be in the same position. As that's not easy in the minified file, I meant this:
    Please delete "jquery.datatables.sorting-plugins.min.js". Then, create a copy of the file "jquery.datatables.sorting-plugins.min.js" and rename it to "jquery.datatables.sorting-plugins.min.js".

    Regards,
    Tobias

  7. jackandthejuice
    Member
    Posted 8 months ago #

    Hi,

    alright, sorting is working now, but still not in the right way as you can see.

    Thanks!

  8. TobiasBg
    Member
    Plugin Author

    Posted 8 months ago #

    Hi,

    ah, I just saw it now... (I hadn't taken a look into the HTML source of the page.)
    The problem here is that those cells contain a lot of HTML code, for those small tooltip popups. Those also contain numbers, and as those are not filtered out, they are being taken into account in the sorting, thus giving the wrong results.

    You will therefore need a different approach here (which should then also work for the "monatliche Grundgebühr" column:
    Next to those, you'll need to add a new column in the table that only gets the numerical value of the cell. For example, on the right of the cell "25,78€" there should then be a cell with the content (and only that) "25.78".
    We can then hide that extra columns with some more JavaScript code and tell the "real" column to use the data from that column for the sorting.
    After you have added the column, please remove the "Custom Command"

    "aoColumnDefs": [ { "sType": "formatted-num-comma", "aTargets": [ 4 ] } ]

    again, and instead add

    "aoColumnDefs": [ { "iDataSort": 5, "aTargets": [ 4 ] }, { "bVisible": false, "aTargets": [ 5 ] } ]

    That will hide the new column, but the current column will use it for sorting.

    Regards,
    Tobias

  9. jackandthejuice
    Member
    Posted 8 months ago #

    Hi Tobias,

    thanks for your reply. I already supsected the html code in the columns behind this failure. I'll use the weekend and give your proposal a try - you will hear from me soon. :)

    Thanks a lot for helping and the really great support! Have a nice weekend!

  10. TobiasBg
    Member
    Plugin Author

    Posted 8 months ago #

    Hi,

    alright, sounds good. As you seem to have some knowledge on DataTables already (with the extra filtering code in your site), the DataTables Documentation at http://www.datatables.net/ should also be helpful with the syntax of those "Custom Commands", as those pretty much are pure DataTables commands.

    Regards,
    Tobias

  11. jackandthejuice
    Member
    Posted 8 months ago #

    Hi Tobias,

    thanks for your solutions. I've just tried your way and it works really fine. I will appreciate your help with a donation.

    Best regards!

  12. TobiasBg
    Member
    Plugin Author

    Posted 8 months ago #

    Hi,

    no problem, you are very welcome! :-) Good to hear that this helped!

    And thanks for the gift, I really appreciate it! :-)

    Best wishes,
    Tobias

    P.S.: In case you haven't, please rate TablePress here in the plugin directory. Thanks!

Reply

You must log in to post.

About this Plugin

About this Topic