This repository was archived by the owner on Jun 1, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathcustom-angularComponentFilter.ts
More file actions
125 lines (110 loc) · 4.4 KB
/
custom-angularComponentFilter.ts
File metadata and controls
125 lines (110 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import type { ComponentRef } from '@angular/core';
import type { Subscription } from 'rxjs';
import {
AngularUtilService,
type Column,
type ColumnFilter,
type Filter,
type FilterArguments,
type FilterCallback,
type GridOption,
OperatorType,
type OperatorString,
type SearchTerm,
type SlickGrid,
unsubscribeAllObservables,
} from './../modules/angular-slickgrid';
export class CustomAngularComponentFilter implements Filter {
private _shouldTriggerQuery = true;
private _subscriptions: Subscription[] = [];
/** Angular Component Reference */
componentRef!: ComponentRef<any>;
grid!: SlickGrid;
searchTerms: SearchTerm[] = [];
columnDef!: Column;
callback!: FilterCallback;
operator: OperatorType | OperatorString = OperatorType.equal;
/** Angular Util Service (could be inside the Grid Options Params or the Filter Params ) */
get angularUtilService(): AngularUtilService {
let angularUtilService = this.gridOptions?.params?.angularUtilService;
if (!angularUtilService || !(angularUtilService instanceof AngularUtilService)) {
angularUtilService = this.columnFilter?.params?.angularUtilService;
}
return angularUtilService;
}
/** Get the Collection */
get collection(): any[] {
return this.columnFilter?.collection || [];
}
/** Getter for the Column Filter */
get columnFilter(): ColumnFilter {
return (this.columnDef && this.columnDef.filter) || {};
}
/** Getter for the Grid Options pulled through the Grid Object */
get gridOptions(): GridOption {
return (this.grid?.getOptions() ?? {}) as GridOption;
}
/**
* Initialize the Filter
*/
init(args: FilterArguments) {
this.grid = args.grid as SlickGrid;
this.callback = args.callback;
this.columnDef = args.columnDef;
this.searchTerms = ('searchTerms' in args ? args.searchTerms : []) || [];
if (!this.columnFilter || !this.columnFilter.params.component || !(this.angularUtilService instanceof AngularUtilService)) {
throw new Error(`[Angular-Slickgrid] For Filter with Angular Component to work properly, you need to provide the "AngularUtilService" via the Filter "params" OR the Grid Options "params"
Example: this.columnDefs = [{ id: 'title', field: 'title', filter: { model: CustomFilter, collection: [...], params: { component: MyComponent, angularUtilService: this.angularUtilService }}];
OR this.columnDefs = [{ id: 'title', field: 'title', filter: { model: CustomFilter, collection: [...] }]; this.gridOptions = { params: { angularUtilService: this.angularUtilService }}`);
}
if (this.columnFilter?.params.component) {
// use a delay to make sure Angular ran at least a full cycle and it finished rendering the Component before hooking onto it
// else we get the infamous error "ExpressionChangedAfterItHasBeenCheckedError"
const headerElm = this.grid.getHeaderRowColumn(this.columnDef.id);
if (headerElm) {
headerElm.innerHTML = '';
const componentOuput = this.angularUtilService.createAngularComponentAppendToDom(
this.columnFilter.params.component,
headerElm,
{ collection: this.collection }
);
this.componentRef = componentOuput.componentRef;
this._subscriptions.push(
componentOuput.componentRef.instance.onItemChanged.subscribe((item: any) => {
this.callback(undefined, {
columnDef: this.columnDef,
operator: this.operator,
searchTerms: [item.id],
shouldTriggerQuery: this._shouldTriggerQuery,
});
// reset flag for next use
this._shouldTriggerQuery = true;
})
);
}
}
}
/**
* Clear the filter value
*/
clear(shouldTriggerQuery = true) {
this._shouldTriggerQuery = shouldTriggerQuery;
if (this.componentRef?.instance && 'selectedId' in this.componentRef.instance) {
this.componentRef.instance.selectedId = 0;
}
}
/** destroy the Angular Component & Subscription */
destroy() {
if (this.componentRef?.destroy) {
this.componentRef.destroy();
}
// also unsubscribe all Angular Subscriptions
unsubscribeAllObservables(this._subscriptions);
}
/** Set value(s) on the DOM element */
setValues(values: SearchTerm[] | SearchTerm) {
if (this.componentRef?.instance && 'selectedId' in this.componentRef.instance) {
this.componentRef.instance.selectedId = values;
}
}
}