Manually trigger change event on editor field
Manually trigger change event on editor field
kaustubh.agrawal2000
Posts: 88Questions: 39Answers: 2
in Editor
I want to manually trigger change event on an editor field so that the logic in
editor.dependent("", )...
Logic is re-executed.
Is it possible ?? how can I do that ?
This question has accepted answers - jump to:
This discussion has been closed.
Answers
You could try a trick like this:
Although the above code will trigger the "dependent" event, it does not make a lot of sense if you are using an event for something that is not really happening, so maybe you can explain why you want this kind of approach? Maybe there are better ways to drive....
You can use
field().input()
to get theinput
element and then trigger achange
event on it:Allan
Hi Allan,
The above answer doesn't work when the field that has the dependent is initialised as a hidden field.
At the moment, that ends up meaning that hidden fields need to have dependent values sourced and then updated manually on a per-field basis.
If you have a complex dependent return data source that updates multiple fields options, show, hide etc, this workaround ends up needing a lot more code to do the same job, because instead of the dependent just updating all the various things that are returned automatically, you have to go and manually update each one via the editor field api calls.
Is the dependent not triggering or being able to be triggered for hidden fields intentional? Is there a workaround that I could use that doesn't involve manually updating every field that the dependent would normally update automatically?
DEMOS:
Demonstration where a readonly field triggers the dependent call:
http://live.datatables.net/zacizofe/1/edit?js,output
Demonstration where a hidden field does not trigger the dependent call:
http://live.datatables.net/muvojoko/1/edit?js,output
Demonstration with a workaround to force the update of the options based on the hidden field value:
http://live.datatables.net/fitehiqa/2/edit?js,output
Code with workaround for hidden field active (for easier reading in the context of this thread):
No - that's not intentional. At least it wasn't! The reason for it is that the
hidden
input type doesn't actually create a<input type="hidden">
element at all. It will just store the value in Javascript which is why a DOM event triggered on it does nothing.I've just been experimenting with some code to make it work, and while it does, it feels very hacky. But while doing so I thought of a better option - create a field as normal then hide it via the API - e.g.
That way its a regular DOM
input
element so events will work, but the end user can't see it.Allan
Thank you for the speedy reply!
Your response reminded me of the
attr
property, so setting up the field astype: 'text'
with theattr: {type: 'hidden'}
creates a DOM element, and this works with triggering the dependent, while still having the field hidden from the get go, and not having to make any additional api calls to hide it!Still a little hacky, but I think it's cleaner in communicating the intent of the field to always be hidden, while still having its value be available for dependent triggers.