compare old / new values?

compare old / new values?

FreedyFreedy 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:

Answers

  • Tester2017Tester2017 Posts: 145Questions: 23Answers: 17
    edited March 2018

    Supposing that you want to know this after a submit is given.

    Use the editor events "open" and "preSubmit", like this:

    editor.on(`open`, function(e, mode, action) {
        fieldOldValue = this.field(`yourField`).val();
    });
    
    editor.on(`preSubmit`, function(e, data, action)
    {
        if (fieldOldValue != this.field(`yourField`).val()){
            // do something
        }
    });
    

    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:

     if (action ===`edit`)
    
  • Tester2017Tester2017 Posts: 145Questions: 23Answers: 17

    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.

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    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

  • FreedyFreedy Posts: 33Questions: 5Answers: 0

    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

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    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

  • FreedyFreedy Posts: 33Questions: 5Answers: 0

    isn't field()->getValue supposed to do that?

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    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

This discussion has been closed.