oSort function enhancement
oSort function enhancement
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.
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.
This discussion has been closed.
Replies
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
[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.
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
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.
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
"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
[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.
Allan