fnAddData - Prepend?

fnAddData - Prepend?

DaveomaniaDaveomania Posts: 15Questions: 0Answers: 0
edited January 2012 in General
Hello!

I am using the fnAddData call as follows:

[code]oTable.fnAddData(["SomeData", "SomeMoreData"]);[/code]

In a datatable which I set up as follows:

[code]oTable = $("[ID$=gvComments]").dataTable({
"sPaginationType": "full_numbers",
"iDisplayLength": 5,
"bFilter": false,
"bInfo": false,
"bLengthChange": false,
"aaSorting": []
});[/code]

I am wondering, how I can use this method and have my data prepended to my table instead of always on the last page, last row?

Thank you very much!

Replies

  • allanallan Posts: 63,538Questions: 1Answers: 10,476 Site admin
    The position of the new row is determined by the sorting applied to the table. If the data in the row tells DataTables to have the new row appear in the first position in the table due to the current sort, then it will - equally if the data says that it will position at the end of the table then it will.

    So the "fix" is to ensure the ordering is what you want in terms of your data.

    Allan
  • DaveomaniaDaveomania Posts: 15Questions: 0Answers: 0
    Perfect Allan.

    The data is being sorted when pulled form the db and then dynamically built into a table prior to dataTable being applied.. but I think if I just add a hidden field with the appropriate data to sort in each column, I should be able to make this work!

    I appreciate the response! :)
  • allanallan Posts: 63,538Questions: 1Answers: 10,476 Site admin
    The aaSortingFixed parameter might be of some use / interest here - it will force an initial sort, before doing the user defined sort.

    Allan
  • DaveomaniaDaveomania Posts: 15Questions: 0Answers: 0
    Hi Allan :)

    Thanks for the recent suggestion. It appears as though the standard sorting is working just fine. However I have come across a new issue which might just be on my part and not knowing the API fully.

    When I dynamically add a row to the table via fnAddDate I then use fnDraw to re-initialize the table. This works perfectly. I also have functionality in the table to delete rows using fnDeleteRow(tr) where tr is the current row. This also works perfectly.

    The problem arises when I use fnAddData, and then use fnDeleteRow on the newly added row. At this point, fnDeleteRow will remove the next row.

    Any thoughts? Was my post confusing? lol
  • DaveomaniaDaveomania Posts: 15Questions: 0Answers: 0
    Thought I would add in, that when I check the current table row index for the newly added row, I get 0 when it's the first row.. which is correct.. but passing 0 to fnDeleteRow deletes the last row 0.
  • DaveomaniaDaveomania Posts: 15Questions: 0Answers: 0
    edited January 2012
    Well I have found out that when removing a row by node, ie: TR you need to use the get method.

    Example: [code]var tr = $(this).closest('tr').get(0);
    oTable.fnDeleteRow(tr);[/code]

    That solved my issue with the wrong node being removed in the mark-up. :)
  • allanallan Posts: 63,538Questions: 1Answers: 10,476 Site admin
    Or you can use:

    [code]
    var tr = $(this).closest('tr')[0];
    oTable.fnDeleteRow(tr);
    [/code]

    fnDeleteRow expects a node, not a jQuery object.

    This is on the radar for change in DataTables 1.10 or 1.11 when I get there :-)

    Allan
  • DaveomaniaDaveomania Posts: 15Questions: 0Answers: 0
    Even better. Thanks a lot for checking back and giving more insight Allan! :)
This discussion has been closed.