Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

option attribute disabled="disabled" #480

Closed
wants to merge 4 commits into from

8 participants

@loxK

Handle the disabled attribute of select options. Fixes #479

@keleshev

I would really like to see this behavior in select2.

@kevin-brown
Owner

The current version will break if Select2 is not attached to a <select> element.

@keleshev

@kevin-brown I'm not sure I understand, isn't Select2 always attached to a <select> and not to anything else?

@kevin-brown
Owner

Select2 can be attached to an <input type="hidden" /> for things like AJAX data and using a data array.

This implementation looks for result.element which is only present if it is attached to a <select> element.

@ootoovak

Damn, this is a deal breaker for me at the moment and sadly I do not have time to fix it myself. Hope to see it fixed and merged soon. Thanks for the effort so far @loxK.

@kevin-brown
Owner

@ootoovak It will work fine if you only use Select2 attached to a <select> element. You just need to grab the code from this pull request. I'm pretty sure it is up to date with the current master.

If you attach it to an <input> tag... then you need to wait for the fix.

@ootoovak

@kevin-brown cheers, I'll give it a go. I didn't look forward to rolling back to Chosen at this point in time.

@MikeBagos

Better do

disabled = result.element ? result.element[0].disabled : false;

to cover the case where it's not a <select>

@cvrebert

By the transitive property, this would fix #157.

@ivaynberg ivaynberg closed this in 8e9a231
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 7 additions and 4 deletions.
  1. +3 −1 select2.css
  2. +4 −3 select2.js
View
4 select2.css
@@ -554,6 +554,8 @@ disabled look for already selected choices in the results dropdown
text-decoration: none;
}
+.select2-result-unselectable { color: #aaa }
+
.select2-offscreen {
position: absolute;
left: -10000px;
@@ -570,4 +572,4 @@ disabled look for already selected choices in the results dropdown
.select2-search input {
background-position: 100% -21px !important;
}
-}
+}
View
7 select2.js
@@ -704,24 +704,25 @@ the specific language governing permissions and limitations under the Apache Lic
populate=function(results, container, depth) {
- var i, l, result, selectable, compound, node, label, innerContainer, formatted;
+ var i, l, result, selectable, disabled, compound, node, label, innerContainer, formatted;
for (i = 0, l = results.length; i < l; i = i + 1) {
result=results[i];
selectable=id(result) !== undefined;
+ disabled=result.element ? result.element[0].disabled : false;
compound=result.children && result.children.length > 0;
node=$("<li></li>");
node.addClass("select2-results-dept-"+depth);
node.addClass("select2-result");
- node.addClass(selectable ? "select2-result-selectable" : "select2-result-unselectable");
+ node.addClass(selectable && !disabled ? "select2-result-selectable" : "select2-result-unselectable");
if (compound) { node.addClass("select2-result-with-children"); }
node.addClass(self.opts.formatResultCssClass(result));
label=$("<div></div>");
label.addClass("select2-result-label");
- formatted=opts.formatResult(result, label, query);
+ formatted=opts.formatResult(result, label, query, node);
if (formatted!==undefined) {
label.html(self.opts.escapeMarkup(formatted));
}
Something went wrong with that request. Please try again.