Prevent Select input OnChange event running when Editor form is opened

Prevent Select input OnChange event running when Editor form is opened

brendonsbrendons Posts: 39Questions: 14Answers: 1

On my form I have a select input named 'ApplicationStatus' with options of 'Pending' and 'Offered'. 'Pending' is the default.
When the select is changed (via Editor) from 'Pending' to 'Offered' I run a server process (a php file) via ajax.
That all woks as planned, however I have an unexpected behaviour; if a record that already has a status of 'Offered' is opened in Editor, the onChange event is ignored and the code runs immediately.
How can I prevent the OnChange event running for records that are already in the 'Offered' state when Editor is opened but nothing has changed?

My code:

editor.field('ApplicationStatus').input().on('change', function (e) {
    //if the record being edited already has a status of Offered, the next line fires when editor opens 
    //but it shouldn't.
    //it should only fire if the select has changed

    if (editor.field('ApplicationStatus').val() === 'Offered') {
                    var myEnr = editor.field('Enr_AppID').val();
                    var myCurrGrade = editor.field('CurrentGrade').val();
                    $.post("php/send_enrollment_documents.php",
                            {
                                Enr_AppID: "" + myEnr,
                                CurrentGrade: "" + myCurrGrade
                            }
                    );
    }
});

This question has an accepted answers - jump to answer

Answers

  • Tom (DataTables)Tom (DataTables) Posts: 139Questions: 0Answers: 26

    Hi Brendons

    Would you be able to give me a link to the page or to a test case then I can take a look for you.

    Information on how to create a test page, if you can't provide a link to your own page can be found here.

    Thanks

    Tom

  • allanallan Posts: 63,799Questions: 1Answers: 10,514 Site admin
    Answer ✓

    Just to add to what Tom has mentioned, what you are seeing is caused by Editor triggering the change event as it set the value for the edit to occur. However, to be able to distingish between a programmatic change like that and a user triggered change, Editor will pass a second parameter to the event handler, an object with the property editor set to boolean true.

    So you could use:

    editor.field('ApplicationStatus').input().on('change', function (e, d) {
        if ( d && d.editor ) {
            return;
        }
     
        if (editor.field('ApplicationStatus').val() === 'Offered') {
                        var myEnr = editor.field('Enr_AppID').val();
                        var myCurrGrade = editor.field('CurrentGrade').val();
                        $.post("php/send_enrollment_documents.php",
                                {
                                    Enr_AppID: "" + myEnr,
                                    CurrentGrade: "" + myCurrGrade
                                }
                        );
        }
    });
    

    Allan

  • brendonsbrendons Posts: 39Questions: 14Answers: 1

    Many thanks for the replies.
    Allan's answer did the trick.

This discussion has been closed.