Skip to content

clearOptions: Don't remove already selected items #1080

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Fixed bug making `clearOptions` function. Now it doesn't remove already selected options. (thanks @caseymct - #1079)
* Fixed bug making `allowEmptyOption: true` useless (thanks @mcavalletto - #739)
* Functions in option `render` can now return a DOM node in addition to
text. (#617)
9 changes: 7 additions & 2 deletions src/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -1336,10 +1336,15 @@ $.extend(Selectize.prototype, {
self.loadedSearches = {};
self.userOptions = {};
self.renderCache = {};
self.options = self.sifter.items = {};
var options = self.options;
$.each(self.options, function(key, value) {
if(self.items.indexOf(key) == -1) {
delete options[key];
}
});
self.options = self.sifter.items = options;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not the same as:

$.each(self.options, function(key, value) {
  if(self.items.indexOf(key) == -1) {
    delete self.options[key];
  }
});
self.sifter.items = self.options;

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brookjordan I confirm, your code works fine.

self.lastQuery = null;
self.trigger('option_clear');
self.clear();
},

/**
Expand Down