asfenimage.blogg.se

Select2 ajax livereload
Select2 ajax livereload













  1. #SELECT2 AJAX LIVERELOAD HOW TO#
  2. #SELECT2 AJAX LIVERELOAD CODE#

If you were to supply an initSelection function for an autocomplete (that uses ajax), it would probably need to make an ajax call to build the data object. We normally require to do autocomplete task with select box when we have large amount of data like products, items, category, tag, user etc. In your case, you probably don't need to supply the initSelection function since your element does not have an initial value (in the html) and you are not going to call its. Jquery select2 plugin is a very famous jquery plugin, using select2 plugin we can do several thing like select box with search, select option with check box, ajax auto-complete etc. (I say "return" here, but it doesn't really return the data object it passes it to a callback function.) In that case, the initSelection function would just have to look up the id in the data list and return the corresponding data object. If the Select2 control was displaying a static list, the initSelection function would be easy to write (and it seems like Select2 could supply it for you). That is because those values consist of only an id and Select2 needs the entire data object (partly so it can display the correct label). If you search through the Select2 documentation for "initSelection", you will see that it is used when the element has an initial value and when the element's. Try getting rid of the initSelection function and instead set the initial value using: lect2('data', ) trigger('change') causes the initSelection function to get called, which changes the selected value back to "ENABLED_FROM_JS". Are you using that function to set the initial value? I know the Select2 documentation makes it sound like that is it's purpose, but it also says "Essentially this is an id->object mapping function," and that is not how you have implemented it.įor some reason the call to. I think the problem is the initSelection function. Select2 v4 has a very different API than v3. Note: The Question and this Answer are for Select2 v3. I hope this extra information helps somebody else!

#SELECT2 AJAX LIVERELOAD CODE#

Then the code works as it should, with the newObject available in select2's change event and the values being set correctly. If, instead, you use: $(element).select2('data', newObject, true) I was using: $(element).select2('data', newObject).trigger('change') īut this results in an empty add object inside select2's change event. Having said that, it seems that my biggest mistake here was the way I was trying to trigger the change. The accepted answer below is very useful, helps shed some light on the right way to initialise the selection and explains what initSelection is for. I have tried all sorts of combinations of jQuery and Select2 data and val methods, called against various inputs on the page, but nothing seems to work.

  • the Select2 field will submit a value of "1" when the form is sent to the server.
  • the Select2 field shows the text "NEW VALUE" to the user, and.
  • What I want to know is what function I can attach to the button so that

    select2 ajax livereload

    There's a JS Fiddle here that shows roughly what I have.

    #SELECT2 AJAX LIVERELOAD HOW TO#

    I have a Select2 auto-complete input (built via SonataAdmin), but cannot for the life of me figure out how to programmatically set it to a known key/value pair.















    Select2 ajax livereload