angular-select2 icon indicating copy to clipboard operation
angular-select2 copied to clipboard

Matcher is hardcoded

Open atamanroman opened this issue 9 years ago • 3 comments

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
    });
};

atamanroman avatar Jan 18 '16 16:01 atamanroman

I ran into this problem as well. Thanks for pointing out the fix @atamanroman

chadwithuhc avatar Mar 17 '16 21:03 chadwithuhc

Also found this problem. I was checking if it wasn't fixed already.

aalecs avatar Jun 22 '16 15:06 aalecs

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!

palashmon avatar May 11 '17 13:05 palashmon