Problematic sorting requirement

Problematic sorting requirement

syndesissyndesis Posts: 12Questions: 0Answers: 0
edited November 2013 in DataTables 1.9
Debug: ahujef
Live: http://live.datatables.net/opubud

First off, thanks for creating and maintaining this software. I've used it a lot and always found it very helpful.

This code reflects several problems I had to solve in order to meet my requirements. I don't think all the functionality here affects the problem I'm currently facing, but I found 90% solutions to a lot of these things in these forums and spent a fair amount of time on the last 10% so maybe the code here can save someone else some time. Features implemented: drag and drop between tables with differing numbers of columns (preserving custom row IDs and classes), expandable / collapsible duplicate rows using filters, reset table button that transfers all data (preserving custom row IDs and classes) to another table.

Description: I have two datatables which are connected as jqueryui sortables so I can drag and drop rows between them. In both tables, rows need to be sorted by priority. In the left table, "duplicate" rows (defined as having identical Description columns) need to be grouped into an expandable/collapsible set. Re-sorting does not need to be allowed.

Problem: Difficult sorting requirements. Duplicate rows (there may be multiple) may have different priorities from their "main" row. If I expand the main row, the duplicate(s) show up somewhere else in the table according to their priority. In the example, rows with description "BBB." When they have the same priority like "AAA" they show up how I want them. If I do aaSortingFixed on Description, the rows are always grouped properly but they won't show up in proper priority order (high to low). I'm trying to figure out the best way to resolve this so that duplicates always show up directly under their main row while still being able to display all rows based on the priority of the main rows.

Thanks!

Replies

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    Just so I'm completely clear, when you say you expand the main row, is this inserting TR elements into the table's body? Are you using fnOpen or your own method for doing that?

    Either way, DataTables doesn't do anything while such child rows, and thus won't sort them. If sorting is required on child rows, then I'm afraid that is something that needs to be done externally.

    Regards,
    Allan
  • syndesissyndesis Posts: 12Questions: 0Answers: 0
    When I expand the main row, it just does a filter to show the duplicate rows. I search for the main rows which are expanded (based on their class) and return true from afnFiltering for the duplicate rows which also have that class. So those rows aren't technically children and are sorted based on the current sorting method.
  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    I see - so they are actually part of the table proper. That's nice and cunning - like it :-)

    Is there any way you can show me the page? I'm struggling to visualise it a little I'm afraid.

    Allan
  • syndesissyndesis Posts: 12Questions: 0Answers: 0
    Sorry, I guess my links in the OP got lost in the wall of text :)

    Debug: ahujef
    Live: http://live.datatables.net/opubud
  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    Doh - sorry I missed that!

    So am I correct in saying that the issue is that if you click the top arrow (row 3) its child row appears below the row 6?

    As you say that's because of the ordering. The ordering being applied is the priority first, then the grouping (description), hence why it is out of order.

    To keep the grouping, you would need to have it sort but he group first - but that causes the priority order to explode. Is that fundamentally the issue (sorry, I'm being a bit slow on the uptake today!)?

    I'v got to confess, I'm not sure how to handle this in DataTables. Let me think about it a little bit and get back to you!

    Allan
  • syndesissyndesis Posts: 12Questions: 0Answers: 0
    Yes, that is precisely the issue. Thanks for looking into it!
  • syndesissyndesis Posts: 12Questions: 0Answers: 0
    I gave up trying to get this working in the clever and cunning fashion and used fnOpen instead. In doing so, I believe I discovered a bug in the fnAddTr plugin. Line 35 should be:

    [code]
    oSettings.aoData[iIndex]._anHidden[aInvisible[i]] = nTds[aInvisible[i]];
    [/code]

    I also wrote a corresponding fnGetTr plugin based on fnGetTds

    [code]
    /* plugin to retrieve TR object including invisible columns */
    $.fn.dataTableExt.oApi.fnGetTr = function ( oSettings, mTr ) {
    var cols = oSettings.aoColumns.length;

    var iRow = (typeof mTr == 'object') ?
    oSettings.oApi._fnNodeToDataIndex(oSettings, mTr) : mTr;
    var nTr = oSettings.aoData[iRow].nTr;

    var hiddenCells = oSettings.aoData[iRow]._anHidden;
    for ( iColumn=0, iColumns=oSettings.aoColumns.length ; iColumn
  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    Thanks for picking that up. The new API in 1.10 will be able to add TR elements directly, negating the need for that plug-in :-).

    I've not thought of a cunning way to meet your sorting requirements yet, but I have been thinking about it. I think one way or another its going to require some new code in DataTables to do it, but I'm not yet sure what form that code should take! I am continuing to think about it (sorry I'm going a bit slow at the moment :-) ).

    Allan
This discussion has been closed.