New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Locked Items #503
Conversation
This patch creates multi select items that can't be removed.
will merge this when i have time to also update the docs with an example and the description. if you have time to do that in gh-pages branch would be great.... |
how would the lock work if i am loading my items dynamically, and not in javascript? |
For Ajax, in the |
Kevin is correct. The ajax would return |
Sorry, my mistake. The javascript side is fine, but if I was rendering the |
Not sure, I believe this feature is only for |
Locked items don't work with One option would be to turn this into a function, with parameters similar to |
After re-reading documentation, select2 does support |
Thanks to both - just to add something here - I would think that the lock feature should apply mainly to a multiple select, as one would want to lock an item in a list (multiple select, and not a single select).... |
@aj7: This was the was the reason the locked property was added--so that some defaults could be put in that can not be removed. I don't think it's particular useful for normal selections, just for selections you want to load as defaults. I'd recommend using the attribute "data-locked" as the attribute to pass in the locked status via the element. |
Hi, guys. Is there any chance that "data-locked" attribute will be implemented? I need functionality like "locked: true", but statically, without scripting. My situation: user only can add new items to multiselect, but never can remove already added. So I must show which items are already added, but must not allow to remove it. All HTML with and is generated at server side. |
As far as I can tell from looking at the 3.4.0 source code, you can specify locked on an HTML option element as follows: <option value="1" selected="selected" locked="locked">United States</option> This works. However, it leads to invalid HTML5. As suggested by @dswitzer and @windbridges, using a Line 776 of select2.js could be changed as follows: --- docroot/vendor/select2/3.4.0/select2.js 2013-05-14 08:27:46.000000000 -0700
+++ docroot/vendor/select2/3.4.0.HACKED/select2.js 2013-06-23 16:52:28.636936609 -0700
@@ -773,7 +773,7 @@
element: element.get(),
css: element.attr("class"),
disabled: element.prop("disabled"),
- locked: equal(element.attr("locked"), "locked")
+ locked: (equal(element.attr("locked"), "locked") || equal(element.attr("data-locked"), "true") || equal(element.data("locked"), true))
};
} else if (element.is("optgroup")) {
return { Then you could change the HTML option as follows: <option value="1" selected="selected" data-locked="true">TOTAL US - XAOC</option> I only performed limited testing, but it seemed to work nicely in Chrome and Firefox. Could one of the devs on the project verify and merge? Thanks |
Per our discussion previously, I've renamed disabled to locked instead.
An example of the code/feature can be found here:
http://jsfiddle.net/5wCDM/