How to prevent dependent handler from firing on opening of editor?

How to prevent dependent handler from firing on opening of editor?

daveslabdaveslab Posts: 40Questions: 14Answers: 0

Hi folks,

I'm using the .dependent() API to change the value of one field based on the value chosen for another field. However, dependent is triggered when the form opens even if the user doesn't touch the fields in question. Is there a way to detect when dependent is triggered on the opening of the form?

Thanks!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    The callback has to happen when the form is opened so that the values being edited can have its options updated based on those values.

    There isn't currently an option to overrule that since it might mean that the dependent field wouldn't have the value that it should do for the row being edited available in it.

    Allan

  • daveslabdaveslab Posts: 40Questions: 14Answers: 0

    Ah, I see. That's a shame. For the moment, I'll change to use attach and detach an event handler based on the open and close events I guess.

  • essicumdessicumd Posts: 10Questions: 2Answers: 1

    @daveslab could you maybe outline a little of how you're doing that as I also would like to prevent certain fields from being changed on editor open. (Allowing users to override settings if they need to but prevent them from accidentally opening a record and reverting to the un-overriden setting if they're updating other fields in the record that are not dependent on one another). Hopefully that makes a little sense. Any help would be much appreciated! Thanks!

  • daveslabdaveslab Posts: 40Questions: 14Answers: 0

    @essicumd Basically, as far as I can tell, when editing, Datatables Editor opens the form and then sets the value of each form element in the order in which the fields are defined. Unfortunately, this setting of the field value triggers the dependent handler and it is impossible to tell if it's the user or the initial action of DTE which is triggering it.

    Imagine for example a table of addresses where each element of the address is a field ; in this case, the country would be the last field. Now let's say that we want to set a dependent handler on the zip code. What you could do (and what I did) is set a dependent handler on the country field which, in turn, sets the dependent handler on the zip code field. That way, you are sure that it will only be triggered by a user edit (since the initial DTE edits already took place). And, of course, you have to disable this handler when the editor is closed.

    Obvious question : what happens if the country is changed by the user? You have to then make sure that there is a check inside the country dependent handler that checks to make sure that the zip code's dependent handler hasn't already been defined.

    That is a really complicated, messy way to do it. There is a shortcut : if you're only dealing with a text field you can tell dependent to use different input events (like only keyup). Ex:

    editor.dependent( 'position', '/ajax/update-position', {
        event: 'keyup change'
    } );
    

    Sorry if that's kinda complicated, but I haven't found another way. One other possible way (perhaps) is to do what I said but on a hidden field, and that way there will be less confusion.

This discussion has been closed.