angular-select2
angular-select2 copied to clipboard
Matcher is hardcoded
Configuring a matcher like this
<select2 class="form-control" ng-model="ngModel.selectedItem" ng-options="item.id as item.text for item in items" options="{matcher: fooMatcher}" required></select2>
has no effect when using angular-select2.
The custom query implementation has the matcher logic hardcoded instead of utilizing opts.matcher
.
See c7b700f:
// line 136
opts.query = function (query) {
var values = filterValues(valuesFn(scope));
var keys = (keyName ? sortedKeys(values) : values) || [];
var options = [];
for (var i = 0; i < keys.length; i++) {
var locals = {};
var key = i;
if (keyName) {
key = keys[i];
locals[keyName] = key;
}
locals[valueName] = values[key];
var value = valueFn(scope, locals);
var label = displayFn(scope, locals) || "";
if (label.toLowerCase().indexOf(query.term.toLowerCase()) > -1) { // <-- here! should be "if (opts.matcher(query.term, label)) {"
options.push({
id: value,
text: label,
obj: values[key]
});
}
}
query.callback({
results: options
});
};
I ran into this problem as well. Thanks for pointing out the fix @atamanroman
Also found this problem. I was checking if it wasn't fixed already.
Using the idea from @atamanroman I have modified the angular-select2
file, so that it can used for any element with or without a matcher function like:
// Check if options have any matcher function applied
if (opts.hasOwnProperty("matcher")) {
if (opts.matcher(query.term, label)) {
options.push({id:value, text:label, obj:values[key]});
}
} else if (label.toLowerCase().indexOf(query.term.toLowerCase()) > -1) {
options.push({id:value, text:label, obj:values[key]});
}
Hope it helps!