With the following changes, Giva, Inc. ( http://givainc.com/ ) has added:
Added the ability to preload default items in select2 multiple.
Added the ability to disable selections.
@ivaynberg - Any feedback on this? Thanks.
Closing, removing the preload change as you can do the same thing with 'data:' - Will modify it the disabled options to work with the existing data function. Will submit a new pull requests when I've completed this.
instead of "disabled" lets call it "locked", disabled is meant for items that show up in the list but cannot be selected - like in the option disabled='true' attribute.
I can certainly change that. After looking things over, it seems that preload is still relevant because data doesn't kick off for a multi-select.
preload should be handled by initSelection...
Then, there's a bug. If you consider the following:
It fails to load anything. If I comment out line 1944 & 1954 in the main select2.js file, this preloads just fine.
i dont think thats a bug. your input field doesnt specify any value so select2 doesnt think there is anything to initialize... if you add value="0" attribute to the input tag it works fine.
for your usecase you can call data after your init select2
My input field shouldn't have any value because I'm attempting to 'preload' data.
You pointed me in the direction of initSelection (note, 3 comments above):
"preload should be handled by initSelection..."
And, I attempted that which is why I said it's a bug / issue. If preloading data should indeed be handled by initSelection, then initSelection needs to accept the fact of the possibility that value is empty.
I do acknowledge that $(element).select2("data", preload_data) does work. I'll go that route and make a pull for the locked items instead.
why would the control have preloaded data if it has no values for such data specified in the tag?
when you want to preselect data in a select you specify the options as selected. same idea with the input tag, set the value to the ids of the items that should be preselected.
My thought was that the initSelection should populate the hidden form field because I'm using it to preload data and preload the selections.
Coworker clarified your thoughts a little via IM and actually gave me a reason why we'd want the hidden form field populated manually (in case for whatever reason, select2 doesn't initialize properly, at least the form will still work)
For clarity's sake, @ivaynberg is talking about this: