Skip to content

Commit 4ba25b8

Browse files
committed
ability to provide custom matchers. closes select2#86
1 parent 4b451fe commit 4ba25b8

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

select2.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,13 @@
278278
}
279279

280280
return function (query) {
281-
var t = query.term.toUpperCase(), filtered = {};
281+
var t = query.term, filtered = {};
282282
if (t === "") {
283283
query.callback({results: data});
284284
return;
285285
}
286286
filtered.results = $(data)
287-
.filter(function () {return text(this).toUpperCase().indexOf(t) >= 0;})
287+
.filter(function () {return query.matcher(t, text(this));})
288288
.get();
289289
query.callback(filtered);
290290
};
@@ -301,11 +301,11 @@
301301
// if not a function we assume it to be an array
302302

303303
return function (query) {
304-
var t = query.term.toUpperCase(), filtered = {results: []};
304+
var t = query.term, filtered = {results: []};
305305
$(data).each(function () {
306306
var isObject = this.text !== undefined,
307307
text = isObject ? this.text : this;
308-
if (t === "" || text.toUpperCase().indexOf(t) >= 0) {
308+
if (t === "" || query.matcher(t, text)) {
309309
filtered.results.push(isObject ? this : {id: this, text: this});
310310
}
311311
});
@@ -470,7 +470,10 @@
470470
formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; },
471471
minimumResultsForSearch: 0,
472472
minimumInputLength: 0,
473-
id: function (e) { return e.id; }
473+
id: function (e) { return e.id; },
474+
matcher: function(term, text) {
475+
return text.toUpperCase().indexOf(term.toUpperCase()) >= 0;
476+
}
474477
}, opts);
475478

476479
if (typeof(opts.id) !== "function") {
@@ -481,15 +484,15 @@
481484
if (select) {
482485
opts.query = this.bind(function (query) {
483486
var data = {results: [], more: false},
484-
term = query.term.toUpperCase(),
487+
term = query.term,
485488
placeholder = this.getPlaceholder();
486489
element.find("option").each(function (i) {
487490
var e = $(this),
488491
text = e.text();
489492

490493
if (i === 0 && placeholder !== undefined && text === "") return true;
491494

492-
if (text.toUpperCase().indexOf(term) >= 0) {
495+
if (query.matcher(term, text)) {
493496
data.results.push({id: e.attr("value"), text: text});
494497
}
495498
});
@@ -676,6 +679,7 @@
676679
term: this.search.val(),
677680
page: page,
678681
context: self.context,
682+
matcher: self.opts.matcher,
679683
callback: this.bind(function (data) {
680684
var parts = [], self = this;
681685
$(data.results).each(function () {
@@ -726,6 +730,7 @@
726730
term: search.val(),
727731
page: this.resultsPage,
728732
context: null,
733+
matcher: opts.matcher,
729734
callback: this.bind(function (data) {
730735
var parts = [], // html parts
731736
def; // default choice

0 commit comments

Comments
 (0)