compare old / new values?
compare old / new values?
Freedy
Posts: 33Questions: 5Answers: 0
In editor, how would I compare the value entered with the one stored in the database?
Thanks.
This question has accepted answers - jump to:
This discussion has been closed.
Answers
Supposing that you want to know this after a submit is given.
Use the editor events "open" and "preSubmit", like this:
This is a base solution because, in reality, you should also look to the value of "action" in the Editor "open" event. You only want to compare values if the action was an "edit" and not, for example, a "create" (as there is no value in the database at that moment) or a "delete" action.
So to be more accurate in both the "open" and "preSubmit " event you should test for:
I was confirming my answer and found out that instead of using the Editor "open" event, it is better to use the Editor "initEdit" event. https://editor.datatables.net/reference/event/initEdit.
Then you do not need to check for the action at the moment of saving your original value.
Depending on what you are using it for, another option is to do this on the server-side with server-side events. The data that is submitted is passed into the
pre*
event handlers and you could query the database to check if the values are different.This is the way to do it if you are creating a multi-user concurrent system. Client-side as Tester2017 suggests will work nicely if you only need to check the data against what the user loaded from the server.
Allan
on the server-side with the pre* events, is there a way to get a server-side field value? I tried getValue but it just returns null.
-Thomas
You mean from the database? You'd need to query the database. You can use the built in database functions or your own PDO connection.
Allan
isn't field()->getValue supposed to do that?
That will set the value that is sent from the server-side to the client-side when data is requested for the table. It effectively bypasses the database for such a field.
Allan