Editor field edit

Editor field edit

mguinnessmguinness Posts: 85Questions: 12Answers: 1

I've been using select2 plugin with remote ajax data sources. I've had several occasions when I need to change the ajax url dynamically, but after the form is initialized you need to clear() and then add() which seems a lot of work to just change a url.

Removing the field is straightforward, but adding it back is a little more problematic. First you need to determine the field position to add it back and then you need to set all the field options again. Would it be possible to create a new edit() method to the api? I've seen this question asked a few times in the forums so it seems that other people would find this useful.

Also according to the event sequence there is no event before field default values are set, i.e. before initEdit. I know I could run code before calling create() / edit() / remove() but a new event that is triggered before the form has been initialized would be convenient.

This question has an accepted answers - jump to answer

Answers

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

    Does Select2 provide an API to be able to change the data source? If so, that would be the perfect way to do it (although I don't actually see such a method on a quick scan through their documentation, although I might just be missing it).

    The inst() field method from the plug-in lets you access the Select2 methods, so that would be the way to do it.

    Allan

  • mguinnessmguinness Posts: 85Questions: 12Answers: 1

    I've also searched and the consensus is that select2 doesn't allow options to be changed dynamically. The only way is to reinitialize control with new options which is unfortunate as inst() won't be useful in that case.

    I suppose adding an edit() method will be a lot of work for you, but how about adding a preInit() event so I can reinitialize control there?

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

    The add() method has an after option which can be used to specify where a field should be entered into the form. I wonder if that parameter matched an existing field it should just replace it at that point. That would seem to solve most (all?) of these issues?

    Allan

  • mguinnessmguinness Posts: 85Questions: 12Answers: 1

    Works for me Allan. The icing on the cake would be a way to retrieve the existing field configuration object if there isn't already a way to achieve that.

This discussion has been closed.