diff --git a/lib/core/src/lib/datatable/components/columns-selector/columns-search-filter.pipe.spec.ts b/lib/core/src/lib/datatable/components/columns-selector/columns-search-filter.pipe.spec.ts new file mode 100644 index 0000000000..6df7f1a2b4 --- /dev/null +++ b/lib/core/src/lib/datatable/components/columns-selector/columns-search-filter.pipe.spec.ts @@ -0,0 +1,65 @@ +/*! + * @license + * Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { ColumnsSearchFilterPipe } from './columns-search-filter.pipe'; +import { DataColumn } from '../../data/data-column.model'; +import { NoopTranslateModule } from '@alfresco/adf-core'; +import { TestBed } from '@angular/core/testing'; + +describe('ColumnsSeearchFilterPipe', () => { + let pipe: ColumnsSearchFilterPipe; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [NoopTranslateModule, ColumnsSearchFilterPipe], + providers: [ColumnsSearchFilterPipe] + }); + + pipe = TestBed.inject(ColumnsSearchFilterPipe); + }); + + it('should filter columns', () => { + const columns: DataColumn[] = [ + { + title: 'Column 1', + key: '', + type: 'text' + }, + { + title: 'Column 2', + key: '', + type: 'number' + }, + { + title: 'Column 3', + key: '', + type: 'number' + } + ]; + + const filteredColumns = pipe.transform(columns, '1'); + + expect(filteredColumns.length).toBe(1); + expect(filteredColumns).toEqual([ + { + title: 'Column 1', + key: '', + type: 'text' + } + ]); + }); +}); diff --git a/lib/core/src/lib/datatable/components/columns-selector/columns-search-filter.pipe.ts b/lib/core/src/lib/datatable/components/columns-selector/columns-search-filter.pipe.ts new file mode 100644 index 0000000000..5091a5a393 --- /dev/null +++ b/lib/core/src/lib/datatable/components/columns-selector/columns-search-filter.pipe.ts @@ -0,0 +1,56 @@ +/*! + * @license + * Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { inject, Pipe, PipeTransform } from '@angular/core'; +import { DataColumn } from '../../data/data-column.model'; +import { TranslationService } from '../../../translation'; + +@Pipe({ + name: 'columnsSearchFilter', + standalone: true +}) +export class ColumnsSearchFilterPipe implements PipeTransform { + private translationService = inject(TranslationService); + + transform(columns: DataColumn[], searchByName: string): DataColumn[] { + const result = []; + + for (const column of columns) { + if (!column.title) { + continue; + } + + if (!searchByName) { + result.push(column); + continue; + } + + const title = this.translationService.instant(column.title); + + if (this.filterString(title, searchByName)) { + result.push(column); + } + } + + return result; + } + + private filterString(value: string = '', filterBy: string = ''): string { + const testResult = filterBy ? value.toLowerCase().indexOf(filterBy.toLowerCase()) > -1 : true; + return testResult ? value : ''; + } +} diff --git a/lib/core/src/lib/datatable/components/columns-selector/columns-selector.component.html b/lib/core/src/lib/datatable/components/columns-selector/columns-selector.component.html index 9e52d364ff..0c3699beb8 100644 --- a/lib/core/src/lib/datatable/components/columns-selector/columns-selector.component.html +++ b/lib/core/src/lib/datatable/components/columns-selector/columns-selector.component.html @@ -37,7 +37,7 @@