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

Commit b4eed8a

Browse files
committed
feat(filterFilter): support deeply nested predicate objects
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
1 parent 08793a6 commit b4eed8a

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/Angular.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
-assertNotHasOwnProperty,
8282
-getter,
8383
-getBlockElements,
84+
-hasOwnProperty,
8485
8586
*/
8687

@@ -96,7 +97,7 @@
9697
* @returns {string} Lowercased string.
9798
*/
9899
var lowercase = function(string){return isString(string) ? string.toLowerCase() : string;};
99-
100+
var hasOwnProperty = Object.prototype.hasOwnProperty;
100101

101102
/**
102103
* @ngdoc function

src/ng/filter/filter.js

+9
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ function filterFilter() {
136136
};
137137
} else {
138138
comparator = function(obj, text) {
139+
if (obj && text && typeof obj === 'object' && typeof text === 'object') {
140+
for (var objKey in obj) {
141+
if (objKey.charAt(0) !== '$' && hasOwnProperty.call(obj, objKey) &&
142+
comparator(obj[objKey], text[objKey])) {
143+
return true;
144+
}
145+
}
146+
return false;
147+
}
139148
text = (''+text).toLowerCase();
140149
return (''+obj).toLowerCase().indexOf(text) > -1;
141150
};

test/ng/filter/filterSpec.js

+11
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ describe('Filter: filter', function() {
7070
});
7171

7272

73+
it('should support deep predicate objects', function() {
74+
var items = [{person: {name: 'John'}},
75+
{person: {name: 'Rita'}},
76+
{person: {name: 'Billy'}},
77+
{person: {name: 'Joan'}}];
78+
expect(filter(items, {person: {name: 'Jo'}}).length).toBe(2);
79+
expect(filter(items, {person: {name: 'Jo'}})).toEqual([
80+
{person: {name: 'John'}}, {person: {name: 'Joan'}}]);
81+
});
82+
83+
7384
it('should match any properties for given "$" property', function() {
7485
var items = [{first: 'tom', last: 'hevery'},
7586
{first: 'adam', last: 'hevery', alias: 'tom', done: false},

0 commit comments

Comments
 (0)