delete rows based on the id

delete rows based on the id

obet.tea46obet.tea46 Posts: 6Questions: 0Answers: 0
edited February 2010 in General
i want to delete rows based on the id in select multiple option.

example:

in select multiple option i have value or id: a1,b2,c3, and d4.

after select b2 and b3 in select multiple option then i hit a remove button,

row b2 and c3 in dataTables removed.

please help..

thanks for the answer.

Replies

  • cloud19cloud19 Posts: 6Questions: 0Answers: 0
    a also have the same problem...

    i'm hoping allan knows about this...

    still awaiting for response..
  • allanallan Posts: 63,538Questions: 1Answers: 10,476 Site admin
    If you know the id of the TR element, then it's as simple as:

    [code]
    oTable.fnDeleteRow( document.getElementById( 'a1' ) );
    [/code]
    ( http://datatables.net/api#fnDeleteRow )

    This assumes that the row is in the visible table. If not you could use a slightly slower method:

    [code]
    oTable.fnDeleteRow( $('#a1', oTable.fnGetNodes()) );
    [/code]

    Allan
  • cloud19cloud19 Posts: 6Questions: 0Answers: 0
    hi allan,

    Thanks for the reply..I have a concern though...

    when I am using oTable.fnGetNodes() and I'm passing in a String with value "my string"

    and when I am showing it in alert(sData), I am only getting "my"..

    is this a bug? I want to use my own implementation of delete on the server side that's why I am checking on this fnGetNodes().
  • obet.tea46obet.tea46 Posts: 6Questions: 0Answers: 0
    hi allan,

    i use the alternate way, i change the select multiple option id to increment number.

    cause your dataTable doesn't use index to delete rows, but using the increment number. is it true?

    anyway thank you very much for the reply..
  • allanallan Posts: 63,538Questions: 1Answers: 10,476 Site admin
    @cloud19:
    >> when I am using oTable.fnGetNodes() and I'm passing in a String with value "my string"

    fnGetNodes will take only one (optional) parameter - which must be an integer, not a string - http://datatables.net/api#fGetNodes - so I'm not quite sure what you mean here. Are you using server-side processing? If so the way to delete a row, is to not do any DataTables stuff until you've actually deleted the row from the database! So the 'delete' action is actually to send an XHR to the server to delete the row and then refresh the table (using fnDraw) based on the new data on the server.

    @obet.tea46:
    DataTables has an internal array called aoData which is uses to store information about each row. This is the 'index' that I tend to refer to in DataTables, and what fnGetPosition will give you. It's also what fnDeleteRow etc deal with (since the ordering can be changed by the end user). With v1.6 this has been made a lot easier as you can pass in TR elements and DataTables will calculate the index to use itself. Hope this explains it!

    Regards,
    Allan
  • obet.tea46obet.tea46 Posts: 6Questions: 0Answers: 0
    edited March 2010
    Allan,

    thankssss a lot ..^_^
  • zadradesignzadradesign Posts: 2Questions: 0Answers: 0
    edited January 2012
    I apologize for resurrecting an old thread, but it is identical to my problem...

    I need to delete a row from a table by its ID. I have tired both examples shown above, and neither are working. As an example:

    [code]
    var oldRow = document.getElementById('rowId');
    $('#curClasses').dataTable().fnDeleteRow(oldRow);
    [/code]

    In the browser, the script simply stops. Firebug is returning the error "a is null" in jquery.dataTables.min.js. If I print the value of oldRow to the screen, it is properly returning [object HTMLTableRowElement].

    Any help will be appreciated.
  • allanallan Posts: 63,538Questions: 1Answers: 10,476 Site admin
    Are you sure that 'rowId' is visible in the document. DataTables removes rows which are not needed in the current view - hence you might get null. You would need to use fnGetNodes or in 1.9 you can use $, for example:

    [code]
    var table = $('#curClasses').dataTable();
    table.fnDeleteRow( table.$('#rowId')[0] );
    [/code]

    Allan
This discussion has been closed.