Skip to content

Commit

Permalink
add dropdown filter in table
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammad-nmeri committed Dec 18, 2024
1 parent 3ac060c commit 9070c26
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 4 deletions.
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

0 comments on commit 9070c26

Please sign in to comment.