From 1c4d2309c964c9bf85d8f4953f88bdde48bbb6ad Mon Sep 17 00:00:00 2001 From: Kimberly Nicholls Date: Wed, 16 Dec 2015 14:30:22 -0800 Subject: [PATCH 1/3] pull runFilter code out into a function and add preFilter option set options.preFilter to filter options when building multiselect --- dist/js/bootstrap-multiselect.js | 134 +++++++++++++++++-------------- 1 file changed, 73 insertions(+), 61 deletions(-) diff --git a/dist/js/bootstrap-multiselect.js b/dist/js/bootstrap-multiselect.js index a7c71eab..33574066 100644 --- a/dist/js/bootstrap-multiselect.js +++ b/dist/js/bootstrap-multiselect.js @@ -1008,6 +1008,69 @@ } }, + runFilter: function() { + this.query = this.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. */ @@ -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); @@ -1036,6 +1102,11 @@ this.$ul.prepend(this.$filter); + if (this.options.preFilter) { + this.query = this.options.preFilter; + this.runFilter(); + } + this.$filter.val(this.query).on('click', function(event) { event.stopPropagation(); }).on('input keydown', $.proxy(function(event) { @@ -1051,66 +1122,7 @@ 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(); } this.updateSelectAll(); From 839510313e3267eaa11c9653f9c9a8f06e728255 Mon Sep 17 00:00:00 2001 From: Kimberly Nicholls Date: Wed, 16 Dec 2015 14:39:18 -0800 Subject: [PATCH 2/3] bump version --- bower.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bower.json b/bower.json index 9a22de26..5135f233 100644 --- a/bower.json +++ b/bower.json @@ -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", From 9e4f0c0607ed63091b1cdb0f2d85c3d0ae9f56a4 Mon Sep 17 00:00:00 2001 From: Kimberly Nicholls Date: Wed, 16 Dec 2015 15:03:39 -0800 Subject: [PATCH 3/3] pass a value to runFilter to support programmatic filtering: `$(selectId).multiselect('runFilter', 'text');` --- dist/js/bootstrap-multiselect.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/dist/js/bootstrap-multiselect.js b/dist/js/bootstrap-multiselect.js index 33574066..0dbef047 100644 --- a/dist/js/bootstrap-multiselect.js +++ b/dist/js/bootstrap-multiselect.js @@ -1008,8 +1008,8 @@ } }, - runFilter: function() { - this.query = this.query || ''; + 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() : ""; @@ -1103,8 +1103,7 @@ this.$ul.prepend(this.$filter); if (this.options.preFilter) { - this.query = this.options.preFilter; - this.runFilter(); + this.runFilter(this.options.preFilter); } this.$filter.val(this.query).on('click', function(event) { @@ -1121,8 +1120,7 @@ this.searchTimeout = this.asyncFunction($.proxy(function() { if (this.query !== event.target.value) { - this.query = event.target.value; - this.runFilter(); + this.runFilter(event.target.value); } this.updateSelectAll();