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

Commit 525f00a

Browse files
Ghislain BeaulacGhislain Beaulac
authored andcommitted
feat(filter): add posibility to filter complex objects
1 parent d31e20f commit 525f00a

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,25 @@ export class GridClientSideComponent implements OnInit {
9797
},
9898
{ id: 'utcDate', name: 'UTC Date', field: 'utcDate', formatter: Formatters.dateTimeIsoAmPm, sortable: true, minWidth: 115,
9999
type: FieldType.dateUtc, outputType: FieldType.dateTimeIsoAmPm, filterable: true, filter: { model: Filters.compoundDate } },
100-
{ id: 'effort-driven', name: 'Effort Driven', field: 'effortDriven', minWidth: 85, maxWidth: 85, formatter: Formatters.checkmark,
100+
{ id: 'effort-driven', name: 'Effort Driven', field: 'effortDriven.isEffort', minWidth: 85, maxWidth: 85,
101101
type: FieldType.boolean,
102102
sortable: true,
103103
filterable: true,
104+
// to pass multiple formatters, use the params property
105+
// also these formatters are executed in sequence, so if you want the checkmark to work correctly, it has to be the last formatter defined
106+
formatter: Formatters.multiple,
107+
params: { formatters: [Formatters.complexObject, Formatters.checkmark] },
104108
filter: {
105109
// We can also add HTML text to be rendered (any bad script will be sanitized) but we have to opt-in, else it will be sanitized
106110
// enableRenderHtml: true,
107111
// collection: [{ value: '', label: '' }, { value: true, label: 'True', labelPrefix: `<i class="fa fa-check"></i> ` }, { value: false, label: 'False' }],
108112

109-
collection: [ { value: '', label: '' }, { value: true, label: 'True' }, { value: false, label: 'False' } ],
113+
collection: [ { isEffort: '', label: '' }, { isEffort: true, label: 'True' }, { isEffort: false, label: 'False' } ],
114+
customStructure: {
115+
value: 'isEffort',
116+
label: 'label'
117+
},
118+
isComplexObject: true,
110119
model: Filters.singleSelect,
111120

112121
// we could add certain option(s) to the "multiple-select" plugin
@@ -162,6 +171,7 @@ export class GridClientSideComponent implements OnInit {
162171
const randomPercent = randomBetween(0, 100);
163172
const randomHour = randomBetween(10, 23);
164173
const randomTime = randomBetween(10, 59);
174+
const randomIsEffort = (i % 3 === 0);
165175

166176
tempDataset.push({
167177
id: i,
@@ -173,7 +183,10 @@ export class GridClientSideComponent implements OnInit {
173183
start: (i % 4) ? null : new Date(randomYear, randomMonth, randomDay), // provide a Date format
174184
usDateShort: `${randomMonth}/${randomDay}/${randomYearShort}`, // provide a date US Short in the dataset
175185
utcDate: `${randomYear}-${randomMonthStr}-${randomDay}T${randomHour}:${randomTime}:${randomTime}Z`,
176-
effortDriven: (i % 3 === 0)
186+
effortDriven: {
187+
isEffort: randomIsEffort,
188+
label: randomIsEffort ? 'Effort' : 'NoEffort',
189+
}
177190
});
178191
}
179192

src/app/modules/angular-slickgrid/models/columnFilter.interface.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ export interface ColumnFilter {
7171
/** Do we want the Filter to handle translation (localization)? */
7272
enableTranslateLabel?: boolean;
7373

74+
/**
75+
* Defaults to false, does the filter has to deal with item that are complex object?
76+
* If so, it will explode the object using the dot "." notation to find the value to filter against.
77+
*/
78+
isComplexObject?: boolean;
79+
7480
/**
7581
* Use "params" to pass any type of arguments to your Custom Filter
7682
* for example, to pass a second collection to a select Filter we can type this:

src/app/modules/angular-slickgrid/services/filter.service.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
SlickEvent,
1616
OperatorString
1717
} from './../models/index';
18-
import { castToPromise } from './utilities';
18+
import { castToPromise, getDescendantProperty } from './utilities';
1919
import { FilterFactory } from '../filters/filterFactory';
2020
import { Subject } from 'rxjs/Subject';
2121
import * as isequal_ from 'lodash.isequal';
@@ -185,10 +185,16 @@ export class FilterService {
185185
if (!columnDef) {
186186
return false;
187187
}
188+
189+
const fieldName = columnDef.queryField || columnDef.queryFieldFilter || columnDef.field;
188190
const fieldType = columnDef.type || FieldType.string;
189191
const filterSearchType = (columnDef.filterSearchType) ? columnDef.filterSearchType : null;
192+
let cellValue = item[fieldName];
190193

191-
let cellValue = item[columnDef.queryField || columnDef.queryFieldFilter || columnDef.field];
194+
// when item is a complex object, we need to filter the value contained in the object following the dot "." notation
195+
if (columnDef && columnDef.filter && columnDef.filter.isComplexObject) {
196+
cellValue = getDescendantProperty(item, fieldName);
197+
}
192198

193199
// if we find searchTerms use them but make a deep copy so that we don't affect original array
194200
// we might have to overwrite the value(s) locally that are returned

0 commit comments

Comments
 (0)