Skip to content

Commit 211b0a1

Browse files
authored
Merge pull request #5101 from IgniteUI/filtering-refactor
chore(*): Refactoring filtering-condition code
2 parents f7808b7 + fcb9a24 commit 211b0a1

File tree

6 files changed

+46
-35
lines changed

6 files changed

+46
-35
lines changed

azure-pipelines.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ steps:
1414
displayName: 'Build source code and styles'
1515

1616
- script: npm run test:lib:azure
17-
displayName: 'Run non-grid tests'
17+
displayName: 'Run tests'
1818
env:
1919
NODE_OPTIONS: "--max_old_space_size=4096"
2020

projects/igniteui-angular/src/lib/data-operations/filtering-condition.spec.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { IgxStringFilteringOperand,
22
IgxNumberFilteringOperand,
33
IgxDateFilteringOperand,
4-
IgxBooleanFilteringOperand } from './filtering-condition';
4+
IgxBooleanFilteringOperand,
5+
IgxFilteringOperand} from './filtering-condition';
56

67
describe('Unit testing FilteringCondition', () => {
78
it('tests string conditions', () => {
@@ -138,4 +139,23 @@ describe('Unit testing FilteringCondition', () => {
138139
expect(!f.condition('notNull').logic(null) && f.condition('notNull').logic(undefined) && f.condition('notNull').logic(false))
139140
.toBeTruthy('notNull');
140141
});
142+
it('tests custom conditions', () => {
143+
const f = CustomFilter.instance();
144+
expect(f.condition('Custom').logic('Asd', 'asd')).toBeFalsy();
145+
expect(f.condition('Custom').logic('Asd', 'Asd')).toBeTruthy();
146+
});
141147
});
148+
149+
class CustomFilter extends IgxFilteringOperand {
150+
private constructor() {
151+
super();
152+
this.append({
153+
name: 'Custom',
154+
logic: (value: any, searchVal: any, ignoreCase: boolean) => {
155+
return value === searchVal;
156+
},
157+
isUnary: false,
158+
iconName: 'starts_with'
159+
});
160+
}
161+
}

projects/igniteui-angular/src/lib/data-operations/filtering-condition.ts

+5-24
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @export
66
*/
77
export class IgxFilteringOperand {
8+
protected static _instance: IgxFilteringOperand = null;
89
public operations: IFilteringOperation[];
910

1011
public constructor() {
@@ -25,6 +26,10 @@ export class IgxFilteringOperand {
2526
}];
2627
}
2728

29+
public static instance(): IgxFilteringOperand {
30+
return this._instance || (this._instance = new this());
31+
}
32+
2833
public conditionList(): string[] {
2934
return this.operations.map((element) => element.name);
3035
}
@@ -44,8 +49,6 @@ export class IgxFilteringOperand {
4449
* @export
4550
*/
4651
export class IgxBooleanFilteringOperand extends IgxFilteringOperand {
47-
private static _instance: IgxBooleanFilteringOperand = null;
48-
4952
protected constructor() {
5053
super();
5154
this.operations = [{
@@ -85,10 +88,6 @@ export class IgxBooleanFilteringOperand extends IgxFilteringOperand {
8588
}
8689
}].concat(this.operations);
8790
}
88-
89-
public static instance(): IgxBooleanFilteringOperand {
90-
return this._instance || (this._instance = new this());
91-
}
9291
}
9392

9493
/**
@@ -97,8 +96,6 @@ export class IgxBooleanFilteringOperand extends IgxFilteringOperand {
9796
* @export
9897
*/
9998
export class IgxDateFilteringOperand extends IgxFilteringOperand {
100-
private static _instance: IgxDateFilteringOperand = null;
101-
10299
protected constructor() {
103100
super();
104101
this.operations = [{
@@ -318,10 +315,6 @@ export class IgxDateFilteringOperand extends IgxFilteringOperand {
318315
}].concat(this.operations);
319316
}
320317

321-
public static instance(): IgxDateFilteringOperand {
322-
return this._instance || (this._instance = new this());
323-
}
324-
325318
/**
326319
* Splits a Date object into parts
327320
*
@@ -377,8 +370,6 @@ export class IgxDateFilteringOperand extends IgxFilteringOperand {
377370
* @export
378371
*/
379372
export class IgxNumberFilteringOperand extends IgxFilteringOperand {
380-
private static _instance: IgxNumberFilteringOperand = null;
381-
382373
protected constructor() {
383374
super();
384375
this.operations = [{
@@ -439,10 +430,6 @@ export class IgxNumberFilteringOperand extends IgxFilteringOperand {
439430
}
440431
}].concat(this.operations);
441432
}
442-
443-
public static instance(): IgxNumberFilteringOperand {
444-
return this._instance || (this._instance = new this());
445-
}
446433
}
447434

448435
/**
@@ -451,8 +438,6 @@ export class IgxNumberFilteringOperand extends IgxFilteringOperand {
451438
* @export
452439
*/
453440
export class IgxStringFilteringOperand extends IgxFilteringOperand {
454-
private static _instance: IgxStringFilteringOperand = null;
455-
456441
protected constructor() {
457442
super();
458443
this.operations = [{
@@ -526,10 +511,6 @@ export class IgxStringFilteringOperand extends IgxFilteringOperand {
526511
}].concat(this.operations);
527512
}
528513

529-
public static instance(): IgxStringFilteringOperand {
530-
return this._instance || (this._instance = new this());
531-
}
532-
533514
/**
534515
* Applies case sensitivity on strings if provided
535516
*

projects/igniteui-angular/src/lib/test-utils/grid-samples.spec.ts

-6
Original file line numberDiff line numberDiff line change
@@ -894,8 +894,6 @@ export class SelectionWithTransactionsComponent extends BasicGridComponent {
894894
}
895895

896896
export class CustomFilter extends IgxFilteringOperand {
897-
private static _instance: CustomFilter;
898-
899897
private constructor () {
900898
super();
901899
this.operations = [{
@@ -907,10 +905,6 @@ export class CustomFilter extends IgxFilteringOperand {
907905
iconName: 'custom'
908906
}];
909907
}
910-
911-
public static instance(): CustomFilter {
912-
return this._instance || (this._instance = new this());
913-
}
914908
}
915909
@Component({
916910
template: `<igx-grid [data]="data" height="500px" [allowFiltering]='true'>

src/app/grid/grid.sample.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ <h4>{{ col.field }}</h4>
139139
</div>
140140
</div>
141141

142-
<igx-grid #grid2 [data]="data | async" [paging]="true" [perPage]="10" (onCellSelection)="onInlineEdit($event)">
142+
<igx-grid #grid2 [data]="data | async" [paging]="true" [perPage]="10" [allowFiltering]="true" (onCellSelection)="onInlineEdit($event)">
143143
<igx-column [sortable]="true" [field]="'ProductID'" [header]="'ID'"></igx-column>
144-
<igx-column [sortable]="true" [field]="'ProductName'"></igx-column>
144+
<igx-column [sortable]="true" [field]="'ProductName'" [filters]="customFilter" [filterable]="true"></igx-column>
145145
<igx-column [sortable]="true" [field]="'UnitsInStock'" [header]="'In Stock'">
146146
<!--
147147
<ng-template igxCell let-col="column" let-ri="rowIndex" let-item="item">
@@ -188,7 +188,7 @@ <h4 class="sample-title">Grid with templated cell remote data</h4>
188188
</div>
189189
<igx-grid #grid3 displayDensity="compact" [data]="remote | async" [paging]="true" [perPage]="10" (onPagingDone)="process($event)" (onSortingDone)="process($event)" [primaryKey]="'ProductID'" [rowSelectable]="true">
190190
<igx-column [sortable]="true" [field]="'ProductID'" ></igx-column>
191-
<igx-column [editable]="true" [field]="'ProductName'" ></igx-column>
191+
<igx-column [editable]="true" [field]="'ProductName'"></igx-column>
192192
<igx-column [sortable]="true" [field]="'UnitsInStock'" [header]="'Units in Stock'"></igx-column>
193193
<igx-column [field]="'Discontinued'" [sortable]="true" [hidden]="true">
194194
<ng-template igxHeader let-column="column">

src/app/grid/grid.sample.ts

+16
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export class GridSampleComponent implements OnInit, AfterViewInit {
4949
newRecord = '';
5050
editCell;
5151
exportFormat = 'XLSX';
52+
customFilter = CustomStringFilter.instance();
5253

5354
constructor(private localService: LocalService,
5455
private remoteService: RemoteService,
@@ -282,3 +283,18 @@ export class GridSampleComponent implements OnInit, AfterViewInit {
282283
column.editable = true;
283284
}
284285
}
286+
287+
export class CustomStringFilter extends IgxStringFilteringOperand {
288+
289+
private constructor() {
290+
super();
291+
this.append({
292+
name: 'Custom',
293+
logic: (value: any, searchVal: any, ignoreCase: boolean) => {
294+
return value === searchVal;
295+
},
296+
isUnary: false,
297+
iconName: 'starts_with'
298+
});
299+
}
300+
}

0 commit comments

Comments
 (0)