Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit 6831c53

Browse files
committed
fix: Filter model is now FilterConstructor and shouldn't be newed
- the Filter `model` prop should be a constructor and it shouldn't newed (instantiated), it was defined like so in the docs and Uncyclos but that was actually wrong - ref Slickgrid-Universal [PR 1430](ghiscoding/slickgrid-universal#1430)
1 parent b5d6191 commit 6831c53

File tree

7 files changed

+22
-22
lines changed

7 files changed

+22
-22
lines changed

docs/column-functionalities/filters/custom-filter.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ You can also create your own Custom Filter with any html/css you want and/or jQu
1616
#### Limitations
1717
- as mentioned in the description, only html/css and/or jQuery libraries are supported.
1818
- this mainly mean that Angular templates (components) are not supported (feel free to contribute).
19-
- SlickGrid uses `table-cell` as CSS for it to display a consistent height for each rows (this keeps the same row height/line-height to always be the same).
19+
- SlickGrid uses `table-cell` as CSS for it to display a consistent height for each rows (this keeps the same row height/line-height to always be the same).
2020
- all this to say that you might be in a situation were your filter shows in the back of the grid. The best approach to overcome this is to use a modal if you can or if the library support `append to body container`. For example, you can see that `multiple-select-vanilla` supports a `container`
2121

2222
### How to use Custom Filter?
23-
1. You first need to create a `class` using the [Filter interface](https://github.com/ghiscoding/slickgrid-universal/blob/master/packages/common/src/interfaces/filter.interface.ts). Make sure to create all necessary public properties and functions.
23+
1. You first need to create a `class` using the [Filter interface](https://github.com/ghiscoding/slickgrid-universal/blob/master/packages/common/src/interfaces/filter.interface.ts). Make sure to create all necessary public properties and functions.
2424
- You can see a demo with a [custom-inputFilter.ts](https://github.com/ghiscoding/angular-slickgrid/blob/master/src/app/examples/custom-inputFilter.ts) that is used in the [demo - example 4](https://ghiscoding.github.io/Angular-Slickgrid/#/clientside)
25-
2. Simply set the `columnDefinition.filter.model` to your new custom Filter class and instantiate it with `new` (you can also use dependency injection in the constructor if you wish). Here is an example with a custom input filter:
26-
```typescript
25+
2. Simply set the `columnDefinition.filter.model` to your new custom Filter class and instantiate it with `new` (you can also use dependency injection in the constructor if you wish). Here is an example with a custom input filter:
26+
```typescript
2727
// define you columns, in this demo Effort Driven will use a Select Filter
28-
this.columnDefinitions = [
28+
this.columnDefinitions = [
2929
{ id: 'title', name: 'Title', field: 'title' },
3030
{ id: 'description', name: 'Description', field: 'description', type: FieldType.string,
31-
filterable: true,
31+
filterable: true,
3232
filter: {
33-
model: new CustomInputFilter() // create a new instance to make each Filter independent from each other
33+
model: CustomInputFilter // create a new instance to make each Filter independent from each other
3434
}
3535
}
3636
];
@@ -47,7 +47,7 @@ If you want to load the grid with certain default filter(s), you can use the fol
4747

4848
For example, setting a default value into an `input` element, you can simply get the search term with `columnDef.filter.searchTerms` and set the default value in jquery with `$(filterElm).val(this.searchTerms);`
4949

50-
### Collection
50+
### Collection
5151
If you want to pass a `collection` to your filter (for example, a multiple-select needs a select list of options), you can then use it in your custom filter through `columnDef.filter.collection`
5252

5353
#### `key/label` pair
@@ -61,7 +61,7 @@ this.columnDef.filter.collection.forEach((option: SelectOption) => {
6161
```
6262

6363
#### Custom Structure (key/label pair)
64-
What if your `collection` have totally different value/label pair? In this case, you can use the `customStructure` to change the property name(s) to use. You can change the label and/or the value, they can be passed independently.
64+
What if your `collection` have totally different value/label pair? In this case, you can use the `customStructure` to change the property name(s) to use. You can change the label and/or the value, they can be passed independently.
6565
For example:
6666
```typescript
6767
// use custom structure value/label pair
@@ -80,7 +80,7 @@ this.columnDef.filter.collection.forEach((option: SelectOption) => {
8080
By default a `collection` uses the `label/value` pair without translation or `labelKey/value` pair with translation usage. So if you want to use translations, then you can loop through your `collection` and use the `labelKey/value` properties. For example:
8181
```typescript
8282
this.columnDef.filter.collection.forEach((option: SelectOption) => {
83-
// translate label
83+
// translate label
8484
const textLabel = (option.labelKey && typeof this.i18n.tr === 'function') ? this.i18n.tr(option.labelKey || ' ') : option.labelKey;
8585

8686
// use the option value & translated label
@@ -98,7 +98,7 @@ const labelName = (this.columnDef.filter.customStructure) ? this.columnDef.filte
9898
const valueName = (this.columnDef.filter.customStructure) ? this.columnDef.filter.customStructure.value : 'value';
9999

100100
this.columnDef.filter.collection.forEach((option: SelectOption) => {
101-
// translate label
101+
// translate label
102102
const textLabel = (option.labelKey && typeof this.i18n.tr === 'function') ? this.i18n.tr(option[labelName] || ' ') : option[labelName];
103103

104104
// use the option value & translated label
@@ -109,7 +109,7 @@ this.columnDef.filter.collection.forEach((option: SelectOption) => {
109109
## Custom Filter with Angular Components
110110
You can see them in [Example 22](https://ghiscoding.github.io/Angular-Slickgrid/#/angular-components) which have both Custom Editors & Filters which uses Angular Components. The 2nd column "Assignee" is the column that uses both (it uses `ng-select` 3rd party lib wrapped in an Angular Components [here](https://github.com/ghiscoding/angular-slickgrid/blob/master/src/app/examples/filter-ng-select.component.ts)) and you need to create a Custom Filter like [here](https://github.com/ghiscoding/angular-slickgrid/blob/master/src/app/examples/custom-angularComponentFilter.ts) and use that Custom Filter in your column definition like [here](https://github.com/ghiscoding/angular-slickgrid/blob/master/src/app/examples/grid-angular.component.ts#L109).
111111

112-
Personally I don't find this very straightforward and I don't recommend using Angular Components for Editors/Filters as it adds a lot of boilerplate (compare to 1 step with a jQuery Custom Filter) but if you really wish to go that route, it's now possible following the steps shown below.
112+
Personally I don't find this very straightforward and I don't recommend using Angular Components for Editors/Filters as it adds a lot of boilerplate (compare to 1 step with a jQuery Custom Filter) but if you really wish to go that route, it's now possible following the steps shown below.
113113

114114
The steps to use an Angular Component as a Custom Filter are the following:
115115
1. Create a Custom Filter that will handle the creation or compilation of the Angular Component into a SlickGrid Editors. For that you can take a look at this [Custom Filter](https://github.com/ghiscoding/angular-slickgrid/blob/master/src/app/examples/custom-angularComponentFilter.ts)

docs/migrations/Migration-to-2.x.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ this.columnDefinitions = [
239239
id: 'description', name: 'Description', field: 'description',
240240
filter: {
241241
- type: FilterType.custom,
242-
- customFilter: new CustomInputFilter()
243-
+ model: new CustomInputFilter() // create a new instance to make each Filter independent from each other
242+
- customFilter: CustomInputFilter
243+
+ model: CustomInputFilter // create a new instance to make each Filter independent from each other
244244
}
245245
},
246246
{

src/app/examples/custom-angularComponentEditor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ export class CustomAngularComponentEditor implements Editor {
7777
init() {
7878
if (!this.columnEditor || !this.columnEditor.params.component || !(this.angularUtilService instanceof AngularUtilService)) {
7979
throw new Error(`[Angular-Slickgrid] For Editor with Angular Component to work properly, you need to provide the "AngularUtilService" via the Editor "params" OR the Grid Options "params"
80-
Example: this.columnDefs = [{ id: 'title', field: 'title', editor: { model: CustomAngularComponentEditor, collection: [...], params: { component: MyComponent, angularUtilService: this.angularUtilService }}];
81-
OR this.columnDefs = [{ id: 'title', field: 'title', editor: { model: CustomAngularComponentEditor, collection: [...] }]; this.gridOptions = { params: { angularUtilService: this.angularUtilService }}`);
80+
Example: this.columnDefs = [{ id: 'title', field: 'title', editor: { model: CustomEditor, collection: [...], params: { component: MyComponent, angularUtilService: this.angularUtilService }}];
81+
OR this.columnDefs = [{ id: 'title', field: 'title', editor: { model: CustomEditor, collection: [...] }]; this.gridOptions = { params: { angularUtilService: this.angularUtilService }}`);
8282
}
8383
if (this.columnEditor?.params.component) {
8484
const componentOutput = this.angularUtilService.createAngularComponentAppendToDom(this.columnEditor.params.component, this.args.container);

src/app/examples/custom-angularComponentFilter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ export class CustomAngularComponentFilter implements Filter {
6363

6464
if (!this.columnFilter || !this.columnFilter.params.component || !(this.angularUtilService instanceof AngularUtilService)) {
6565
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"
66-
Example: this.columnDefs = [{ id: 'title', field: 'title', filter: { model: CustomAngularComponentFilter, collection: [...], params: { component: MyComponent, angularUtilService: this.angularUtilService }}];
67-
OR this.columnDefs = [{ id: 'title', field: 'title', filter: { model: CustomAngularComponentFilter, collection: [...] }]; this.gridOptions = { params: { angularUtilService: this.angularUtilService }}`);
66+
Example: this.columnDefs = [{ id: 'title', field: 'title', filter: { model: CustomFilter, collection: [...], params: { component: MyComponent, angularUtilService: this.angularUtilService }}];
67+
OR this.columnDefs = [{ id: 'title', field: 'title', filter: { model: CustomFilter, collection: [...] }]; this.gridOptions = { params: { angularUtilService: this.angularUtilService }}`);
6868
}
6969

7070
if (this.columnFilter?.params.component) {

src/app/examples/grid-angular.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export class GridAngularComponent implements OnInit {
123123
filterable: true,
124124
sortable: true,
125125
filter: {
126-
model: new CustomAngularComponentFilter(), // create a new instance to make each Filter independent from each other
126+
model: CustomAngularComponentFilter, // create a new instance to make each Filter independent from each other
127127
collection: this.assignees,
128128
params: {
129129
component: FilterNgSelectComponent,
@@ -155,7 +155,7 @@ export class GridAngularComponent implements OnInit {
155155
filterable: true,
156156
sortable: true,
157157
filter: {
158-
model: new CustomAngularComponentFilter(), // create a new instance to make each Filter independent from each other
158+
model: CustomAngularComponentFilter, // create a new instance to make each Filter independent from each other
159159
collection: this.assignees,
160160
params: {
161161
component: FilterNgSelectComponent,

src/app/examples/grid-clientside.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class GridClientSideComponent implements OnInit {
6969
id: 'description', name: 'Description', field: 'description', filterable: true, sortable: true, minWidth: 80,
7070
type: FieldType.string,
7171
filter: {
72-
model: new CustomInputFilter(), // create a new instance to make each Filter independent from each other
72+
model: CustomInputFilter, // create a new instance to make each Filter independent from each other
7373
enableTrimWhiteSpace: true // or use global "enableFilterTrimWhiteSpace" to trim on all Filters
7474
}
7575
},

src/app/examples/grid-range.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class GridRangeComponent implements OnInit, OnDestroy {
9797
id: 'description', name: 'Description', field: 'description', filterable: true, sortable: true, minWidth: 80,
9898
type: FieldType.string,
9999
filter: {
100-
model: new CustomInputFilter(), // create a new instance to make each Filter independent from each other
100+
model: CustomInputFilter, // create a new instance to make each Filter independent from each other
101101
enableTrimWhiteSpace: true // or use global "enableFilterTrimWhiteSpace" to trim on all Filters
102102
}
103103
},

0 commit comments

Comments
 (0)