Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function recreateExpression(expression: IFilteringExpression, fields: FieldType[
}

if (!expression.condition && expression.conditionName) {
throw Error('Wrong `conditionName`, `condition` or `field` provided!');
throw Error('Wrong `conditionName`, `condition` or `field` provided! It is possible that there is a type mismatch between the condition type and field type.');
}

if (!expression.conditionName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ export interface IFilteringOperation {
hidden?: boolean;
/* blazorCSSuppress */
/* blazorAlternateType: FilteringOperationLogicHandler */
logic: (value: any, searchVal?: any, ignoreCase?: boolean) => boolean;
logic?: null | ((value: any, searchVal?: any, ignoreCase?: boolean) => boolean);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export enum FilteringLogic {
*/
export declare interface IFilteringExpression {
fieldName: string;
condition?: IFilteringOperation;
conditionName?: string;
condition?: IFilteringOperation | null;
conditionName?: string | null;
searchVal?: any;
searchTree?: IExpressionTree;
searchTree?: IExpressionTree | null;
ignoreCase?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export enum FilteringExpressionsTreeType {
export declare interface IExpressionTree {
filteringOperands: (IExpressionTree | IFilteringExpression)[];
operator: FilteringLogic;
fieldName?: string;
entity?: string;
returnFields?: string[];
fieldName?: string | null;
entity?: string | null;
returnFields?: (string | null)[] | null;
}

/* marshalByValue */
Expand Down
22 changes: 16 additions & 6 deletions projects/igniteui-angular/src/lib/grids/grid-base.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1856,8 +1856,8 @@ export abstract class IgxGridBaseDirective implements GridType,
}

value.type = FilteringExpressionsTreeType.Regular;
if (value && this.columns) {
this._filteringExpressionsTree = recreateTreeFromFields(value, this.columns) as IFilteringExpressionsTree;
if (value && this._columns?.length > 0) {
this._filteringExpressionsTree = recreateTreeFromFields(value, this._columns) as IFilteringExpressionsTree;
} else {
this._filteringExpressionsTree = value;
}
Expand Down Expand Up @@ -1906,7 +1906,11 @@ export abstract class IgxGridBaseDirective implements GridType,

if (value && isTree(value)) {
value.type = FilteringExpressionsTreeType.Advanced;
this._advancedFilteringExpressionsTree = recreateTreeFromFields(value, this.columns) as IFilteringExpressionsTree;
if (this._columns && this._columns.length > 0) {
this._advancedFilteringExpressionsTree = recreateTreeFromFields(value, this._columns) as IFilteringExpressionsTree;
} else {
this._advancedFilteringExpressionsTree = value;
}
this.filteringPipeTrigger++;
} else {
this._advancedFilteringExpressionsTree = null;
Expand Down Expand Up @@ -6607,6 +6611,9 @@ export abstract class IgxGridBaseDirective implements GridType,
if (this._columns && this._filteringExpressionsTree) {
this._filteringExpressionsTree = recreateTreeFromFields(this._filteringExpressionsTree, this.columns) as IFilteringExpressionsTree;
}
if (this._columns && this._advancedFilteringExpressionsTree) {
this._advancedFilteringExpressionsTree = recreateTreeFromFields(this._advancedFilteringExpressionsTree, this.columns) as IFilteringExpressionsTree;
}
this.resetCaches();
}

Expand Down Expand Up @@ -6668,9 +6675,12 @@ export abstract class IgxGridBaseDirective implements GridType,
this.autogenerateColumns();
} else {
this._columns = this.getColumnList();
if (this._columns && this._filteringExpressionsTree) {
this._filteringExpressionsTree = recreateTreeFromFields(this._filteringExpressionsTree, this._columns) as IFilteringExpressionsTree;
}
}
if (this._columns && this._filteringExpressionsTree) {
this._filteringExpressionsTree = recreateTreeFromFields(this._filteringExpressionsTree, this._columns) as IFilteringExpressionsTree;
}
if (this._columns && this._advancedFilteringExpressionsTree) {
this._advancedFilteringExpressionsTree = recreateTreeFromFields(this._advancedFilteringExpressionsTree, this._columns) as IFilteringExpressionsTree;
Comment on lines +6679 to +6683
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

At some point we might want to dedupe this into a private function we can call in all places :)

}

this.initColumns(this._columns, (col: IgxColumnComponent) => this.columnInit.emit(col));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
IgxGridAdvancedFilteringComponent,
IgxGridExternalAdvancedFilteringComponent,
IgxGridAdvancedFilteringBindingComponent,
IgxGridAdvancedFilteringDynamicColumnsComponent
IgxGridAdvancedFilteringDynamicColumnsComponent,
IgxGridAdvancedFilteringSerializedTreeComponent
} from '../../test-utils/grid-samples.spec';
import { FormattedValuesFilteringStrategy } from '../../data-operations/filtering-strategy';
import { IgxHierGridExternalAdvancedFilteringComponent } from '../../test-utils/hierarchical-grid-components.spec';
Expand Down Expand Up @@ -1389,6 +1390,52 @@ describe('IgxGrid - Advanced Filtering #grid - ', () => {
// Verify no filtered data
expect(grid.filteredData).toBe(null);
}));

});

describe('Expression tree rehydration - ', () => {
it('should correctly filter with a deserialized expression tree.', fakeAsync(() => {
const errorSpy = spyOn(console, 'error');
let fix = TestBed.createComponent(IgxGridAdvancedFilteringSerializedTreeComponent);
Comment thread
damyanpetev marked this conversation as resolved.
fix.detectChanges();
let grid = fix.componentInstance.grid;

expect(errorSpy).not.toHaveBeenCalled();

// Verify filtered data
expect(grid.filteredData.length).toEqual(3);
expect(grid.rowList.length).toBe(3);
}));

it('should correctly filter with a declared IFilteringExpressionsTree object.', fakeAsync(() => {
const errorSpy = spyOn(console, 'error');
let fix = TestBed.createComponent(IgxGridAdvancedFilteringSerializedTreeComponent);
fix.detectChanges();
fix.componentInstance.grid.advancedFilteringExpressionsTree = fix.componentInstance.filterTreeObject;
fix.detectChanges();
let grid = fix.componentInstance.grid;

expect(errorSpy).not.toHaveBeenCalled();

// Verify filtered data
expect(grid.filteredData.length).toEqual(2);
expect(grid.rowList.length).toBe(2);
}));

it('should correctly filter when binding to a declared IFilteringExpressionsTree object.', fakeAsync(() => {
const errorSpy = spyOn(console, 'error');
let fix = TestBed.createComponent(IgxGridAdvancedFilteringSerializedTreeComponent);
fix.detectChanges();
fix.componentInstance.filterTree = fix.componentInstance.filterTreeObject;
fix.detectChanges();
let grid = fix.componentInstance.grid;

expect(errorSpy).not.toHaveBeenCalled();

// Verify filtered data
expect(grid.filteredData.length).toEqual(2);
expect(grid.rowList.length).toBe(2);
}));
});
});

Expand Down
55 changes: 55 additions & 0 deletions projects/igniteui-angular/src/lib/test-utils/grid-samples.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2077,6 +2077,61 @@ export class IgxGridAdvancedFilteringBindingComponent extends BasicGridComponent
}
}

@Component({
template: `
<igx-grid [data]="data" height="500px" [allowAdvancedFiltering]="true" [(advancedFilteringExpressionsTree)]="filterTree">
<igx-column width="100px" [field]="'ID'" [header]="'ID'" [hasSummary]="true" [filterable]="false" [resizable]="resizable">
</igx-column>
<igx-column width="100px" [field]="'ProductName'" [filterable]="filterable" [resizable]="resizable" dataType="string"></igx-column>
<igx-column width="100px" [field]="'Downloads'" [filterable]="filterable" [resizable]="resizable" dataType="number"></igx-column>
<igx-column width="100px" [field]="'Released'" [filterable]="filterable" [resizable]="resizable" dataType="boolean"></igx-column>
</igx-grid>`,
imports: [IgxGridComponent, IgxColumnComponent]
})
export class IgxGridAdvancedFilteringSerializedTreeComponent extends BasicGridComponent implements OnInit {
public resizable = false;
public filterable = true;
public filterTree: IFilteringExpressionsTree;
public filterTreeObject: IFilteringExpressionsTree;

public override data = SampleTestData.excelFilteringData();

public ngOnInit(): void {
this.filterTree = JSON.parse(`{
"filteringOperands": [
{
"conditionName": "greaterThan",
"fieldName": "Downloads",
"searchVal": 200
}
],
"operator": 0
}`);

this.filterTreeObject = {
"filteringOperands": [
{
"fieldName": "ProductName",
"condition": {
"name": "contains",
"isUnary": false,
"iconName": "filter_contains"
},
"conditionName": "contains",
"ignoreCase": true,
"searchVal": "Ig",
"searchTree": null
}
],
"operator": 1,
"returnFields": [
"ID",
"ProductName"
]
};
}
}

@Component({
template: `
<igx-grid [data]="data" width="300px" height="250px">
Expand Down