oSort function enhancement

oSort function enhancement

mimicmimic Posts: 43Questions: 0Answers: 0
edited November 2009 in General
Could you pass settings (and with it data) object to oSort function? I am doing some column data dependent sorting and I would like to define generic oSort function but I cannot as I do not have access to all column data from function.

What I am really trying to achieve is to keep row groups as in http://datatables.net/examples/advanced_init/row_grouping.html fixed as they have been provided in original table. And this seems hard to achieve. (Currently I store original order and then use a comparison function which does something like "indexInOriginalOrderOf(a) - indexInOriginalOrderOf(b)" for groups' names.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi mimic,

    No, I'm afraid it is not possible to pass the settings object into oSort, since this is a basic Javascript Array.sort(a,b) function ( http://www.w3schools.com/jsref/jsref_sort.asp ).

    It might be possible to achieve what you are looking for using "Custom data source sorting" ( http://datatables.net/development/sorting ), having said that, have you considered using aaSortingFixed ( http://datatables.net/usage/options#aaSortingFixed ) to keep the sorting "fixed". This is exactly what this parameter is for :-)

    Regards,
    Allan
  • mimicmimic Posts: 43Questions: 0Answers: 0
    Yes, currently I store original order of elements and then I use custom sorting function to compare it in stored array:

    [code]
    jQuery.fn.dataTableExt.oSort['group-sort-asc'] = function (a, b) {
    return compare($.inArray(a, allGroups), $.inArray(b, allGroups));
    };
    [/code]

    I use also aaSortingFixed to keep it fixed. So I have to use both aaSortingFixed and custom sorting function just to keep a column sorted in initial order. Quite a lot for that.

    But the main problem is that if I have multiple tables on a page my approach could fail as there could be same values in multiple tables so sorting would fail (currently I make a global list of all fixed values for all tables in allGroups). The solution would be to define custom sorting function for every table, but this is really a nuisance if you want to automatize.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi mimic,

    I'm afraid I'm not sure I understand the issue. In my demo that you linked to you can see that the grouping (which is based on the first column in the original HTML table) is always given the priority when sorting due to the use of aaSortingFixed. There should be no need to do $.() never mind $.inArray() in a sort() handler, which will be very expensive.

    Is this not adequate for your needs?

    Regards,
    Allan
  • mimicmimic Posts: 43Questions: 0Answers: 0
    No, sadly not. The problem is that I need groups/first column in fixed order based on initial order (of initial table) which is not in any particular well-defined order but based on a predefined order of groups defined in a server program which outputs this table (where this order is based on a meaning of groups, not names, values or something "computable"). The other problem is that this has to be generic (so that I can apply DataTables to all tables on the site) so ti ts not possible to hardcode this order for one particular table (in an easy way).

    So the best would be if I could "fix" column to be as it was initially, not just fixed to numeric or alphabetical or some other order.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Oh I see... Sorry I didn't realise that there was no "logical" order to the table. Would it be possible to add a logical order using something like the hidden sorting plug-in (you would have etc in the HTML and sort on the title attribute).

    Is currently is no method to "fix" a column, but have that as the primary "sorted" column - interesting idea - if a bit of a nightmare to implement I would guess :-)

    Regards,
    Allan
  • mimicmimic Posts: 43Questions: 0Answers: 0
    Hm, maybe not. You could add option to "really fix order" which would simply add a dummy column to data which would have as values initial row indexes. And then you would use current "fix order" functionality on that.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi mimic,

    "Really fix"ing the order is done using aaSortingFixed ( http://datatables.net/usage/options#aaSortingFixed ) as mentioned in my first post. This will ensure that a particular column (hidden or otherwise) is always sorted on first - this in combination with for dummy data will ensure that your order remains as it was when the page was loads. However, as noted you can't have a column in fixed order, when it is not sorted.

    Regards,
    Allan
  • hkippohkippo Posts: 1Questions: 0Answers: 0
    I had similar problem (If I understood correctly) and solved it by accessing internal variables directly:

    [code]
    oTable.dataTableSettings[0].aaSortingFixed = ...
    oTable.fnSort( [ [0,'asc'] ] );
    [/code]

    When modifying that parameter directly, it seemed that calling fnSort was required to perform the sort operation.

    I used this for switching grouping on/off. Solution could be prettier.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Yup - I think setting the fixed sorting array is a candidate for a plug-in API function.... :-)

    Allan
  • mimicmimic Posts: 43Questions: 0Answers: 0
    hkippo, I am not really sure if this does the trick. I do not have any "logical" order on the column I would like to group on. Or maybe I just do not understand your solution.
This discussion has been closed.