fnDeleteRow +DataTables object retrieval + server side= not deleting?

fnDeleteRow +DataTables object retrieval + server side= not deleting?

rob_deanrob_dean Posts: 4Questions: 0Answers: 0
edited October 2010 in General
Hi Allan,
Thanks for DataTables! It is great!
I am trying to delete a row with the following code. Not sure if it matters, but it is an ajax source for the data. For some reason, the row is not deleted and the table is not redrawn (I understand this code does not actually remove the record on the database) - no errors are thrown:
[code]
function DeleteItem(id) {
//ajax call to delete
var oTable= $("#example").dataTable();
var node = fnGetSelected(oTable); ;
oTable.fnDeleteRow(node,null,true);
}

function fnGetSelected(oTableLocal) {
var aReturn = new Array();
var aTrs = oTableLocal.fnGetNodes();
for (var i = 0; i < aTrs.length; i++) {
if ($(aTrs[i]).hasClass('deleting')) {
aReturn.push(aTrs[i]);
}
}
return aReturn;
}
[/code]

Replies

  • cdaiglecdaigle Posts: 57Questions: 0Answers: 0
    Hi,

    Is the DeleteItem function supposed to delete the row represented by id (the argument being passed in) or the selected rows? If it is the selected rows, you will need to loop through node and delete each row in the array individually (since fnDeleteRow only deletes 1 at a time).

    Otherwise, I assume you should only need to call oTable.fnDeleteRow(id);.

    Hope this helps.
  • rob_deanrob_dean Posts: 4Questions: 0Answers: 0
    Hi cdaigle,

    The id parameter is supposed to be a record identifier used by the ajax call that will delete the record on the database. It does not refer to the row on the dataTable.
  • cdaiglecdaigle Posts: 57Questions: 0Answers: 0
    Hi Rob,

    Does looping through node work? This code should work (unless there is a problem with the server-side processing):
    [code]
    function DeleteItem(id) {
    //ajax call to delete
    var oTable= $("#example").dataTable();
    var aNodes = fnGetSelected(oTable);
    for( var idx = 0; idx < aNodes.length; idx++ )
    oTable.fnDeleteRow(aNodes[idx],null,false);
    oTable.fnDraw();
    }
    [/code]
    I changed the third parameter to false and call fnDraw() at the end to optimize the function (so it removes all the rows then redraws, rather than redraw everytime).

    If you know you will only be removing one row every time, something simpler like this should work (assuming the row to be deleted is the first one returned by fnGetSelected):
    [code]
    function DeleteItem(id) {
    //ajax call to delete
    var oTable= $("#example").dataTable();
    oTable.fnDeleteRow(fnGetSelected(oTable)[0]);
    }
    [/code]
  • rob_deanrob_dean Posts: 4Questions: 0Answers: 0
    Hi cdaigle,
    Thanks for your interest in my question.

    I tried this but had no luck:
    [code]
    function DeleteItem(id) {
    //ajax call to delete
    var oTable= $("#example").dataTable();
    oTable.fnDeleteRow(fnGetSelected(oTable)[0],null,true);
    }

    function fnGetSelected(oTableLocal) {
    var aReturn = new Array();
    var aTrs = oTableLocal.fnGetNodes();
    for (var i = 0; i < aTrs.length; i++) {
    if ($(aTrs[i]).hasClass('deleting')) {
    aReturn.push(aTrs[i]);
    }
    }
    return aReturn;
    }
    [/code]
This discussion has been closed.