[solved] fnDeleteRow() deleting wrong rows

[solved] fnDeleteRow() deleting wrong rows

manikantamanikanta Posts: 7Questions: 0Answers: 0
edited August 2011 in DataTables 1.8
// I've posted my question in the bug reports unknowingly. Reposting here expecting some help.

I've created the 'Actions' column dynamically and inserted 'Delete' icon, and passing the table index.

Script I m using to create the table and column:

[code]
(function () {
App = {};
// delete the row
App.deleteRow = function (index, dbId) {
if(confirm('Do you want to delete: ' + dbId)) {
if(deleteRecordAtServer()) { // make server request
jqdt.fnDeleteRow(index, function (dtSettings, row) {
console.log('Row deleted');
}, true);
}
}
};

var renderActions = function (idCol) {
var id = idCol.aData.id; // get the id
return '';
}

$.fn.dataTableExt.sErrMode = 'throw';
jqdt = $('#dataTable').dataTable( {
bJQueryUI: true,
sPaginationType: 'full_numbers',
aaData: testdata.records,
aoColumns: [
{ mDataProp: 'name', sTitle: 'Name' },
{ mDataProp: 'name', sTitle: 'Name' },
{ mDataProp: 'id', bSortable: false, sWidth: 'auto',
sTitle: 'Actions', fnRender: renderActions }
]
} );

}());
[/code]

And, the test data is:

[code]
testdata = {
"records": [
{"id": "1", "name": "Record 1" },
{"id": "2", "name": "Record 2" },
{"id": "3", "name": "Record 3" },
{"id": "4", "name": "Record 4" },
{"id": "5", "name": "Record 5" }
]
}
[/code]

When I delete for first time (some times, for second time too) correct row is getting deleted. But from then on, some random row is getting deleted.

Is this a bug or will indexes change when a row is deleted? if indexes are changed again, then we've a problem with the 'Delete' link generation!

Thanks,
ManiKanta

Replies

  • manikantamanikanta Posts: 7Questions: 0Answers: 0
    I found a workaround (or is it the correct solution?) by using the below code (thanks to the examples code & StackOverFlow post: http://stackoverflow.com/questions/1926183/how-to-delete-current-row-with-jquery-datatable-plugin).

    [code]
    App.deleteRow = function (dtTow, dbId) {
    var index = jqdt.fnGetPosition(dtTow); // get the table index
    if(confirm('Do you want to delete: ' + dbId)) {
    if(deleteRecordAtServer()) { // make server request
    jqdt.fnDeleteRow(index, function (dtSettings, row) {
    console.log('Row deleted');
    }, true);
    }
    }
    };

    var renderActions = function (idCol) {
    var id = idCol.aData.id; // get the id
    $('.row-delete', '#dt_actions_' + id).live('click', function () {
    var row = $(this).closest("tr").get(0);
    App.deleteRow(row, id);
    });
    return '\
    ';
    }

    [/code]
  • ernopernop Posts: 3Questions: 0Answers: 0
    This fixed it for me. I had the row tr, but sending that to fnDeleteRow didn't work.

    Solved it by just doing:
    [code]
    rownum=table.fnGetPosition(row.find('td')[0])[0]
    req.fnDeleteRow(rownum);
    [/code]
  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    Just give fnDeleteRow the TR element you want to delete. fnGetPosition should only very rarely be needed :-)

    Allan
  • marcusmarcus Posts: 3Questions: 0Answers: 0
    @allan - I had the same problem but only with rows added programmatically to the table with table.fnAddData([file]). Deletes worked on rows that were rendered when the table was created, but if I added a new row then used fnDeleteRow it always deleted the previous row, even though I was sure that I had the correct td. I ended having to do the same thing that ernop is doing:
    table.fnDeleteRow(table.fnGetPosition(tr.find('td')[0])[0])

    I'm using 1.9.4.
  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    Could you link to a test case showing that issue so I can fix it please?

    Allan
This discussion has been closed.