preSubmit firing twice on form submit cancel - (DT 1.10.16, Editor 1.6.5)
preSubmit firing twice on form submit cancel - (DT 1.10.16, Editor 1.6.5)
Versions:
jQuery 3.2.1
DataTables 1.10.16
Editor 1.6.5
I'm trying to create a prompt that asks people if they intended to delete a record if Deleted is selected from a status dropdown box (delete is actually updating a status column in the database, not an actual SQL delete). I've been trying to get it to work consistently, but I keep running into preSubmit firing twice.
I have the following code as a test:
editor.on( 'preSubmit', function ( e, data, action ) {
alert('test');
return false;
});
Whenever I submit the form, it alerts me with "test" twice, no matter what. If I add conditional code inside to check for if it's Deleted as in the following code:
// make sure user intended to delete record
editor.on( 'preSubmit', function ( e, data, action ) {
if(editor.field( '__my status field__' ).input().val() == '5'){
if(!confirm('Are you sure you want to delete this record?')){
return false;
}else{
return true;
};
}
});
It'll pop up asking me the question. If I hit "cancel", it'll ask me again. If I hit cancel again, it'll submit the form the next time I try to submit.
I'm stumped, and I'm hoping it's not a typo like last time you helped me (I seriously checked for that, I swear hah).
This question has accepted answers - jump to:
Answers
Hi maliwik maybe check if the action is an update like
I know that I have experienced weird behavior in the preSubmit event when I neglect to specify the action.
Shay
One other thing - are you pressing return or clicking on the button in the confirm dialogue? Try clicking on it rather than pressing return. I'm wondering if the return might be propagated through to the document.
Beyond that, can you link to a test case?
Allan
Rob, I added that for consistency-sake. Thanks!
Allan,
That is indeed what it is doing. Here is my test code:
When clicking the button everything behaves normally, but when hitting Return instead it asks twice.
Do you know if there might be a fix for this?
What browser are you using? With Chrome 60 if I run the following on this page
It seems to work as expected. It will never submit an edit since it is always returning false, but that's useful in this case to test.
Allan
I'm currently using Opera and the script checks to see if someone has selected the Deleted status from the Status dropdown box. I don't actually delete the data from the database, it just sets it so it's invisible from the end-user. I've been trying to figure out how to do this via the Editor remove button with PHP in the backend, but I've been unable to do so.
Use an edit action - not a delete. You can label it as "Delete" in the UI if you want, but really you are just editing the data so it doesn't match a condition.
To make it simple what I would normally recommend for this is to use a second Editor instance which just has the condition field. When the button is pressed (custom button documentation) use
edit()
to start editing,message()
to ask the user if they want to do the delete,buttons()
to define the buttons at the bottom of the form andval()
to set the field's value. When submitted, if it no longer matches thewhere
condition, it will be removed from the table.Allan
Thanks Allan!
I used the following code to add to the DataTables buttons declaration:
I then did some simple logic in the PHP backend like such:
Now it'll also make sure that the user can be modified (we don't want our client accidentally deleting themselves hehe ) and if they can't, it'll let them know that the selected user cannot be deleted.
In Firefox Developers edition and Opera at least, the key button presses still fire twice. It'll ask me if I want to delete twice if I hit the D shortcut key but I'm not sure how to fix that part.
Try adding
e.stopPropagation()
ande.preventDefault()
at the top of youraction
function.Allan
That didn't seem to fix it unfortunately.
I'm not sure what would cause that and haven't been able to reproduce it here. Are you able to link to a test case please?
Thanks,
Allan
It's in an administration section. Can I e-mail you some test login credentials?
You can drop me a private message by clicking my name above and then the "Send message" button.
Thanks,
Allan
With some detective work by Allan, I figured out that if you use the
B
dom element more than once within thedom
option when declaring the DataTables object such as"dom": '<"#tableButtonsTop.tableButtons"Bf>iprtip<"#tableButtonsBottom.tableButtons"B>',
, it will fire the key defined by thekey
option however many times your button groups exists when declaring the buttons. This happens in at least Opera and Firefox Developer Edition.I'm uncertain how to get around this behavior.
Ah - I missed an element of that before and it explains why I was a bit confused before. Don't use
B
twice in thedom
string. That would use thebuttons
configuration object twice, which is what is causing this issue.You'd need to add your second set of Buttons using the method shown in this example.
Allan
Is there a way to clone the button group instead of creating everything twice? Because I just want to have the same exact button group both above and below the table.
Basically what I did is created a function that just makes the buttons according to a few options and appends them to a container. It's a bit messy, but it works. I think perhaps allowing for the insertion of as many button groups initially defined within the main DataTables configuration object through the
B
element in thedom
would be pretty handy if it's possible.Here are the functions for inserting the group and deletion of record via an edit instead of delete action if anybody wants to use it or modify it to their needs:
The parameters are self explanatory, although
addKeys
should befalse
for all but the first call of the function to avoid triggering thebutton-action
event any subsequent times.To do it this way,
dom
must not have anyB
elements in it, andbuttons
should be set tofalse
in the original DataTables declaration.The following is a short snippet of what I used to insert the buttons into the dom:
tableReportCategories
is my DataTables object,editorReportCategories
is my Editor object,commandsTop
andcommandsBottom
are the two names of the groups,#tableButtonsTop
and#tableButtonsBottom
are the names of the containers I want the button groups to be inserted to, andtrue
andfalse
are theaddKeys
options - the keypress triggers will be added to the first one only (true
).I hope this can help anybody else out who's having issues with this.
Thanks for your help Allan and Rob!
-- Mike
Yup - that looks just about the perfect way to work around this. Thanks for positing your solution.
I'll look at putting something into Buttons to handle this case as I think it should just work as expected out of the box.
Allan
Just in case anyone else stumbles across this, Buttons 1.5.0 will address this issue directly. If buttons over multiple instances for the same table share a
key
handler, only the first will now trigger.Allan