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

Commit f5ed9f9

Browse files
authored
Merge pull request #68 from ghiscoding/feat/filter-complex-object
Feat/filter complex object
2 parents d31e20f + 25c0a52 commit f5ed9f9

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,28 @@ 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+
{
101+
id: 'effort-driven', name: 'Effort Driven', field: 'effortDriven.isEffort', minWidth: 85, maxWidth: 85,
101102
type: FieldType.boolean,
102103
sortable: true,
104+
105+
// to pass multiple formatters, use the params property
106+
// also these formatters are executed in sequence, so if you want the checkmark to work correctly, it has to be the last formatter defined
107+
formatter: Formatters.multiple,
108+
params: { formatters: [Formatters.complexObject, Formatters.checkmark] },
109+
110+
// when the "field" string includes the dot "." notation, the library will consider this to be a complex object and Filter accordingly
103111
filterable: true,
104112
filter: {
105113
// 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
106114
// enableRenderHtml: true,
107115
// collection: [{ value: '', label: '' }, { value: true, label: 'True', labelPrefix: `<i class="fa fa-check"></i> ` }, { value: false, label: 'False' }],
108116

109-
collection: [ { value: '', label: '' }, { value: true, label: 'True' }, { value: false, label: 'False' } ],
117+
collection: [ { isEffort: '', label: '' }, { isEffort: true, label: 'True' }, { isEffort: false, label: 'False' } ],
118+
customStructure: {
119+
value: 'isEffort',
120+
label: 'label'
121+
},
110122
model: Filters.singleSelect,
111123

112124
// we could add certain option(s) to the "multiple-select" plugin
@@ -162,6 +174,7 @@ export class GridClientSideComponent implements OnInit {
162174
const randomPercent = randomBetween(0, 100);
163175
const randomHour = randomBetween(10, 23);
164176
const randomTime = randomBetween(10, 59);
177+
const randomIsEffort = (i % 3 === 0);
165178

166179
tempDataset.push({
167180
id: i,
@@ -173,7 +186,10 @@ export class GridClientSideComponent implements OnInit {
173186
start: (i % 4) ? null : new Date(randomYear, randomMonth, randomDay), // provide a Date format
174187
usDateShort: `${randomMonth}/${randomDay}/${randomYearShort}`, // provide a date US Short in the dataset
175188
utcDate: `${randomYear}-${randomMonthStr}-${randomDay}T${randomHour}:${randomTime}:${randomTime}Z`,
176-
effortDriven: (i % 3 === 0)
189+
effortDriven: {
190+
isEffort: randomIsEffort,
191+
label: randomIsEffort ? 'Effort' : 'NoEffort',
192+
}
177193
});
178194
}
179195

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 (dot "." notation), we need to filter the value contained in the object tree
195+
if (fieldName.indexOf('.') >= 0) {
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)