Skip to content
This repository was archived by the owner on Jul 2, 2018. It is now read-only.

Commit e227e06

Browse files
committed
Added filter option, fixes #70
1 parent 2ee9083 commit e227e06

6 files changed

+71
-8
lines changed

CHANGES.txt

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2.4.0 @ 2012-11-12
2+
Deprecated filterResults, use filter instead
3+
Allow filter to take function as argument
4+
Added processData to docs
5+
16
2.3.1 @ 2012-11-12
27
Changed preventDefaultReturn to an integer option.
38

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
#
55

66
autocomplete:
7-
uglifyjs -nc ./src/jquery.autocomplete.js > ./src/jquery.autocomplete.min.js
7+
uglifyjs ./src/jquery.autocomplete.js > ./src/jquery.autocomplete.min.js

demo/index.html

+27-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,25 @@
9696
return processed;
9797
}
9898
});
99+
100+
$("#ac7").autocomplete({
101+
data: [
102+
['Chico Marx'],
103+
['Harpo Marx'],
104+
['Groucho Marx'],
105+
],
106+
filter: function(result, filter) {
107+
var s = result.value.toLowerCase();
108+
var f = filter.toLowerCase();
109+
var p = s.indexOf(f);
110+
if (p >= 0) {
111+
// Start of text or after a whitespace
112+
return p === 0 || !$.trim(s.substr(p - 1, 1));
113+
}
114+
return false;
115+
}
116+
});
117+
99118
});
100119

101120
</script>
@@ -158,8 +177,15 @@ <h2>Demo 6 (custom JSON processor)</h2>
158177
<input type="text" id="ac6">
159178
</form>
160179

180+
<h2>Demo 7 (custom filter)</h2>
181+
182+
<p>This demo uses the Marx Brothers and a custom filter.</p>
183+
184+
<form>
185+
<input type="text" id="ac7">
186+
</form>
187+
161188
<p style="height: 100px" class="spacer">&nbsp;</p>
162-
163189

164190
<hr>
165191

doc/jquery.autocomplete.txt

+14-3
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,15 @@ autoFill (default value: false)
138138
If this is set to true, the autocompleter will automatically fill the search box with the
139139
first entry in the results.
140140

141+
filter (default value: true)
142+
If this is set to false, the autocompleter will consider every result a valid response.
143+
If this is set to a function, the autocompleter will call that function for each result object
144+
and expect a true/false (include / leave out) response.
145+
At any other setting (including true), the autocompleter will filter the results using its
146+
standard filter.
147+
141148
filterResults (default value: true)
149+
DEPRECATED; use filter instead
142150
If this is set to true, the autocompleter will filter the results presented by the server,
143151
selecting only those that begin with the user input.
144152
If this is set to false, the autocompleter will consider every result from the server a
@@ -182,9 +190,12 @@ autoWidth (default value: "min-width")
182190
from interfering with your results width. Other sensible values are "min-width" (default) and "width".
183191

184192
useDelimiter (default value: false)
185-
Whether or not to delimit separate autocomplete strings within the same input field with a
186-
comma. For example, you may want to allow the user to enter a comma separated list of values--and
187-
each value will autocomplete without erasing the other delimited values in the input field.
193+
Whether or not to delimit separate autocomplete strings within the same input field with a
194+
comma. For example, you may want to allow the user to enter a comma separated list of values--and
195+
each value will autocomplete without erasing the other delimited values in the input field.
196+
197+
processData (default value: null)
198+
This function will process all incoming data before anything else. Use this to alter incoming data.
188199

189200
onError (default value: null)
190201
A JavaScript function that will be called if there is an error in the AJAX request to a remote

src/jquery.autocomplete.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
preventDefaultTab: 0,
6969
autoFill: false,
7070
filterResults: true,
71+
filter: true,
7172
sortResults: true,
7273
sortFunction: null,
7374
onItemSelect: null,
@@ -677,13 +678,13 @@
677678
};
678679

679680
/**
680-
* Filter result
681+
* Default filter for results
681682
* @param {Object} result
682683
* @param {String} filter
683684
* @returns {boolean} Include this result
684685
* @private
685686
*/
686-
$.Autocompleter.prototype.filterResult = function(result, filter) {
687+
$.Autocompleter.prototype.defaultFilter = function(result, filter) {
687688
if (!result.value) {
688689
return false;
689690
}
@@ -704,6 +705,26 @@
704705
return true;
705706
};
706707

708+
/**
709+
* Filter result
710+
* @param {Object} result
711+
* @param {String} filter
712+
* @returns {boolean} Include this result
713+
* @private
714+
*/
715+
$.Autocompleter.prototype.filterResult = function(result, filter) {
716+
// No filter
717+
if (this.options.filter === false) {
718+
return true;
719+
}
720+
// Custom filter
721+
if ($.isFunction(this.options.filter)) {
722+
return this.options.filter(result, filter);
723+
}
724+
// Default filter
725+
return this.defaultFilter(result, filter);
726+
};
727+
707728
/**
708729
* Filter results
709730
* @param results

src/jquery.autocomplete.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)