Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(platform): table filter enhancement #12864

Merged
merged 6 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -16,6 +16,7 @@ export interface BaseCollectionFilter<T> {
value2?: Nullable<T>;
exclude?: boolean;
strategy?: FilterStrategy;
fieldName?: string;
}
export interface CollectionStringFilter extends BaseCollectionFilter<string> {
strategy: FilterStringStrategy;
Expand Down
2 changes: 1 addition & 1 deletion libs/platform/table-helpers/services/table.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('TableServiceService', () => {
const setTableStateSpy = jest.spyOn(service, 'setTableState');
const filterChangeSpy = jest.spyOn(service.filterChange$, 'next');
const newFilterBy: CollectionStringFilter[] = [
{ field: 'name', value: 'Product name', strategy: FILTER_STRING_STRATEGY.CONTAINS }
{ field: 'name', value: 'Product name', fieldName: 'name', strategy: FILTER_STRING_STRATEGY.CONTAINS }
];
const newState: TableState = { ...DEFAULT_TABLE_STATE, filterBy: newFilterBy };
const event: FilterChange = { current: newFilterBy, previous: DEFAULT_TABLE_STATE.filterBy };
Expand Down
28 changes: 24 additions & 4 deletions libs/platform/table-helpers/services/table.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { skip } from 'rxjs/operators';
import { DEFAULT_TABLE_STATE } from '../constants';

import { toSignal } from '@angular/core/rxjs-interop';
import { Nullable } from '@fundamental-ngx/cdk/utils';
import { CollectionFilter, CollectionGroup, CollectionPage, CollectionSort, TableState } from '../interfaces';
import {
ColumnsChange,
Expand Down Expand Up @@ -188,7 +189,12 @@ export class TableService {
const prevState = this.getTableState();
const prevFilterRules = (prevState && prevState.filterBy) || [];

const newFilterRules: CollectionFilter[] = filterRules ? [...filterRules] : [];
const newFilterRules: CollectionFilter[] = filterRules
? filterRules.map((rule) => ({
...rule,
fieldName: this._getFieldName(rule.field, rule.fieldName)
}))
: [];
const state: TableState = { ...prevState, filterBy: newFilterRules };

if (!equal(prevFilterRules, state.filterBy)) {
Expand All @@ -209,7 +215,10 @@ export class TableService {

const newFilterRules: CollectionFilter[] = [
...prevFilterRules.filter((existing) => !rulesToAdd.find(({ field }) => field === existing.field)),
...rulesToAdd
...rulesToAdd.map((rule) => ({
...rule,
fieldName: this._getFieldName(rule.field, rule.fieldName)
}))
];

const state: TableState = { ...prevState, filterBy: newFilterRules };
Expand Down Expand Up @@ -427,9 +436,12 @@ export class TableService {
*/
buildFilterRulesMap(state = this.getTableState()): void {
const prevState = this.getTableState();
const evt = { current: state.filterBy, previous: prevState.filterBy };
const filterRulesWithFieldNames = state.filterBy.map((rule) => ({
...rule,
fieldName: this._getFieldName(rule.field, rule.fieldName)
}));
this.filterRules$.set(
state.filterBy.reduce((hash, rule) => {
filterRulesWithFieldNames.reduce((hash, rule) => {
const key = rule.field;
if (!hash.has(key)) {
hash.set(key, []);
Expand All @@ -438,6 +450,8 @@ export class TableService {
return hash;
}, new Map<string, CollectionFilter[]>())
);
const evt = { current: filterRulesWithFieldNames, previous: state.filterBy };

this.stateChange$.next({ type: 'filter', state: evt });
}

Expand Down Expand Up @@ -468,6 +482,12 @@ export class TableService {
this.buildSortRulesMap();
this.buildFilterRulesMap();
}

/** @hidden */
private _getFieldName(field: string, fieldName: Nullable<string>): string {
const column = this.tableColumns$.getValue().find((col) => col.key === field);
return column ? column.name : fieldName ?? field;
}
}

/** @hidden */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,15 @@
@if (appliedFilters().length) {
<fd-toolbar
fdType="info"
class="fdp-table__applied-filters-toolbar"
[active]="true"
[titleId]="tableToolbarTitleId"
[shouldOverflow]="shouldOverflow"
[headingLevel]="headingLevel"
>
<label fd-toolbar-label>
{{ 'platformTable.filteredBy' | fdTranslate }}:
@for (filter of appliedFilters(); track filter.columnName; let i = $index) {
{{ filter.columnName }} ({{ filter.params }})
@if (i < appliedFilters().length - 1) {
,
}
}
{{ _formatAppliedFilters(appliedFilters()) }}
</label>
<fd-toolbar-spacer></fd-toolbar-spacer>
<button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ export class TableToolbarComponent implements TableToolbarInterface {
this._table.expandAll();
}

/** @hidden */
_formatAppliedFilters(appliedFilters: { columnName: string; params: string }[]): string {
return appliedFilters
.map((filter, index) => {
const separator = index < appliedFilters.length - 1 ? ', ' : '';
return `${filter.columnName} (${filter.params})${separator}`;
mikerodonnell89 marked this conversation as resolved.
Show resolved Hide resolved
})
.join('');
}

/** @hidden */
_closeFilterToolbar(): void {
this._tableService.resetFilters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export class FilterStepComponent implements FiltersViewStep {
this._filterBy = filterByList.find(({ field }) => field === this.columnKey);
}

/** Column name to display in the filter */
@Input()
columnName: string;

/** Go back event */
@Output()
back: EventEmitter<void> = new EventEmitter<void>();
Expand Down Expand Up @@ -92,6 +96,7 @@ export class FilterStepComponent implements FiltersViewStep {
const filterBy: CollectionFilter = this._filterBy || {
field: this.columnKey,
value: null,
fieldName: this.columnName,
strategy: FILTER_STRATEGY.EQ,
exclude: false
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<fdp-filter-step
[columnKey]="activeFilterColumnKey!"
[filter]="activeFilter!"
[columnName]="activeFilter!.label"
[filterBy]="filterBy"
(back)="goToFilters()"
(valueChange)="applyFilter($event, activeFilter!)"
Expand Down
6 changes: 6 additions & 0 deletions libs/platform/table/table.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ fdk-dynamic-portal {
}
}

.fdp-table__applied-filters-toolbar {
label {
flex-shrink: 1;
}
}

.#{$block}__body-table,
.#{$block}__header-table,
.#{$block}__body-hs-table {
Expand Down
8 changes: 7 additions & 1 deletion libs/platform/table/table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1875,7 +1875,8 @@ export class TableComponent<T = any>
/** @hidden */
private _setAppliedFilterNames(filters: CollectionFilter[]): void {
const formattedFilters = filters.map((f) => ({
columnName: this._formatColumnName(f.field),
columnName: this._formatColumnName(f.fieldName || ''),

params: this._formatParams(f.value)
}));

Expand All @@ -1888,6 +1889,11 @@ export class TableComponent<T = any>
return String(value); // Handle non-object values
}

// handle array
if (Array.isArray(value)) {
return value.map((val) => this._formatParams(val)).join(', ');
}

return Object.entries(value)
.map(([key, val]) => `${key}: ${this._formatParams(val)}`) // Recursive call for nested objects
.join(', ');
Expand Down
2 changes: 1 addition & 1 deletion libs/platform/table/tests/table.component-host.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ describe('TableComponent Host', () => {
describe('data source', () => {
it('fetch should be triggered when call table.filter()', () => {
const filterBy: CollectionFilter[] = [
{ field: 'status', value: 'valid', strategy: 'equalTo', exclude: false }
{ field: 'status', value: 'valid', strategy: 'equalTo', fieldName: 'status', exclude: false }
];

tableComponent.filter(filterBy);
Expand Down
Loading