Skip to content

Commit 8092b69

Browse files
committed
fix(grid): Remove column filter when remove a column #5494
1 parent fb2450f commit 8092b69

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

projects/igniteui-angular/src/lib/grids/api.service.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,6 @@ export class GridBaseAPIService <T extends IgxGridBaseComponent & IGridDataBinda
328328
}
329329

330330
public clear_filter(fieldName: string) {
331-
if (fieldName) {
332-
const column = this.get_column_by_name(fieldName);
333-
if (!column) {
334-
return;
335-
}
336-
}
337-
338331
const grid = this.grid;
339332
grid.endEdit(false);
340333
const filteringState = grid.filteringExpressionsTree;

projects/igniteui-angular/src/lib/grids/filtering/grid-filtering.service.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,10 @@ export class IgxFilteringService implements OnDestroy {
218218
if (!this.columnToExpressionsMap.has(columnId)) {
219219
const column = this.grid.columns.find((col) => col.field === columnId);
220220
const expressionUIs = new Array<ExpressionUI>();
221-
222-
this.generateExpressionsList(column.filteringExpressionsTree, this.grid.filteringExpressionsTree.operator, expressionUIs);
223-
this.columnToExpressionsMap.set(columnId, expressionUIs);
224-
221+
if (column) {
222+
this.generateExpressionsList(column.filteringExpressionsTree, this.grid.filteringExpressionsTree.operator, expressionUIs);
223+
this.columnToExpressionsMap.set(columnId, expressionUIs);
224+
}
225225
return expressionUIs;
226226
}
227227

@@ -322,7 +322,7 @@ export class IgxFilteringService implements OnDestroy {
322322
}
323323

324324
const column = this.grid.columns.find((col) => col.field === columnId);
325-
const isComplex = this.isFilteringTreeComplex(column.filteringExpressionsTree);
325+
const isComplex = column && this.isFilteringTreeComplex(column.filteringExpressionsTree);
326326
if (isComplex) {
327327
this.columnsWithComplexFilter.add(columnId);
328328
}

projects/igniteui-angular/src/lib/grids/grid/column.spec.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { IgxGridComponent } from './grid.component';
55
import { IgxGridModule } from './index';
66
import { GridTemplateStrings, ColumnDefinitions } from '../../test-utils/template-strings.spec';
77
import { SampleTestData } from '../../test-utils/sample-test-data.spec';
8-
import { ColumnHiddenFromMarkupComponent, ColumnCellFormatterComponent } from '../../test-utils/grid-samples.spec';
8+
import { ColumnHiddenFromMarkupComponent, ColumnCellFormatterComponent, DynamicColumnsComponent } from '../../test-utils/grid-samples.spec';
99
import { wait } from '../../test-utils/ui-interactions.spec';
1010
import { configureTestSuite } from '../../test-utils/configure-suite';
1111
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
12+
import { IgxStringFilteringOperand } from 'igniteui-angular';
1213

1314
describe('IgxGrid - Column properties', () => {
1415
configureTestSuite();
@@ -24,7 +25,8 @@ describe('IgxGrid - Column properties', () => {
2425
TemplatedInputColumnsComponent,
2526
ColumnCellFormatterComponent,
2627
ColumnHaederClassesComponent,
27-
ColumnHiddenFromMarkupComponent
28+
ColumnHiddenFromMarkupComponent,
29+
DynamicColumnsComponent
2830
],
2931
imports: [IgxGridModule, NoopAnimationsModule]
3032
})
@@ -301,6 +303,31 @@ describe('IgxGrid - Column properties', () => {
301303
expect(grid.getCellByColumn(i, 'Name').value).toBe(expectedVal[i]);
302304
}
303305
});
306+
307+
it('should remove filter when a columns is removed dynamically', () => {
308+
const fix = TestBed.createComponent(DynamicColumnsComponent);
309+
fix.detectChanges();
310+
311+
const columns = fix.componentInstance.columns;
312+
const grid = fix.componentInstance.grid;
313+
grid.allowFiltering = true;
314+
fix.detectChanges();
315+
316+
expect(grid.columns.length).toBe(7);
317+
318+
grid.filter('CompanyName', 'NoItemsFound', IgxStringFilteringOperand.instance().condition('contains'), true);
319+
fix.detectChanges();
320+
321+
expect(grid.rowList.length).toEqual(0);
322+
323+
expect(() => {
324+
fix.componentInstance.columns = columns.slice(2, columns.length - 1);
325+
fix.detectChanges();
326+
}).not.toThrow();
327+
328+
expect(grid.rowList.length).toBeGreaterThan(10);
329+
expect(grid.columns.length).toBe(4);
330+
});
304331
});
305332

306333
@Component({

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,3 +1099,23 @@ export class IgxGridClipboardComponent extends BasicGridComponent {
10991099
public data = SampleTestData.excelFilteringData();
11001100
formatter = (value: any) => `** ${value} **`;
11011101
}
1102+
1103+
@Component({
1104+
template: GridTemplateStrings.declareGrid(` [height]="height" [width]="width"`, ``,
1105+
ColumnDefinitions.generatedWithDataType)
1106+
})
1107+
export class DynamicColumnsComponent extends GridWithSizeComponent {
1108+
public columns = [
1109+
{ field: 'ID', width: 100 , dataType: 'number'},
1110+
{ field: 'CompanyName', width: 300 , dataType: 'string'},
1111+
{ field: 'ContactName', width: 200 , dataType: 'string'},
1112+
{ field: 'ContactTitle', width: 200 , dataType: 'string'},
1113+
{ field: 'Address', width: 300 , dataType: 'string'},
1114+
{ field: 'City', width: 100 , dataType: 'string'},
1115+
{ field: 'Region', width: 100 , dataType: 'string'}
1116+
];
1117+
1118+
data = SampleTestData.contactInfoDataFull();
1119+
width = '800px';
1120+
height = '800px';
1121+
}

0 commit comments

Comments
 (0)