Deleting a row - using a confirmation - is somehow breaking the normal processing of pages
Deleting a row - using a confirmation - is somehow breaking the normal processing of pages
I had a datatable grid that allows for a row to be deleted by clicking on the remove link.
The delete request is sent to the function below for confirmation or canceling. In both cases, returning false should have canceled any event default actions, and left the user on the current page. However, if the user canceled the delete in the confirm dialog, then the user was somehow taken to the page that was -1 in history(page prior to the current page).
If the user confirmed the delete, and the row is deleted from the grid, everything is fine and the page that contains the grid was still the displayed page and the grid row was is removed. Great!
However, if the delete is canceled by the user, and I just return false in the code, the user was not being allowed to stay on the page that he/she was viewing… they were somehow returned to the page that they came from before the current page.
To fix this, I had to affect some action on the grid prior to returning false in the cancel delete logic. I had to calculate the number of rows on the grid, then add 1 to that total, and try a delete of the row that does not really exist… By doing this – the return false works and the user is left on the same page as they are viewing the grid from.
I have no idea why this is what happened, but I wanted to publish this to the team so that if you are working with the datatables, and have a similar situation, you can at least be aware of what I experienced.
Thanks.
Here is the code that is called, when the remove link in the data grid was clicked... This version is working because in the cancel delete flow, I hack the code when the uses cancels the confirm with a negative response, to attempt to delete a row that does not exist. This fixes the problem.
The grid column in each row with a button or a link(href='#') (I've tried it with both elements) that has an onClick event wired to call this function. If the operator cancels the confirm, I have to make some datatable action or else I will not stay on the current page... I will be forced to the prior page.
function removeMyIndex(event, index, id) {
var answer = confirm("Remove the existing defect?");
if (answer) {
var objDefect = new DefectObject();
var data = $('#dt-ajax-objects').dataTable().fnGetData(index);
deletedDefects.push(objDefect);
var x = $("#dt-ajax-objects");
$("#dt-ajax-objects").data("DeletedDefects", deletedDefects);
$('#dt-ajax-objects').dataTable().fnDeleteRow(index);
return false;
}
else {
var save = $('#dt-ajax-objects').dataTable().fnGetData().length;
$('#dt-ajax-objects').dataTable().fnDeleteRow(save + 1);
// if i don't issue the above two lines of code... the return false will result in me getting back to a page prior to the one I was on.
return false;
}
The delete request is sent to the function below for confirmation or canceling. In both cases, returning false should have canceled any event default actions, and left the user on the current page. However, if the user canceled the delete in the confirm dialog, then the user was somehow taken to the page that was -1 in history(page prior to the current page).
If the user confirmed the delete, and the row is deleted from the grid, everything is fine and the page that contains the grid was still the displayed page and the grid row was is removed. Great!
However, if the delete is canceled by the user, and I just return false in the code, the user was not being allowed to stay on the page that he/she was viewing… they were somehow returned to the page that they came from before the current page.
To fix this, I had to affect some action on the grid prior to returning false in the cancel delete logic. I had to calculate the number of rows on the grid, then add 1 to that total, and try a delete of the row that does not really exist… By doing this – the return false works and the user is left on the same page as they are viewing the grid from.
I have no idea why this is what happened, but I wanted to publish this to the team so that if you are working with the datatables, and have a similar situation, you can at least be aware of what I experienced.
Thanks.
Here is the code that is called, when the remove link in the data grid was clicked... This version is working because in the cancel delete flow, I hack the code when the uses cancels the confirm with a negative response, to attempt to delete a row that does not exist. This fixes the problem.
The grid column in each row with a button or a link(href='#') (I've tried it with both elements) that has an onClick event wired to call this function. If the operator cancels the confirm, I have to make some datatable action or else I will not stay on the current page... I will be forced to the prior page.
function removeMyIndex(event, index, id) {
var answer = confirm("Remove the existing defect?");
if (answer) {
var objDefect = new DefectObject();
var data = $('#dt-ajax-objects').dataTable().fnGetData(index);
deletedDefects.push(objDefect);
var x = $("#dt-ajax-objects");
$("#dt-ajax-objects").data("DeletedDefects", deletedDefects);
$('#dt-ajax-objects').dataTable().fnDeleteRow(index);
return false;
}
else {
var save = $('#dt-ajax-objects').dataTable().fnGetData().length;
$('#dt-ajax-objects').dataTable().fnDeleteRow(save + 1);
// if i don't issue the above two lines of code... the return false will result in me getting back to a page prior to the one I was on.
return false;
}
This discussion has been closed.
Replies
If, in your datatable definition, you have code for the fnRowCallBack as below, and you click on any row, or link, or button in the row….
This code will be executed, even if you are in a confirmation dialog, and you cancel and return false…. This code get run, even on a confirmation dialog cancel request that returns false.