Skip to content

Commit 96bd94d

Browse files
authored
Merge pull request #12358 from IgniteUI/mtsvyatkova/issue-12056
Define multiple keys for IgxFilterOptions
2 parents 2604f96 + c4d3db8 commit 96bd94d

File tree

4 files changed

+62
-15
lines changed

4 files changed

+62
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ All notable changes for each version of this project will be documented in this
5353
- `multi` mode - select/deselect all dates between the last selected/deselected and the one clicked while holding `Shift`.
5454
- `range` mode - extend/shorten the range from the last selected date to the one clicked while holding `Shift`.
5555

56+
- `IgxFilterOptions`
57+
- Added support for multiple keys.
58+
5659
### Theme Changes
5760
- **Breaking Changes** - The `palette` function no longer provides `info`, `success`, `warn` and `error` colors. Therefore you have to pass custom values for them if you need to use these colors. You can also use the values for `info`, `success`, `warn` and `error` colors from our predefined color palettes.
5861

projects/igniteui-angular/src/lib/directives/filter/filter.directive.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,36 @@ describe('Filter', () => {
7474
expect(list.items.length).toBe(4);
7575
});
7676

77+
it('should filter a list by multiple keys', () => {
78+
const fixture = TestBed.createComponent(DynamicListTestComponent);
79+
const list = fixture.componentInstance.list;
80+
81+
fixture.detectChanges();
82+
expect(list.items.length).toBe(4);
83+
const items = list.items;
84+
85+
for (const item of items) {
86+
expect(item instanceof IgxListItemComponent).toBeTruthy();
87+
}
88+
89+
fixture.componentInstance.fo.key = ['key', 'text'];
90+
fixture.componentInstance.filterValue = '1';
91+
fixture.detectChanges();
92+
93+
expect(list.items.length).toBe(1);
94+
expect(list.items[0] instanceof IgxListItemComponent).toBeTruthy();
95+
96+
fixture.componentInstance.filterValue = '';
97+
fixture.detectChanges();
98+
99+
expect(list.items.length).toBe(4);
100+
101+
fixture.componentInstance.filterValue = 'Nav3';
102+
fixture.detectChanges();
103+
104+
expect(list.items.length).toBe(1);
105+
});
106+
77107
it('should emit filter events on declaratively created list', () => {
78108
let visibleItems;
79109
const fixture = TestBed.createComponent(DeclarativeListTestComponent);

projects/igniteui-angular/src/lib/directives/filter/filter.directive.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ export class IgxFilterOptions {
1818
public inputValue = '';
1919

2020
// Item property, which value should be used for filtering
21-
public key: string;
21+
public key: string | string[];
2222

23-
// Represent items of the list. It should be used to handle decalaratevely defined widgets
23+
// Represent items of the list. It should be used to handle declaratively defined widgets
2424
public items: any[];
2525

2626
// Function - get value to be tested from the item
2727
// item - single item of the list to be filtered
2828
// key - property name of item, which value should be tested
29-
// Default behavior - returns "key"- named property value of item if key si provided,
29+
// Default behavior - returns "key"- named property value of item if key is provided,
3030
// otherwise textContent of the item's html element
3131
public get_value(item: any, key: string): string {
3232
let result = '';
@@ -125,6 +125,22 @@ export class IgxFilterDirective implements OnChanges {
125125
})
126126

127127
export class IgxFilterPipe implements PipeTransform {
128+
private findMatchByKey(item: any, options: IgxFilterOptions, key: string) {
129+
const match = options.matchFn(options.formatter(options.get_value(item, key)), options.inputValue);
130+
131+
if (match) {
132+
if (options.metConditionFn) {
133+
options.metConditionFn(item);
134+
}
135+
} else {
136+
if (options.overdueConditionFn) {
137+
options.overdueConditionFn(item);
138+
}
139+
}
140+
141+
return match;
142+
}
143+
128144
public transform(items: any[],
129145
// options - initial settings of filter functionality
130146
options: IgxFilterOptions) {
@@ -140,19 +156,17 @@ export class IgxFilterPipe implements PipeTransform {
140156
}
141157

142158
result = items.filter((item: any) => {
143-
const match = options.matchFn(options.formatter(options.get_value(item, options.key)), options.inputValue);
144-
145-
if (match) {
146-
if (options.metConditionFn) {
147-
options.metConditionFn(item);
148-
}
159+
if (!Array.isArray(options.key)) {
160+
return this.findMatchByKey(item, options, options.key);
149161
} else {
150-
if (options.overdueConditionFn) {
151-
options.overdueConditionFn(item);
152-
}
162+
let isMatch = false;
163+
options.key.forEach(key => {
164+
if (this.findMatchByKey(item, options, key)) {
165+
isMatch = true;
166+
}
167+
});
168+
return isMatch;
153169
}
154-
155-
return match;
156170
});
157171

158172
return result;

src/app/list/list.sample.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export class ListSampleComponent implements OnInit {
145145

146146
public get fo1() {
147147
const _fo = new IgxFilterOptions();
148-
_fo.key = 'text';
148+
_fo.key = ['text', 'key'];
149149
_fo.inputValue = this.search1;
150150
return _fo;
151151
}

0 commit comments

Comments
 (0)