Update field value based on another field changed inline.
Update field value based on another field changed inline.
I need to reset fields values based on a value changed in another inline field . I tried using the following script, however when the table is loaded the first time, the code is executed and wipes out the values in said fields. I just need to perform this operation when a user changes the HRWPER. So let's say, if the percent field changed from 2.0 to 3.0, I need to reset other fields.
I am sure there is a better way to do this so all suggestions are welcomed.
$( editor.field( 'HRWPER' ).input() ).on( 'change', function () {
//**Reset Plant Manager Approval function
editor.field( 'HRWMGR' ).val( 0);
editor.field( 'HRWMNA' ).val( '');
} );
This question has an accepted answers - jump to answer
Answers
You need to check if the
change
event was triggered by the end user or by Editor (i.e. when the edit it triggered, it has to set the value, which involves a change). You can do that using the second parameter that is passed into the event handler:Allan
Gave that a try and received this error..
Cannot read property 'editor' of undefined.
It does not like the "d.editor" in the if statement.
Sorry
if ( ! d && ! d.editor ) {
should have been:!
Allan
Allan,
I changed the if statement and the error is gone, however the function is not behaving they way I intended inline. When editing the HRWPER inline, the HRWMGR and HRWMNA fields that I need reset are not being submit to the DB, only the changed HRWPER is submit.
However, the function works fine in the editor form, the fields reset and when I click the update button it of course submits changes to the DB .
What am I missing?
Are you using the
allIfChanged
value for thesubmit
option of theform-options
object that we've discussed before?Allan
Yes, unless I have to add this elsewhere?
No - that should be all you need if that is the only place that you trigger inline editing. That should submit all of the fields in the form whenever the value of a single field changes. Can you link to the page so I can take a look please?
Allan
Allan,
I found the culprit. It is the following code. Not sure why it is messing with the inline submit However if I comment this out it, works for inline editing of the HRWPER field.
Not so fast , The last post referencing my Field validation function was causing a problems for my ->on( 'preEdit', function. I am still experiencing the issue with my editor.field change function. The JS is getting into the function when the HRWPER change, however the submit is not fired to change values in DB fields.
Just to recap (I'm get a little lost - sorry!):
That is this, right?
Then when you submit the form, are you saying that
HRWPER
is included in the submission, butHRWMGR
andHRWMNA
are not?Can you check the data being submitted to the server in the network panel of your browser please?
Sorry about the delay replying to your e-mails - I will do so later today.
Allan
I am sorry Allan I was juggle 2 things . Yes this is still an issue.
My file_put_contents( '/tmp/a', json_encode( $values )."\n", FILE_APPEND ); contains..
"HRWMGR":"54319",
"HRWMNA":"Jose Cuervo",
Based on my JS script the should be
"HRWMGR":"0",
"HRWMNA":"",
I will wait on your email reply .
If you add a console.log into that code (between lines 1 and 2, and another one on line 3) does that code actually execute?
Allan
Updated and answered. Allan pointed out that the
should be using the 'keydown' event instead of the 'change'
Refer to https://www.quirksmode.org/dom/events/keys.html for more insight.