Skip to content
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

add preFilter option to filter options when building multiselect #675

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "bootstrap-multiselect",
"description": "Twitter Bootstrap plugin to make selects user friendly.",
"homepage": "http://davidstutz.github.io/bootstrap-multiselect/",
"version": "0.9.13",
"version": "0.9.14",
"keywords": [
"js",
"css",
Expand Down
134 changes: 72 additions & 62 deletions dist/js/bootstrap-multiselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,69 @@
}
},

runFilter: function(query) {
this.query = query || '';
var currentGroup, currentGroupVisible;
$.each($('li', this.$ul), $.proxy(function(index, element) {
var value = $('input', element).length > 0 ? $('input', element).val() : "";
var text = $('label', element).text();

var filterCandidate = '';
if ((this.options.filterBehavior === 'text')) {
filterCandidate = text;
}
else if ((this.options.filterBehavior === 'value')) {
filterCandidate = value;
}
else if (this.options.filterBehavior === 'both') {
filterCandidate = text + '\n' + value;
}

if (value !== this.options.selectAllValue && text) {

// By default lets assume that element is not
// interesting for this search.
var showElement = false;

if (this.options.enableCaseInsensitiveFiltering) {
filterCandidate = filterCandidate.toLowerCase();
this.query = this.query.toLowerCase();
}

if (this.options.enableFullValueFiltering && this.options.filterBehavior !== 'both') {
var valueToMatch = filterCandidate.trim().substring(0, this.query.length);
if (this.query.indexOf(valueToMatch) > -1) {
showElement = true;
}
}
else if (filterCandidate.indexOf(this.query) > -1) {
showElement = true;
}

// Toggle current element (group or group item) according to showElement boolean.
$(element).toggle(showElement).toggleClass('filter-hidden', !showElement);

// Differentiate groups and group items.
if ($(element).hasClass('multiselect-group')) {
// Remember group status.
currentGroup = element;
currentGroupVisible = showElement;
}
else {
// Show group name when at least one of its items is visible.
if (showElement) {
$(currentGroup).show().removeClass('filter-hidden');
}

// Show all group items when group name satisfies filter.
if (!showElement && currentGroupVisible) {
$(element).show().removeClass('filter-hidden');
}
}
}
}, this));
},

/**
* Builds the filter.
*/
Expand All @@ -1021,7 +1084,10 @@

this.$filter = $(this.options.templates.filter);
$('input', this.$filter).attr('placeholder', this.options.filterPlaceholder);

if (this.options.preFilter) {
$('input', this.$filter).val(this.options.preFilter);
}

// Adds optional filter clear button
if(this.options.includeFilterClearBtn){
var clearBtn = $(this.options.templates.filterClearBtn);
Expand All @@ -1036,6 +1102,10 @@

this.$ul.prepend(this.$filter);

if (this.options.preFilter) {
this.runFilter(this.options.preFilter);
}

this.$filter.val(this.query).on('click', function(event) {
event.stopPropagation();
}).on('input keydown', $.proxy(function(event) {
Expand All @@ -1050,67 +1120,7 @@
this.searchTimeout = this.asyncFunction($.proxy(function() {

if (this.query !== event.target.value) {
this.query = event.target.value;

var currentGroup, currentGroupVisible;
$.each($('li', this.$ul), $.proxy(function(index, element) {
var value = $('input', element).length > 0 ? $('input', element).val() : "";
var text = $('label', element).text();

var filterCandidate = '';
if ((this.options.filterBehavior === 'text')) {
filterCandidate = text;
}
else if ((this.options.filterBehavior === 'value')) {
filterCandidate = value;
}
else if (this.options.filterBehavior === 'both') {
filterCandidate = text + '\n' + value;
}

if (value !== this.options.selectAllValue && text) {

// By default lets assume that element is not
// interesting for this search.
var showElement = false;

if (this.options.enableCaseInsensitiveFiltering) {
filterCandidate = filterCandidate.toLowerCase();
this.query = this.query.toLowerCase();
}

if (this.options.enableFullValueFiltering && this.options.filterBehavior !== 'both') {
var valueToMatch = filterCandidate.trim().substring(0, this.query.length);
if (this.query.indexOf(valueToMatch) > -1) {
showElement = true;
}
}
else if (filterCandidate.indexOf(this.query) > -1) {
showElement = true;
}

// Toggle current element (group or group item) according to showElement boolean.
$(element).toggle(showElement).toggleClass('filter-hidden', !showElement);

// Differentiate groups and group items.
if ($(element).hasClass('multiselect-group')) {
// Remember group status.
currentGroup = element;
currentGroupVisible = showElement;
}
else {
// Show group name when at least one of its items is visible.
if (showElement) {
$(currentGroup).show().removeClass('filter-hidden');
}

// Show all group items when group name satisfies filter.
if (!showElement && currentGroupVisible) {
$(element).show().removeClass('filter-hidden');
}
}
}
}, this));
this.runFilter(event.target.value);
}

this.updateSelectAll();
Expand Down