Julius
Julius

Reputation: 2483

AngularJS ng-repeat with filter on two array keys

I'm using ng-repeat to display an array, and using the filter. My array has many keys, and I was able to apply filter to one specific key:

ng-repeat="project in projects = (list | filter: { name: filter }) | orderBy: 'name'"

But when I describe a filter, it should display array entries where the string from filter is found either in name or description.

Any suggestions?

SOLVED

http://plnkr.co/edit/scuPYt?p=preview

Upvotes: 1

Views: 540

Answers (1)

Chris Southam
Chris Southam

Reputation: 582

You'll need to create a search filter in your $scope for that controller:

$scope.searchFilter = function (project) {
    var keyword = new RegExp($scope.filter, 'i');
    return !$scope.filter || keyword.test(project.name) || keyword.test(project.description);
};

And then change the ng-repeat to:

ng-repeat="project in projects | filter: searchFilter) | orderBy: 'name'"

You may need to add list back in - not come across that before.

Upvotes: 1

Related Questions