How do I define a secondary column to sort on per each column?

How do I define a secondary column to sort on per each column?

KennethAKennethA Posts: 3Questions: 0Answers: 0
edited August 2011 in General
I'm having trouble to understand how to define a secondary column to sort on for each column. I have successfully added a secondary column for the initial sort when the table is loaded, but when the user resorts the table by cklicking the table column, it will only sort on that column and not any secondary column. How to define it?

I would like to define it per column level like, when column 0 is clicked, secondary would be column 5, column 1 clicked - sort secondary on column 2 etc etc... Is that even possible?

If not - at least tell me how to get a permanent secondary column to sort on...
Thank You!

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    I don't think DataTables has an automatic way of doing this (there is an iDataSort value for columns that lets you sort by one other column when clicking a column, but not a set)

    but you can do this by catching the click and calling fnSort yourself with an array of sort directions
    [code]
    var oTable;

    $(document).ready(function() {
    oTable = $('#example').dataTable();

    /* Sort immediately with columns 0 and 1 */
    oTable.fnSort( [ [0,'asc'], [1,'asc'] ] );
    } );
    [/code]

    http://www.datatables.net/ref#fnSort
  • KennethAKennethA Posts: 3Questions: 0Answers: 0
    Ok, thank You. I'll give it a try.... Next step is to figure out how to know which column has been clicked and in what order to sort...

    I'll then need to do something like:

    if (oTable.selectedColumnIndex == 1) { oTable.fnSort([ [1,'asc'],[5,'asc']]);}

    it's the part with "selectedColumnIndex" I have to figure out now...
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    exactly. I can't think of how to do it off the top of my head either. I'll keep it in mind and let you know if I think of something.
  • andrewsharpe79andrewsharpe79 Posts: 4Questions: 0Answers: 0
    I'm also interested in the multiple iDataSort values option, and my use case is I have two separate columns (hidden) that I combine to make a single column (displayed), and when sorting on the displayed column I want to use both underlying columns.

    Here's a patch (to 1.8.1) that I believe allows the desired behaviour

    [code]
    --- DataTables/js/jquery.dataTables.js 2011-07-22 09:08:44.000000000 +1000
    +++ DataTables/js/jquery.dataTables_as.js 2011-08-24 12:00:01.000000000 +1000
    @@ -4640,15 +4640,24 @@
    for ( i=0 ; i
  • andrewsharpe79andrewsharpe79 Posts: 4Questions: 0Answers: 0
    edited August 2011
    To use the new functionality with the patch just declare iDataSort as an array of the target column indexes
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Very nice - like it! How do you deal with things like clicking on the column you are currently sorting by to reverse the sorting though? There might also be a case for mixed direction sorting, although that sounds particularly complicated, and I suspect confusing to the end user...

    Allan
  • andrewsharpe79andrewsharpe79 Posts: 4Questions: 0Answers: 0
    WRT reverse sorting, I hadn't dug that deep but a quick look makes me think it'll already be handled in fnInnerSorting in _fnSortAttachListener (I could be wrong though).

    The concept is that the one visible column is only sortable as a whole, ie. you can't sort by only one of it's backing columns, you're always sorting on both backing columns. That should alleviate any confusion from the end user because the behaviour will be consistent (as per the developers definition).

    As you noted this patch doesn't handle opposite direction sorting for the multiple iDataSort columns (my use case was based on start/end dates, so same direction is all I needed), though it could be handled by making iDataSort an array of arrays similar to aaSorting. Arguments for this include making the interface consistent with aaSorting and the obvious added flexilibility. Arguments against are the small number of uses and the amount of work required (would probably need to poke around in fnInnerSorting to get that going, I just don't have that itch right now).

    I've created a discussion in the feature request category so perhaps we could continue there? http://www.datatables.net/forums/discussion/6326/multi-valued-idatasort
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    edited November 2011
    An old conversation now I know, but I've just been working on this (the old to-do list does get worked through eventually :-) ) and have decided to introduce a new initialisation parameter for columns called aDataSort. This works in almost exactly the way iDataSort does (which is still present in DataTables, although aDataSort will get priority if it is defined as well), but in the case of aDataSort you can define an array of column indexes. For example:

    [code]
    $(document).ready(function() {
    $('#example').dataTable( {
    "aoColumnDefs": [ {
    "aDataSort": [ 0, 1 ],
    "aTargets": [ 0 ]
    } ]
    });
    } );
    [/code]

    In this code the first column will sort on column index 0 first and then column index 1 secondly (if needed).

    In terms of sorting direction both columns will be sorted in the same direction (i.e. either ascending or descending). If you want to define a different sorting direction you would need to do an override such as that described above to use fnSort manually, but I think that the implemented method will cover the majority of cases.

    All thoughts on this are most welcome, as always!

    This is now available in the 1.8.3.dev build (although that might become 1.9) available on the downloads page if anyone wants to try it. Note thought that it is currently incompatible with ColReorder. All other plug-ins should work okay.

    Regards,
    Allan
  • KennethAKennethA Posts: 3Questions: 0Answers: 0
    Perfect! I will definitly download the new version and try it out. I'm not currently using ColReorder (as far as I know) so I don't think that's an issue for me, but good to know.
    Thanks!
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Fantastic - let me know how you get on with the new version :-)

    Allan
This discussion has been closed.