[solved] fnDeleteRow() deleting wrong rows
[solved] fnDeleteRow() deleting wrong rows
manikanta
Posts: 7Questions: 0Answers: 0
// 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
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
This discussion has been closed.
Replies
[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]
Solved it by just doing:
[code]
rownum=table.fnGetPosition(row.find('td')[0])[0]
req.fnDeleteRow(rownum);
[/code]
Allan
table.fnDeleteRow(table.fnGetPosition(tr.find('td')[0])[0])
I'm using 1.9.4.
Allan