Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
feat(filterFilter): support deeply nested predicate objects
Browse files Browse the repository at this point in the history
Due to 339a165, it became impossible to filter nested properties of an object using the filterFilter.
A proposed solution to this was to enable the use of nested predicate objects. This change enables the
use of these nested predicate objects.

Example:

```html
<div ng-repeat="it in items | filter:{ address: { country: 'Canuckistan'}}"></div>
```

Or

```js
$filter('filter')(items, { address: { country: 'Canuckistan' } });
```

Closes #6215
Related to #6009
  • Loading branch information
caitp committed Feb 11, 2014
1 parent 08793a6 commit b4eed8a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
-assertNotHasOwnProperty,
-getter,
-getBlockElements,
-hasOwnProperty,
*/

Expand All @@ -96,7 +97,7 @@
* @returns {string} Lowercased string.
*/
var lowercase = function(string){return isString(string) ? string.toLowerCase() : string;};

var hasOwnProperty = Object.prototype.hasOwnProperty;

/**
* @ngdoc function
Expand Down
9 changes: 9 additions & 0 deletions src/ng/filter/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ function filterFilter() {
};
} else {
comparator = function(obj, text) {
if (obj && text && typeof obj === 'object' && typeof text === 'object') {
for (var objKey in obj) {
if (objKey.charAt(0) !== '$' && hasOwnProperty.call(obj, objKey) &&
comparator(obj[objKey], text[objKey])) {
return true;
}
}
return false;
}
text = (''+text).toLowerCase();
return (''+obj).toLowerCase().indexOf(text) > -1;
};
Expand Down
11 changes: 11 additions & 0 deletions test/ng/filter/filterSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ describe('Filter: filter', function() {
});


it('should support deep predicate objects', function() {
var items = [{person: {name: 'John'}},
{person: {name: 'Rita'}},
{person: {name: 'Billy'}},
{person: {name: 'Joan'}}];
expect(filter(items, {person: {name: 'Jo'}}).length).toBe(2);
expect(filter(items, {person: {name: 'Jo'}})).toEqual([
{person: {name: 'John'}}, {person: {name: 'Joan'}}]);
});


it('should match any properties for given "$" property', function() {
var items = [{first: 'tom', last: 'hevery'},
{first: 'adam', last: 'hevery', alias: 'tom', done: false},
Expand Down

1 comment on commit b4eed8a

@gajender1995
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks man it's working , am using injector
var var1= $filter('filter')($scope.welcome, {x_axis: { date :'17/9/2016'}});

Please sign in to comment.