[Patch] fnChooseInitialSort

[Patch] fnChooseInitialSort

igotimacigotimac Posts: 4Questions: 0Answers: 0
edited September 2009 in General
Hi,

We had the specific need that certain columns be sorted descending when you first click them and then ascending on the second click (as opposed to the current behavior of ascending on first click and descending on second).

we have things like
[code]

fnChooseInitialSort: function(colIndex){
return (colIndex < 3) ? "asc" : "desc";
},

[/code]
in our datable instantiations

So here's a little patch I thought you might be interested in:

[code]

@@ -2254,6 +2254,12 @@
* too early), so we on;y do it when we absolutely have to.
*/
var fnInnerSorting = function () {
+ var fnDefaultSortDetermine = function(colIndex, currentSort){
+ var fn = oSettings.fnChooseInitialSort;
+ var initialSort = fn ? fn(colIndex) : "asc";
+ var alternateSort = (initialSort == "desc") ? "asc" : "desc";
+ return currentSort==initialSort ? alternateSort : initialSort;
+ };
if ( e.shiftKey )
{
/* If the shift key is pressed then we are multipe column sorting */
@@ -2277,7 +2283,7 @@

if ( bFound === false )
{
- oSettings.aaSorting.push( [ iDataIndex, "asc" ] );
+ oSettings.aaSorting.push( [ iDataIndex, fnDefaultSortDetermine(iDataIndex, null) ] );
}
}
else
@@ -2285,12 +2291,12 @@
/* If no shift key then single column sort */
if ( oSettings.aaSorting.length == 1 && oSettings.aaSorting[0][0] == iDataIndex )
{
- oSettings.aaSorting[0][1] = oSettings.aaSorting[0][1]=="asc" ? "desc" : "asc";
+ oSettings.aaSorting[0][1] = fnDefaultSortDetermine(iDataIndex, oSettings.aaSorting[0][1]);
}
else
{
oSettings.aaSorting.splice( 0, oSettings.aaSorting.length );
- oSettings.aaSorting.push( [ iDataIndex, "asc" ] );
+ oSettings.aaSorting.push( [ iDataIndex, fnDefaultSortDetermine(iDataIndex, null) ] );
}
}

@@ -4380,6 +4386,7 @@
_fnMap( oSettings, oInit, "fnDrawCallback" );
_fnMap( oSettings, oInit, "fnInitComplete" );
_fnMap( oSettings, oInit, "fnServerData" );
+ _fnMap( oSettings, oInit, "fnChooseInitialSort" );
_fnMap( oSettings, oInit, "aaSorting" );
_fnMap( oSettings, oInit, "aaSortingFixed" );
_fnMap( oSettings, oInit, "sPaginationType" );

[/code]

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi igotimac,

    Thanks very much for the patch - very interesting indeed! Another option for doing this would be to make the sorting functions 'flip', so you could change the sType for the column and have the descending sort function actually return an ascending result. You can also override the built-in sorting functions to match the behaviour that you want if it is specific to a particular column type.

    Regards,
    Allan
  • igotimacigotimac Posts: 4Questions: 0Answers: 0
    Hey Allan,

    Glad you like it. My co-worker also suggested the sort function override approach... but I wanted the little arrows that show which way the thing is sorted to be correct.

    Jacob
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi Jacob,

    Good point - I hadn't actually thought about the little icons. I'll have a look at the performance impact on sorting and hopefully include this into a future version of DataTables as it's quite a nice feature to have in the core! Nice one, and thanks for taking the time to share with us :-)

    Regards,
    Allan
  • igotimacigotimac Posts: 4Questions: 0Answers: 0
    Cool, It's great to have a project maintainer so open to contributions. I'll be sure to let you know about other patches we make. I look forward to the folding of further features into future versions.

    Jacob
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi Jacob,

    All patches are welcome! The idea of DataTables is to make it as useful as possible, and generalising cases like this are a particularly good way of going about things.

    Normally if there is a plug-in method for adding the functionality I would tend to go for that approach (in an effort to keep the DataTables core as light as possible), which is why I thought about the sorting plug-ins initially. However in this case it would remove an assumption that I made that sorting would be performed ascending first!

    I wonder if it might be an idea to has a new aoColumns parameter ("sDefaultSorting" perhaps?) to give the sorting direction for each column, rather than using a function. Was there a specific reason you used a function for this (other than to reduce the number of lines of code with the <3 ;-) )?

    Regards,
    Allan
  • igotimacigotimac Posts: 4Questions: 0Answers: 0
    Allan,

    Yea I suppose an aoColumns parameter might be a more consistent way to represent the concern. I guess I'm still a relative newbie to the data tables source. I was thinking that a function would give me more flexibility, but given the way we're using it, sDefaultSorting is probably cleaner. I'll look into a refactor if I have time later.

    Jacob
This discussion has been closed.