Row Grouping - fnAddData to previous gruop

Row Grouping - fnAddData to previous gruop

KyprusKyprus Posts: 7Questions: 0Answers: 0
edited January 2010 in General
Hi,

I was looking for a way to group rows and found your example in the docs. It works great if I add rows linearly but If I have to add a row to a group (using fnAddData) already created it creates a new group. Looking at the code I can why, but what changes would I have to make for this functionality?

Thanks

Replies

  • allanallan Posts: 63,700Questions: 1Answers: 10,501 Site admin
    The code that is in the example ( http://datatables.net/examples/advanced_init/row_grouping.html ) should cope okay with adding a new row - as long at the text which identifies which group the row is in is _identical_ to that in the rows for the group which is already there. fnAddData will redraw the table, so this shouldn't be a problem (as it calls fnDrawCallback). Perhaps one option to check this is to console.dir( oSettings.aoData ) in the callback function and check that everything is as expected.

    If that doesn't help perhaps you can post an example showing the problem.

    Allan
  • KyprusKyprus Posts: 7Questions: 0Answers: 0
    Hi Allan,

    I don't think I was clear in my first post, sorry. Here's what I'm doing.

    1) I start with a blank table.

    2) fnAddData with a group identifier of group1 - creates a new group like it should

    3) fnAddData with a group id of group1 - adds it to existing group correctly

    4) fnAddData with a group id of group2 - correctly creates a new group and adds it to it

    5) fnAddData with a group id of group1 again - it will create another group1 rather than adding to the original group1


    It looks to me like this is because when you check to see if you should add a new group "if ( sGroup != sLastGroup )" you are only checking against the last group creation and not any before. I hope this clarifies.

    Thanks
  • allanallan Posts: 63,700Questions: 1Answers: 10,501 Site admin
    Oh I see - yes, my code assumes that there is fixed sorting on the group identifier - so that they are always together - hence I get away with that. Do you have sorting set up in a similar manner? aaFixedSorting might help.

    Regards,
    Allan
  • KyprusKyprus Posts: 7Questions: 0Answers: 0
    I have sorting off but adding either sorting option doesn't seem to affect a fnAddData().
  • allanallan Posts: 63,700Questions: 1Answers: 10,501 Site admin
    When fnAddData is called (and it's allowed to redraw), then it will also perform sorting. So whatever the currently selected sorting columns are, these will be sorted on. If you wanted you could disable sorting on each individual column, and only use aaFixedSorting. You need to have bSort:true (or in it's default state) in order for sorting to work...

    Regards,
    Allan
  • KyprusKyprus Posts: 7Questions: 0Answers: 0
    edited February 2010
    Ok got it all to work, thanks Allan!

    Using the code from the example I set bSort to true, used aaFixedSorting on the group column, and turned of sorting on all other columns. Works perfectly now.

    Also I think there's a small error in that code. You have "var i = 0;" above the for loop, thats not needed right?

    Thanks again,
    Kyprus
  • allanallan Posts: 63,700Questions: 1Answers: 10,501 Site admin
    Hi Kyprus,

    Good to hear you got it "sorted" :-). Thanks for picking up my little typo in the demo code as well - I've corrected that now.

    Regards,
    Allan
This discussion has been closed.