FnDeleteRow() using ID

FnDeleteRow() using ID

grundegrunde Posts: 8Questions: 0Answers: 0
edited January 2014 in DataTables 1.9
This forum is AWESOME and I learn so much about DataTables reading through it.

I am using Datatables Version: 1.9.3 and trying to get the fnDeleteRow logic to work appropriately to delete a row by a table's row "id" attribute.

The line that I have been using that works appropriatey SOME of the time is as follows.
[code]oDataTable.fnDeleteRow($('#' + sTestTrackIDs)[0]);[/code]
where sTestTrackIDs is the table's row () ID.

This will work most of the time but I receive the error stating "'nTR' is null or not an object" once I believe to delete the first row of the table.

I have a good idea that the error is caused from deleteing the first row of the table and then the index internally to datatables is being corrupted.
There shouldn't be over a 100 rows on the table so I would be happy to loop through the table to search for the ID and then delete it appropriately but am struggling with how to do that.

Thanks in advance for any help on this!!!

--Gary

Replies

  • grundegrunde Posts: 8Questions: 0Answers: 0
    Thoughts on this from anyone??

    Could someone please assist me with looping through a DataTable to delete the appropriate row with a given ID?
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Sorry - it looks like my reply was saved as a draft rather than being posted...

    Can you link to a test case showing the problem please. I'm not aware of any issues regarding corruption of the index - but it would be worth updating to 1.9.4.

    Your code to do the delete looks to be correct, as long as the row is in the document (i.e. it is shown on the current page in the table).

    A more complete option might be:

    [code]
    oDataTable.fnDeleteRow( oDataTable$('#' + sTestTrackIDs)[0] );
    [/code]

    which uses DataTables `$` method to perform a jQuery selector on all rows, regardless of paging.

    Allan
  • grundegrunde Posts: 8Questions: 0Answers: 0
    Allan,

    I have confirmed that we are using DataTables v.1.9.4.
    I've made the change to use the Datatables object in the fnDeleteRow() function but I still can reproduce the same error easily. Removing anything but the FIRST row works perfectly, however once I remove the first row I get the error:
    Line: 39, Char: 129: Error: 'nTr' is null or not an object
    I am using: jquery.dataTables.min.js with a date of "9/23/2013 13:27"

    Unfortunately, I can't link to a test case because this is in our internal system that cannot be accessed outside of our company (hospital).

    Any thoughts/ideas on what I could try to get past this? I'd be happy to loop through the table rows if needed.

    [code]
    var oTTDataTable = $("#test_tracking_data_table").dataTable();
    oTTDataTable.fnDeleteRow( oTTDataTable.$('#' + sTestTrackIDs)[0] );
    [/code]
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Are you using server-side processing or something?

    Can you show me the loop you are using to delete the row? It might be worth trying to pass in the second parameter to fnDeleteRow as `false` . Although that might hide a bigger problem (my question about server-side processing).

    Could you also try jquery.dataTables.js (the non-min file) and tell me the line number of the error?

    Allan
  • grundegrunde Posts: 8Questions: 0Answers: 0
    I'm not using a loop to delete the row nor server side processing. Just normal HTML + javascript.

    The line number that produces the error for me non-min version is: Line: 1449, Char: 6.....
    Looks like it shows as this line in the file I have:

    [code]
    function _fnDraw( oSettings )
    ....
    ....
    ....
    for ( var j=iStart ; j>>>>>>> if ( aoData.nTr === null )
    {
    _fnCreateTr( oSettings, oSettings.aiDisplay[j] );
    }
    [/code]
    ....
    ....
    ....
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    That's very odd. I've never seen that error crop up there I don't think.

    > Removing anything but the FIRST row works perfectly, however once I remove the first row I get the error:

    Sorry - that's what made me think it was in a loop.

    Are you able to PM me link to the page? I'm afraid I'm out of ideas with the information here.

    Allan
  • grundegrunde Posts: 8Questions: 0Answers: 0
    Sorry for the confusion on the loop. I actually have a cell containing a delete icon on each row where I allow the user to delete the row. From the cell I call a function which accepts the row's ID attribute and then make the call to fnDeleteRow().

    This works great as I click through the different row's delete icon as long as I don't select the delete icon in the first row.

    Once I click the first row's delete icon, i get the error.

    Sorry, I can't share the page due to it's in our internal domain.
  • grundegrunde Posts: 8Questions: 0Answers: 0
    Allan,

    Is there a way that I can use fnDeleteRow another way (not pass in the row's ID).....I'm hoping I can use a function inside DataTables to get ALL Rows, determine the index of the row I want to delete and then call fnDeleteRow() to delete it.

    Isn't this what fnGetPosition is used for??

    Thanks in advance for any suggestions!!

    --Gary
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Yes, fnGetPosition is used to get the internal index, but you can only pass it a row node, which can you can pass fnDeleteRow anyway, so it wouldn't really offer any benefit - just extra lines of code!

    Are you able to use DataTables 1.10? It provides an entirely new API that might be easier to use in this case (it is designed to be!). For example:

    [code]
    var table = $('#myTable').DataTable(); // note the capital D to get the new API

    table.row( '#myId' ).remove().draw();
    [/code]

    No need to work with nodes or paging or anything like that - the `row()` selector will take a jQuery selector and work out what row is wanted.

    If you want the data of all rows you could use `rows().data()` . If you wanted to loop over the rows in the table and delete based on data:

    [code]
    table.rows().flatten().each( function (idx, i) {
    var d = table.row( idx ).data();

    if ( d.whatever === 1 ) {
    table.row( idx ).delete();
    }
    } );

    table.draw();
    [/code]

    That's all possible in 1.9- as well, using fnGetData etc - its just not as neat! Plus, I'm only guessing as to what you are trying to do :-)

    Allan
  • grundegrunde Posts: 8Questions: 0Answers: 0
    As an FYI to everyone regarding this issue, my problem was that I was using a pipe in the Row's ID....This causes very strange behavior where it works some of the time and not others.

    Lesson learned: DO NOT use PIPE's in javascript ID field.............

    http://stackoverflow.com/questions/12306252/jquery-selector-doesnt-accept-pipe-character
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Very interesting! Thanks for letting us know about that. I'll try to remember that one!

    Allan
This discussion has been closed.