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

feat(platform): add dropdown menu for predefined filter options #12859

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -17,14 +17,16 @@
>
<fdp-table-toolbar title="Order Line Items" [hideItemCount]="false" [hideSearchInput]="true"> </fdp-table-toolbar>

<!-- Example of Array Filter-->
<fdp-column
name="name"
key="name"
label="Name"
align="start"
[filterValuesOptions]="options_demo"
[sortable]="true"
[filterable]="true"
[dataType]="dataTypeEnum.STRING"
[dataType]="dataTypeEnum.ARRAY"
>
</fdp-column>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { delay } from 'rxjs/operators';
]
})
export class AdvancedScrollingExampleComponent {
options_demo=['Laptops 1 (Level 1)','Laptops 12 (Level 1)'];
source: TableDataSource<ExampleItem>;
childSource: ChildTableDataSource<ExampleItem>;
readonly filterTypeEnum = FilterType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export const getFilterStrategiesBasedOnDataType = (
dataType: FilterableColumnDataType
): readonly FilterStringStrategy[] | readonly FilterDateStrategy[] => {
switch (dataType) {
case FilterableColumnDataType.ARRAY:
case FilterableColumnDataType.STRING:
return FILTER_STRING_STRATEGIES;
case FilterableColumnDataType.NUMBER:
Expand Down
3 changes: 2 additions & 1 deletion libs/platform/table-helpers/enums/filter-type.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export enum FilterableColumnDataType {
STRING = 'string',
NUMBER = 'number',
BOOLEAN = 'boolean',
DATE = 'date'
DATE = 'date',
ARRAY = 'array',
}
3 changes: 3 additions & 0 deletions libs/platform/table-helpers/table-column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export abstract class TableColumn {
/** Data type the column represents. */
abstract dataType: FilterableColumnDataType;

/** Optional Array of available filter values. */
abstract filterValuesOptions: string[];

/** Width of the column cells. */
abstract width: string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ export class TableColumnComponent extends TableColumn implements OnInit, OnChang
@Input()
dataType: FilterableColumnDataType = FilterableColumnDataType.STRING;

/** Array of available filter options */
@Input()
filterValuesOptions: string[] = [];

/** Toggles grouping feature for the column. */
@Input()
groupable = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@
{{ 'platformTable.P13FilterBooleanOptionFalse' | fdTranslate }}
</li>
</fd-select>
} @else if (rule.dataType === DATA_TYPE.ARRAY && rule.filterValuesOptions?.length) {
<!-- Filter options dropdown menu -->
<fd-select
[ngModel]="rule[valueKey]"
(ngModelChange)="rule.setValue($event);_onModelChange()"
[required]="true"
[name]="valueKey"
class="filter-row__select">
@for (option of rule.filterValuesOptions; track $index) {
<li fd-option [value]="option">{{ option }}</li>
}
</fd-select>
} @else {
<input
type="text"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface FilterableColumn {
key: string;
dataType: FilterableColumnDataType;
filterable?: boolean;
filterValuesOptions?: string[]; // add optional filterValuesOptions
}

export interface FilterDialogData extends TableDialogCommonData {
Expand Down Expand Up @@ -39,6 +40,9 @@ export class FilterRule<T = any> {
/** Data type */
dataType?: FilterableColumnDataType;

/** Available filter Options */
filterValuesOptions: string[] = [];

/** returns whether filter rule has value */
get hasValue(): boolean {
return this.valueExists(this.value) || this.valueExists(this.value2);
Expand All @@ -65,6 +69,9 @@ export class FilterRule<T = any> {
if (this.strategies.length === 0) {
this.setStrategiesByColumnKey(this.columnKey);
}
if (this.filterValuesOptions.length === 0) {
this.setFilterValuesOptionsByColumnKey(this.columnKey);
}
}

/** @hidden */
Expand Down Expand Up @@ -121,6 +128,9 @@ export class FilterRule<T = any> {
// update data type
this.setDataTypeByColumnKey(columnKey);

// update available Filter options
this.setFilterValuesOptionsByColumnKey(columnKey);

// update available strategies list
this.setStrategiesByColumnKey(columnKey);
}
Expand All @@ -132,12 +142,25 @@ export class FilterRule<T = any> {
if (dataType === this.dataType) {
return;
}

this.dataType = dataType;
}

/** @hidden */
setFilterValuesOptionsByColumnKey(columnKey?: string): void {
const filterValuesOptions = this.columns.find((column) => column.key === columnKey)?.filterValuesOptions;
if (this.areArraysEqual(filterValuesOptions,this.filterValuesOptions)) {
return;
}
this.filterValuesOptions = filterValuesOptions ? filterValuesOptions : [];
}

/** @hidden */
private valueExists(value: any): boolean {
return !!value || value === 0 || typeof value === 'boolean';
}

/** @hidden */
private areArraysEqual(arr1: undefined|string[], arr2: undefined|string[]): boolean {
return JSON.stringify(arr1) === JSON.stringify(arr2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ export class TableP13DialogComponent implements OnDestroy {
const columns = this._getTableColumns();
const filterBy = state?.filterBy;
const dialogData: FilterDialogData = {
columns: columns.map(({ label, key, dataType, filterable }) => ({ label, key, dataType, filterable })),
// add filterValuesOptions to be sent with the data
columns: columns.map(({ label, key, dataType, filterable,filterValuesOptions }) => ({ label, key, dataType, filterable,filterValuesOptions })),
collectionFilter: filterBy
};

Expand Down
Loading