Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matcher is hardcoded #38

Open
atamanroman opened this issue Jan 18, 2016 · 3 comments
Open

Matcher is hardcoded #38

atamanroman opened this issue Jan 18, 2016 · 3 comments

Comments

@atamanroman
Copy link

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
    });
};
@chadwithuhc
Copy link

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

@aalecs
Copy link

aalecs commented Jun 22, 2016

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

@palashmon
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants