Skip to content

Commit 6ded2a8

Browse files
committed
Respect disabled options when filtering and selecting values.
There is a new setting called showDisabledOptions that determines whether an option shows in the list as disabled, or doesn't show up at all.
1 parent f8ffdb4 commit 6ded2a8

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

flexselect.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
overflow: hidden;
3434
}
3535

36+
.flexselect_dropdown li.disabled {
37+
cursor: not-allowed;
38+
color: GrayText;
39+
}
40+
3641
.flexselect_selected {
3742
background-color: Highlight;
3843
color: HighlightText;

jquery.flexselect.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
hideDropdownOnEmptyInput: false,
2626
selectedClass: "flexselect_selected",
2727
dropdownClass: "flexselect_dropdown",
28+
showDisabledOptions: false,
2829
inputIdTransform: function(id) { return id + "_flexselect"; },
2930
inputNameTransform: function(name) { return; },
3031
dropdownIdTransform: function(id) { return id + "_flexselect_dropdown"; }
@@ -52,13 +53,14 @@
5253
},
5354

5455
preloadCache: function() {
55-
var name, group, text;
56+
var name, group, text, disabled;
5657
var indexGroup = this.settings.indexOptgroupLabels;
5758
this.cache = this.select.find("option").map(function() {
5859
name = $(this).text();
5960
group = $(this).parent("optgroup").attr("label");
6061
text = indexGroup ? [name, group].join(" ") : name;
61-
return { text: $.trim(text), name: $.trim(name), value: $(this).val(), score: 0.0 };
62+
disabled = $(this).parent('optgroup[disabled]').size() != 0 || typeof $(this).attr('disabled') != 'undefined';
63+
return { text: $.trim(text), name: $.trim(name), value: $(this).val(), disabled: disabled, score: 0.0 };
6264
});
6365
},
6466

@@ -206,11 +208,13 @@
206208
},
207209

208210
filterResults: function() {
211+
var showDisabled = this.settings.showDisabledOptions;
209212
var abbreviation = this.input.val();
210213
if (abbreviation == this.lastAbbreviation) return;
211214

212215
var results = [];
213216
$.each(this.cache, function() {
217+
if (this.disabled && !showDisabled) return;
214218
this.score = LiquidMetal.score(this.text, abbreviation);
215219
if (this.score > 0.0) results.push(this);
216220
});
@@ -237,6 +241,7 @@
237241
},
238242

239243
renderDropdown: function() {
244+
var showDisabled = this.settings.showDisabledOptions;
240245
var dropdownBorderWidth = this.dropdown.outerWidth() - this.dropdown.innerWidth();
241246
var inputOffset = this.input.offset();
242247
this.dropdown.css({
@@ -249,7 +254,12 @@
249254
var html = '';
250255
$.each(this.results, function() {
251256
//html += '<li>' + this.name + ' <small>[' + Math.round(this.score*100)/100 + ']</small></li>';
252-
html += '<li>' + this.name + '</li>';
257+
if (this.disabled && !showDisabled) return;
258+
else if (this.disabled && showDisabled) {
259+
html += '<li class="disabled">' + this.name + '</li>';
260+
} else {
261+
html += '<li>' + this.name + '</li>';
262+
}
253263
});
254264
this.dropdownList.html(html);
255265
this.adjustMaxHeight();
@@ -267,9 +277,15 @@
267277

268278
var rows = this.dropdown.find("li");
269279
rows.removeClass(this.settings.selectedClass);
270-
this.selectedIndex = n;
271280

272-
var row = $(rows[n]).addClass(this.settings.selectedClass);
281+
var row = $(rows[n]);
282+
if (row.hasClass('disabled')) {
283+
this.selectedIndex = null;
284+
return;
285+
}
286+
287+
this.selectedIndex = n;
288+
row.addClass(this.settings.selectedClass);
273289
var top = row.position().top;
274290
var delta = top + row.outerHeight() - this.dropdown.height();
275291
if (delta > 0) {
@@ -283,7 +299,7 @@
283299

284300
pickSelected: function() {
285301
var selected = this.results[this.selectedIndex];
286-
if (selected) {
302+
if (selected && !selected.disabled) {
287303
this.input.val(selected.name);
288304
this.setValue(selected.value);
289305
this.picked = true;

0 commit comments

Comments
 (0)