Viewing 15 replies - 1 through 15 (of 19 total)
  • Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    you are right, empty cells are treated as “0”, and therefore, they are usually sorted to the top, in columns with positive numbers.

    Unfortunately, I’m not aware of a “Custom Command” to change this. This is something that’s deeper in the DataTables JavaScript library. If this is really essential to you, I suggest that you try to find a solution in the documentation of the DataTables library, at https://www.datatables.net
    Very likely, you will need to develop a custom sorting algorithm.

    It might be easier to just avoid empty cells, if somehow possible…

    Regards,
    Tobias

    Thread Starter KingShinobi

    (@kingshinobi)

    i could put a “-” into the cell instead of leaving it empty. could it then be modified to sort it on the bottom?

    i will take a look at the datatables documentation. it’s the last thing, to make my ranking tables complete…

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    unfortunately, my answer with a “-” is the same: I don’t know ?? This will likely also require a modified sorting algorithm, that treats “-” as a very large value (like infinity). But that’s something for the DataTables documentation or forums. Sorry ??

    Regards,
    Tobias

    Thread Starter KingShinobi

    (@kingshinobi)

    i googled a bit and found this js: https://stackoverflow.com/questions/9294042/javascript-ascending-number-sort-with-an-exception

    var myPriceList = [ 0, 1, 0, 1, 5, 3];
    
    myPriceList.sort(function(a,b){
        if(a == 0)
            return 1;
        if(b == 0)
            return -1;
        return a - b;
    });

    document.write(myPriceList.join(‘ ‘))

    this sorts “0” at the bottom. problem is, that i have no idea where to put this code in your plugin.

    Thread Starter KingShinobi

    (@kingshinobi)

    oh there is a thread on datatables forum, but i don’t know where i should put the code. can you point me in the right direction?

    https://datatables.net/forums/discussion/4025/sorting-to-ignore-empty-cells/p1

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    that first piece of code (with the prices) is not that useful, as it is not related to DataTables.
    The code in the DataTables is much better ?? The best option should be the code from fbas’s first reply (the first block of code, which is 13 lines).

    I recommend that you add that to the “TablePress DataTables Sorting Plugins Extension” from https://tablepress.org/extensions/datatables-sorting-plugins/ (on both JavaScript files), and then manually apply that format to your table with an extra “Custom Command” (which is the modern version of fbas’s second block of code):

    "aoColumnDefs": [ { "sType": "mystring", "aTargets": [ 0 ] } ]

    (Change the 0 to the index of your column, counting start at 0!)

    Regards,
    Tobias

    Thread Starter KingShinobi

    (@kingshinobi)

    almost there. i took the code and put it into the plugins file and added the custom command as well.

    the sorting works now pretty random?! on the first column it doesn’t sort at all anymore. on the other columns it sorts until the first empty cell. it doesn’t move the empty cell to the bottom, but starts sorting after it again. i also tried fbas’s second version, but the outcome is pretty much the same. i cannot really see a pattern.

    when i delete the custom command, it’s the same, except now the first column can be sorted again.

    all my cells are integers. could this be the problem?

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    that seems to indicate that there’s something wrong somewhere in the JS.

    Can you please post the link to the page with the table again. I know that you had posted it somewhere already, but I can’t find it…

    Regards,
    Tobias

    Thread Starter KingShinobi

    (@kingshinobi)

    https://datatables.net/forums/discussion/4025/sorting-to-ignore-empty-cells/p1

    this one? yes i also now understand that the changes in the sorting behaviour are coming from the plugin and not the new sorting function.

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    no, I actually meant a link to the page with the table that is now not sorting correctly.

    And you might be right: The other new sorting functions could be influencing this. Thus, please try removing everything that was in that file, and only put in the code from the DataTables forums.

    Regards,
    Tobias

    Thread Starter KingShinobi

    (@kingshinobi)

    oh ok ??

    i never posted a link, that’s why you can’t find it ??

    https://www.apl-online.at/paintballbundesliga.at/tabellen/

    the page is in development and just dummy data. colum 0 and 1 should be sortable. i tried removing all the code and just put in the new sorting algorithm. version 1 and 2 from fbas. the result is, that sorting is not possible anymore. at least without the other code from the plugin, the weird behaviour is gone.

    your functions look a bit different (brackets, etc). could this be the reason for the problem?

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    thanks for the link. Yes, as you never posted one, that could be the reason why I couldn’t find it ??

    The sorting plugins Extension is not activated right now, is it? Can you please activate it?
    It should have the first block of code from fbas’s first post in it, and only that.
    Then, the “Custom Command” that you entered is not what I posted above: The “sType” value should be “mystring” and not “mystring-asc”.

    Regards,
    Tobias

    Thread Starter KingShinobi

    (@kingshinobi)

    all done. i was testing a bit with the custom command… result is still the same.

    Thread Starter KingShinobi

    (@kingshinobi)

    oh, now at least the sorting is active again although it sorts totally normal. should i try the second code from fbas which he posted later?

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    I have a feeling that the fbas’s code is somehow switched. Please try again with

    jQuery.fn.dataTableExt.oSort['mystring-asc'] = function(x,y) {
    	if (x==y) return 0;
    	if (x == "") return 1;
    	if (y == "") return -1;
    	if (x > y) return 1;
    }
    jQuery.fn.dataTableExt.oSort['mystring-desc'] = function(y,x) {
    	if (x==y) return 0;
    	if (x == "") return 1;
     	if (y == "") return -1;
    	if (x > y) return 1;
     }

    Regards,
    Tobias

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘ignore empty cells when sorting asc’ is closed to new replies.