Skip to content

Commit cd92405

Browse files
committed
Merge pull request select2#2053 from jdecuyper/feature-hide-selection-from-result
Add hideSelectionFromResult function
2 parents 2244365 + 84cf134 commit cd92405

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

select2.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,21 +2257,29 @@ the specific language governing permissions and limitations under the Apache Lic
22572257

22582258
// single
22592259
postprocessResults: function (data, initial, noHighlightUpdate) {
2260-
var selected = 0, self = this, showSearchInput = true;
2260+
var selected = 0, selectedElm = null, self = this, showSearchInput = true;
22612261

22622262
// find the selected element in the result list
22632263

22642264
this.findHighlightableChoices().each2(function (i, elm) {
22652265
if (equal(self.id(elm.data("select2-data")), self.opts.element.val())) {
22662266
selected = i;
2267+
selectedElm = elm;
22672268
return false;
22682269
}
22692270
});
22702271

22712272
// and highlight it
22722273
if (noHighlightUpdate !== false) {
22732274
if (initial === true && selected >= 0) {
2274-
this.highlight(selected);
2275+
// By default, the selected item is displayed inside the result list from a single select
2276+
// User can provide an implementation for 'hideSelectionFromResult' and hide it
2277+
if(this.opts.hideSelectionFromResult !== undefined && selectedElm !== null) {
2278+
if(this.opts.hideSelectionFromResult(selectedElm))
2279+
selectedElm.addClass("select2-selected");
2280+
}
2281+
else
2282+
this.highlight(selected);
22752283
} else {
22762284
this.highlight(0);
22772285
}
@@ -2995,9 +3003,14 @@ the specific language governing permissions and limitations under the Apache Lic
29953003
choices.each2(function (i, choice) {
29963004
var id = self.id(choice.data("select2-data"));
29973005
if (indexOf(id, val) >= 0) {
2998-
choice.addClass("select2-selected");
2999-
// mark all children of the selected parent as selected
3000-
choice.find(".select2-result-selectable").addClass("select2-selected");
3006+
// By default, the selected item is hidden from the result list inside a multi select
3007+
// User can provide an implementation for 'hideSelectionFromResult' and allow the same
3008+
// element to be selected multiple times.
3009+
if(self.opts.hideSelectionFromResult === undefined || self.opts.hideSelectionFromResult(choice)) {
3010+
choice.addClass("select2-selected");
3011+
// mark all children of the selected parent as selected
3012+
choice.find(".select2-result-selectable").addClass("select2-selected");
3013+
}
30013014
}
30023015
});
30033016

@@ -3313,7 +3326,8 @@ the specific language governing permissions and limitations under the Apache Lic
33133326
selectOnBlur: false,
33143327
adaptContainerCssClass: function(c) { return c; },
33153328
adaptDropdownCssClass: function(c) { return null; },
3316-
nextSearchTerm: function(selectedObject, currentSearchTerm) { return undefined; }
3329+
nextSearchTerm: function(selectedObject, currentSearchTerm) { return undefined; },
3330+
hideSelectionFromResult: function(selectedObject) { return undefined; }
33173331
};
33183332

33193333
$.fn.select2.ajaxDefaults = {

0 commit comments

Comments
 (0)