Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit 47a1ab7

Browse files
committed
fix(filter): should be able to filter even on hidden columns, fixes #310
1 parent 2593092 commit 47a1ab7

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

src/app/examples/grid-menu.component.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export class GridMenuComponent implements OnInit {
2626
gridOptions: GridOption;
2727
dataset: any[];
2828
selectedLanguage: string;
29-
visibleColumns: Column[];
3029

3130
constructor(private translate: TranslateService) {
3231
// always start with English for Cypress E2E tests to be consistent
@@ -60,8 +59,6 @@ export class GridMenuComponent implements OnInit {
6059
}
6160
];
6261

63-
this.visibleColumns = this.columnDefinitions;
64-
6562
this.gridOptions = {
6663
columnPicker: {
6764
hideForceFitButton: true,

src/app/modules/angular-slickgrid/services/__tests__/filter.service.spec.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import { Filters } from '../../filters';
1616
import { FilterService } from '../filter.service';
1717
import { FilterFactory } from '../../filters/filterFactory';
18+
import { SharedService } from '../shared.service';
1819
import { SlickgridConfig, CollectionService } from '../..';
1920
import { of, throwError } from 'rxjs';
2021

@@ -66,6 +67,7 @@ const gridStub = {
6667

6768
describe('FilterService', () => {
6869
let service: FilterService;
70+
let sharedService: SharedService;
6971
let slickgridEventHandler: SlickEventHandler;
7072

7173
beforeEach(async(() => {
@@ -79,13 +81,15 @@ describe('FilterService', () => {
7981
FilterService,
8082
CollectionService,
8183
FilterFactory,
84+
SharedService,
8285
SlickgridConfig,
8386
],
8487
imports: [
8588
TranslateModule.forRoot()
8689
]
8790
});
8891
service = TestBed.get(FilterService);
92+
sharedService = TestBed.get(SharedService);
8993
slickgridEventHandler = service.eventHandler;
9094
}));
9195

@@ -550,7 +554,7 @@ describe('FilterService', () => {
550554
mockItem1 = { firstName: 'John', lastName: 'Doe', fullName: 'John Doe', age: 26, address: { zip: 123456 } };
551555
});
552556

553-
it('should return False when there are no column definition found', () => {
557+
it('should return True (nothing to filter, all rows will be returned) when there are no column definition found', () => {
554558
const searchValue = 'John';
555559
const mockColumn1 = { id: 'firstName', field: 'firstName', filterable: true } as Column;
556560
jest.spyOn(gridStub, 'getColumns').mockReturnValue([]);
@@ -559,7 +563,7 @@ describe('FilterService', () => {
559563
const columnFilters = { firstName: { columnDef: mockColumn1, columnId: 'firstName', operator: 'EQ', searchTerms: [searchValue] } };
560564
const output = service.customLocalFilter(mockItem1, { dataView: dataViewStub, grid: gridStub, columnFilters });
561565

562-
expect(output).toBe(false);
566+
expect(output).toBe(true);
563567
});
564568

565569
it('should return True when input value from datacontext is the same as the searchTerms', () => {
@@ -574,6 +578,19 @@ describe('FilterService', () => {
574578
expect(output).toBe(true);
575579
});
576580

581+
it('should work on a hidden column by using the sharedService "allColumns" and return True when input value the same as the searchTerms', () => {
582+
const searchValue = 'John';
583+
const mockColumn1 = { id: 'firstName', field: 'firstName', filterable: true } as Column;
584+
sharedService.allColumns = [mockColumn1];
585+
jest.spyOn(gridStub, 'getColumns').mockReturnValue([]);
586+
587+
service.init(gridStub);
588+
const columnFilters = { firstName: { columnDef: mockColumn1, columnId: 'firstName', operator: 'EQ', searchTerms: [searchValue] } };
589+
const output = service.customLocalFilter(mockItem1, { dataView: dataViewStub, grid: gridStub, columnFilters });
590+
591+
expect(output).toBe(true);
592+
});
593+
577594
it('should return True when the searchTerms is an empty array', () => {
578595
const mockColumn1 = { id: 'firstName', field: 'firstName', filterable: true } as Column;
579596
jest.spyOn(gridStub, 'getColumns').mockReturnValue([mockColumn1]);
@@ -597,6 +614,19 @@ describe('FilterService', () => {
597614
expect(output).toBe(false);
598615
});
599616

617+
it('should work on a hidden column by using the sharedService "allColumns" and return return False when input value is not equal to the searchTerms', () => {
618+
const searchValue = 'Johnny';
619+
const mockColumn1 = { id: 'firstName', field: 'firstName', filterable: true } as Column;
620+
sharedService.allColumns = [mockColumn1];
621+
jest.spyOn(gridStub, 'getColumns').mockReturnValue([]);
622+
623+
service.init(gridStub);
624+
const columnFilters = { firstName: { columnDef: mockColumn1, columnId: 'firstName', operator: 'EQ', searchTerms: [searchValue] } };
625+
const output = service.customLocalFilter(mockItem1, { dataView: dataViewStub, grid: gridStub, columnFilters });
626+
627+
expect(output).toBe(false);
628+
});
629+
600630
it('should return True when input value from datacontext is a number and searchTerms is also a number', () => {
601631
const searchValue = 26;
602632
const mockColumn1 = { id: 'age', field: 'age', filterable: true } as Column;

src/app/modules/angular-slickgrid/services/filter.service.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { executeBackendCallback } from './backend-utilities';
2222
import { getDescendantProperty } from './utilities';
2323
import { FilterConditions } from './../filter-conditions';
2424
import { FilterFactory } from '../filters/filterFactory';
25+
import { SharedService } from './shared.service';
2526
import { Subject } from 'rxjs';
2627
import * as isequal_ from 'lodash.isequal';
2728
const isequal = isequal_; // patch to fix rollup to work
@@ -47,7 +48,7 @@ export class FilterService {
4748
onFilterChanged = new Subject<CurrentFilter[]>();
4849
onFilterCleared = new Subject<boolean>();
4950

50-
constructor(private filterFactory: FilterFactory) {
51+
constructor(private filterFactory: FilterFactory, private sharedService: SharedService) {
5152
this._eventHandler = new Slick.EventHandler();
5253
this._onSearchChange = new Slick.Event();
5354
}
@@ -238,10 +239,19 @@ export class FilterService {
238239
const dataView = args && args.dataView;
239240
for (const columnId of Object.keys(args.columnFilters)) {
240241
const columnFilter = args.columnFilters[columnId];
241-
const columnIndex = args.grid.getColumnIndex(columnId);
242-
const columnDef = args.grid.getColumns()[columnIndex];
242+
let columnIndex = args.grid.getColumnIndex(columnId);
243+
let columnDef = args.grid.getColumns()[columnIndex];
244+
245+
// it might be a hidden column, if so it won't be part of the getColumns (because it we hide a column via setColumns)
246+
// when that happens we can try to get the column definition from all defined columns
247+
if (!columnDef && this.sharedService && Array.isArray(this.sharedService.allColumns)) {
248+
columnIndex = this.sharedService.allColumns.findIndex((col) => col.field === columnId);
249+
columnDef = this.sharedService.allColumns[columnIndex];
250+
}
251+
252+
// if we still don't have a column definition then we should return then row anyway (true)
243253
if (!columnDef) {
244-
return false;
254+
return true;
245255
}
246256

247257
// Row Detail View plugin, if the row is padding we just get the value we're filtering on from it's parent

0 commit comments

Comments
 (0)