-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathExample31.tsx
591 lines (531 loc) · 22.5 KB
/
Example31.tsx
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
import { GridOdataService, OdataServiceApi, OdataOption } from '@slickgrid-universal/odata';
import { RxJsResource } from '@slickgrid-universal/rxjs-observable';
import { Observable, of, Subject } from 'rxjs';
import {
Column,
Editors,
FieldType,
Filters,
GridOption,
GridStateChange,
Metrics,
OperatorType,
Pagination,
SlickgridReactInstance,
SlickgridReact,
} from '../../slickgrid-react';
import React from 'react';
import BaseSlickGridState from './state-slick-grid-base';
const defaultPageSize = 20;
const sampleDataRoot = 'assets/data';
interface Status { text: string, class: string }
interface State extends BaseSlickGridState {
paginationOptions?: Pagination;
metrics: Metrics;
isCountEnabled: boolean;
isSelectEnabled: boolean;
isExpandEnabled: boolean;
isOtherGenderAdded: boolean;
odataVersion: number;
odataQuery: string;
processing: boolean;
errorStatus: string;
status: Status;
genderCollection: Array<{ value: string; label: string; }>;
}
interface Props { }
export default class Example31 extends React.Component<Props, State> {
title = 'Example 31: Grid with OData Backend Service using RxJS Observables';
subTitle = `
Optionally use RxJS instead of Promises, you would typically use this with a Backend Service API (OData/GraphQL)
`;
reactGrid!: SlickgridReactInstance;
constructor(public readonly props: Props) {
super(props);
this.state = {
gridOptions: undefined,
columnDefinitions: [],
dataset: [],
paginationOptions: undefined,
errorStatus: '',
isOtherGenderAdded: false,
isCountEnabled: true,
isSelectEnabled: false,
isExpandEnabled: false,
metrics: {} as Metrics,
status: { class: '', text: '' },
odataVersion: 2,
odataQuery: '',
processing: false,
genderCollection: [{ value: 'male', label: 'male' }, { value: 'female', label: 'female' }],
};
}
componentDidMount() {
this.defineGrid();
}
reactGridReady(reactGrid: SlickgridReactInstance) {
this.reactGrid = reactGrid;
}
defineGrid() {
const columnDefinitions: Column[] = [
{
id: 'name', name: 'Name', field: 'name', sortable: true,
type: FieldType.string,
filterable: true,
filter: {
model: Filters.compoundInput
}
},
{
id: 'gender', name: 'Gender', field: 'gender', filterable: true,
editor: {
model: Editors.singleSelect,
// collection: this.genderCollection,
collectionAsync: of(this.state.genderCollection)
},
filter: {
model: Filters.singleSelect,
collectionAsync: of(this.state.genderCollection),
collectionOptions: {
addBlankEntry: true
}
}
},
{ id: 'company', name: 'Company', field: 'company', filterable: true, sortable: true },
{ id: 'category_name', name: 'Category', field: 'category/name', filterable: true, sortable: true },
];
const gridOptions: GridOption = {
enableAutoResize: true,
autoResize: {
container: '#demo-container',
rightPadding: 10
},
checkboxSelector: {
// you can toggle these 2 properties to show the "select all" checkbox in different location
hideInFilterHeaderRow: false,
hideInColumnTitleRow: true
},
editable: true,
autoEdit: true,
autoCommitEdit: true,
rowHeight: 33,
headerRowHeight: 35,
enableCellNavigation: true,
enableFiltering: true,
enableCheckboxSelector: true,
enableRowSelection: true,
enablePagination: true, // you could optionally disable the Pagination
pagination: {
pageSizes: [10, 20, 50, 100, 500],
pageSize: defaultPageSize,
totalItems: 0
},
presets: {
// you can also type operator as string, e.g.: operator: 'EQ'
filters: [
{ columnId: 'gender', searchTerms: ['male'], operator: OperatorType.equal },
],
sorters: [
// direction can be written as 'asc' (uppercase or lowercase) and/or use the SortDirection type
{ columnId: 'name', direction: 'asc' },
],
pagination: { pageNumber: 2, pageSize: 20 }
},
backendServiceApi: {
service: new GridOdataService(),
options: {
enableCount: this.state.isCountEnabled, // add the count in the OData query, which will return a property named "__count" (v2) or "@odata.count" (v4)
enableSelect: this.state.isSelectEnabled,
enableExpand: this.state.isExpandEnabled,
version: this.state.odataVersion // defaults to 2, the query string is slightly different between OData 2 and 4
},
preProcess: () => {
this.setState((state: State) => ({ ...state, errorStatus: '' }));
this.displaySpinner(true);
},
process: (query) => this.getCustomerApiCall(query),
postProcess: (response) => {
this.setState(
(state: State) => ({ ...state, metrics: response.metrics }),
() => this.getCustomerCallback(response)
);
this.displaySpinner(false);
}
} as OdataServiceApi,
externalResources: [new RxJsResource()]
};
this.setState((state: State) => ({
...state,
columnDefinitions,
gridOptions,
}));
}
addOtherGender() {
const newGender = { value: 'other', label: 'other' };
const genderColumn = this.state.columnDefinitions.find((column: Column) => column.id === 'gender');
if (genderColumn) {
let editorCollection = genderColumn.editor!.collection;
const filterCollectionAsync = genderColumn.filter!.collectionAsync as Subject<any>;
if (Array.isArray(editorCollection)) {
// refresh the Editor "collection", we have 2 ways of doing it
// 1. simply Push to the Editor "collection"
// editorCollection.push(newGender);
// 2. or replace the entire "collection"
genderColumn.editor!.collection = [...this.state.genderCollection, newGender];
editorCollection = genderColumn.editor!.collection;
// However, for the Filter only, we have to trigger an RxJS/Subject change with the new collection
// we do this because Filter(s) are shown at all time, while on Editor it's unnecessary since they are only shown when opening them
if (filterCollectionAsync?.next) {
filterCollectionAsync.next(editorCollection);
}
}
}
// don't add it more than once
this.setState((state: State) => ({ ...state, isOtherGenderAdded: true }));
}
displaySpinner(isProcessing: boolean) {
const newStatus = (isProcessing)
? { text: 'loading...', class: 'col-md-2 alert alert-warning' }
: { text: 'finished!!', class: 'col-md-2 alert alert-success' };
this.setState((state: State) => ({
...state,
processing: isProcessing,
status: newStatus
}));
}
getCustomerCallback(data: any) {
// totalItems property needs to be filled for pagination to work correctly
// however we need to force React to do a dirty check, doing a clone object will do just that
let totalItemCount: number = data['totalRecordCount']; // you can use "totalRecordCount" or any name or "odata.count" when "enableCount" is set
if (this.state.isCountEnabled) {
totalItemCount = (this.state.odataVersion === 4) ? data['@odata.count'] : data['d']['__count'];
}
// once pagination totalItems is filled, we can update the dataset
this.setState((state: State) => ({
...state,
paginationOptions: { ...state.gridOptions!.pagination, totalItems: totalItemCount } as Pagination,
dataset: state.odataVersion === 4 ? data.value : data.d.results,
odataQuery: data['query'],
metrics: { ...state.metrics, totalItemCount }
}));
// Slickgrid-React requires the user to update pagination via this pubsub publish
this.reactGrid.eventPubSubService?.publish('onPaginationOptionsChanged', { ...this.state.gridOptions!.pagination, totalItems: totalItemCount } as Pagination, 1);
}
getCustomerApiCall(query: string) {
// in your case, you will call your WebAPI function (wich needs to return an Observable)
// for the demo purpose, we will call a mock WebAPI function
return this.getCustomerDataApiMock(query);
}
/**
* This function is only here to mock a WebAPI call (since we are using a JSON file for the demo)
* in your case the getCustomer() should be a WebAPI function returning a Promise
*/
getCustomerDataApiMock(query: string): Observable<any> {
// the mock is returning an Observable
return new Observable((observer) => {
const queryParams = query.toLowerCase().split('&');
let top: number;
let skip = 0;
let orderBy = '';
let countTotalItems = 100;
const columnFilters = {};
for (const param of queryParams) {
if (param.includes('$top=')) {
top = +(param.substring('$top='.length));
}
if (param.includes('$skip=')) {
skip = +(param.substring('$skip='.length));
}
if (param.includes('$orderby=')) {
orderBy = param.substring('$orderby='.length);
}
if (param.includes('$filter=')) {
const filterBy = param.substring('$filter='.length).replace('%20', ' ');
if (filterBy.includes('contains')) {
const filterMatch = filterBy.match(/contains\(([a-zA-Z\/]+),\s?'(.*?)'/);
const fieldName = filterMatch![1].trim();
(columnFilters as any)[fieldName] = { type: 'substring', term: filterMatch![2].trim() };
}
if (filterBy.includes('substringof')) {
const filterMatch = filterBy.match(/substringof\('(.*?)',\s([a-zA-Z\/]+)/);
const fieldName = filterMatch![2].trim();
(columnFilters as any)[fieldName] = { type: 'substring', term: filterMatch![1].trim() };
}
if (filterBy.includes('eq')) {
const filterMatch = filterBy.match(/([a-zA-Z ]*) eq '(.*?)'/);
if (Array.isArray(filterMatch)) {
const fieldName = filterMatch![1].trim();
(columnFilters as any)[fieldName] = { type: 'equal', term: filterMatch![2].trim() };
}
}
if (filterBy.includes('startswith')) {
const filterMatch = filterBy.match(/startswith\(([a-zA-Z ]*),\s?'(.*?)'/);
const fieldName = filterMatch![1].trim();
(columnFilters as any)[fieldName] = { type: 'starts', term: filterMatch![2].trim() };
}
if (filterBy.includes('endswith')) {
const filterMatch = filterBy.match(/endswith\(([a-zA-Z ]*),\s?'(.*?)'/);
const fieldName = filterMatch![1].trim();
(columnFilters as any)[fieldName] = { type: 'ends', term: filterMatch![2].trim() };
}
}
}
// read the json and create a fresh copy of the data that we are free to modify
fetch(`${sampleDataRoot}/customers_100.json`)
.then(response => response.json())
.then(response => {
let data = response as any[];
// Sort the data
if (orderBy?.length > 0) {
const orderByClauses = orderBy.split(',');
for (const orderByClause of orderByClauses) {
const orderByParts = orderByClause.split(' ');
const orderByField = orderByParts[0];
let selector = (obj: any): string => obj;
for (const orderByFieldPart of orderByField.split('/')) {
const prevSelector = selector;
selector = (obj: any) => {
return prevSelector(obj)[orderByFieldPart as any];
};
}
const sort = orderByParts[1] ?? 'asc';
switch (sort.toLocaleLowerCase()) {
case 'asc':
data = data.sort((a, b) => selector(a).localeCompare(selector(b)));
break;
case 'desc':
data = data.sort((a, b) => selector(b).localeCompare(selector(a)));
break;
}
}
}
// Read the result field from the JSON response.
const firstRow = skip;
let filteredData = data;
if (columnFilters) {
for (const columnId in columnFilters) {
if (columnFilters.hasOwnProperty(columnId)) {
filteredData = filteredData.filter(column => {
const filterType = (columnFilters as any)[columnId].type;
const searchTerm = (columnFilters as any)[columnId].term;
let colId = columnId;
if (columnId && columnId.indexOf(' ') !== -1) {
const splitIds = columnId.split(' ');
colId = splitIds[splitIds.length - 1];
}
let filterTerm;
let col = column;
for (const part of colId.split('/')) {
filterTerm = (col as any)[part];
col = filterTerm;
}
if (filterTerm) {
switch (filterType) {
case 'equal': return filterTerm.toLowerCase() === searchTerm;
case 'ends': return filterTerm.toLowerCase().endsWith(searchTerm);
case 'starts': return filterTerm.toLowerCase().startsWith(searchTerm);
case 'substring': return filterTerm.toLowerCase().includes(searchTerm);
}
}
});
}
}
countTotalItems = filteredData.length;
}
const updatedData = filteredData.slice(firstRow, firstRow + top!);
setTimeout(() => {
const backendResult: any = { query };
if (!this.state.isCountEnabled) {
backendResult['totalRecordCount'] = countTotalItems;
}
if (this.state.odataVersion === 4) {
backendResult['value'] = updatedData;
if (this.state.isCountEnabled) {
backendResult['@odata.count'] = countTotalItems;
}
} else {
backendResult['d'] = { results: updatedData };
if (this.state.isCountEnabled) {
backendResult['d']['__count'] = countTotalItems;
}
}
observer.next(backendResult);
observer.complete();
}, 150);
});
});
}
clearAllFiltersAndSorts() {
this.reactGrid?.gridService.clearAllFiltersAndSorts();
}
goToFirstPage() {
this.reactGrid.paginationService!.goToFirstPage();
}
goToLastPage() {
this.reactGrid.paginationService!.goToLastPage();
}
/** Dispatched event of a Grid State Changed event */
gridStateChanged(gridStateChanges: GridStateChange) {
// console.log('Client sample, Grid State changed:: ', gridStateChanges);
console.log('Client sample, Grid State changed:: ', gridStateChanges.change);
}
setFiltersDynamically() {
// we can Set Filters Dynamically (or different filters) afterward through the FilterService
this.reactGrid.filterService.updateFilters([
// { columnId: 'gender', searchTerms: ['male'], operator: OperatorType.equal },
{ columnId: 'name', searchTerms: ['A'], operator: 'a*' },
]);
}
setSortingDynamically() {
this.reactGrid.sortService.updateSorting([
{ columnId: 'name', direction: 'DESC' },
]);
}
// YOU CAN CHOOSE TO PREVENT EVENT FROM BUBBLING IN THE FOLLOWING 3x EVENTS
// note however that internally the cancelling the search is more of a rollback
handleOnBeforeSort(e: Event) {
// e.preventDefault();
// return false;
return true;
}
handleOnBeforeSearchChange(e: Event) {
// e.preventDefault();
// return false;
return true;
}
handleOnBeforePaginationChange(e: Event) {
// e.preventDefault();
// return false;
return true;
}
// THE FOLLOWING METHODS ARE ONLY FOR DEMO PURPOSES DO NOT USE THIS CODE
// ---
changeCountEnableFlag() {
const isCountEnabled = !this.state.isCountEnabled;
this.setState((state: State) => ({ ...state, isCountEnabled }));
this.resetOptions({ enableCount: isCountEnabled });
return true;
}
changeEnableSelectFlag() {
const isSelectEnabled = !this.state.isSelectEnabled;
this.setState((state: State) => ({ ...state, isSelectEnabled }));
this.resetOptions({ enableSelect: isSelectEnabled });
return true;
}
changeEnableExpandFlag() {
const isExpandEnabled = !this.state.isExpandEnabled;
this.setState((state: State) => ({ ...state, isExpandEnabled }));
this.resetOptions({ enableExpand: isExpandEnabled });
return true;
}
setOdataVersion(version: number) {
this.setState((state: State) => ({ ...state, odataVersion: version }));
this.resetOptions({ version });
return true;
}
private resetOptions(options: Partial<OdataOption>) {
const odataService = this.state.gridOptions?.backendServiceApi?.service as GridOdataService;
odataService.updateOptions(options);
odataService.clearFilters();
this.reactGrid?.filterService.clearFilters();
}
render() {
return !this.state.gridOptions ? '' : (
<div id="demo-container" className="container-fluid">
<h2>
{this.title}
<span className="float-end font18">
see
<a target="_blank"
href="https://github.com/ghiscoding/slickgrid-react/blob/master/src/examples/slickgrid/Example31.tsx">
<span className="fa fa-link"></span> code
</a>
</span>
</h2>
<div className="subtitle" dangerouslySetInnerHTML={{ __html: this.subTitle }}></div>
<div className="row">
<div className="col-md-12" aria-label="Basic Editing Commands">
<button className="btn btn-outline-secondary btn-sm" data-test="clear-filters-sorting"
onClick={() => this.clearAllFiltersAndSorts()} title="Clear all Filters & Sorts">
<span className="mdi mdi-close"></span>
<span>Clear all Filter & Sorts</span>
</button>
<button className="btn btn-outline-secondary btn-sm mx-1" data-test="set-dynamic-filter"
onClick={() => this.setFiltersDynamically()}>
Set Filters Dynamically
</button>
<button className="btn btn-outline-secondary btn-sm" data-test="set-dynamic-sorting"
onClick={() => this.setSortingDynamically()}>
Set Sorting Dynamically
</button>
<button className="btn btn-outline-secondary btn-sm mx-1" style={{ marginLeft: '10px' }} data-test="add-gender-button"
onClick={() => this.addOtherGender()} disabled={this.state.isOtherGenderAdded}>
Add Other Gender via RxJS
</button>
</div>
</div>
<br />
<div>
<span>
<label>Programmatically go to first/last page:</label>
<button className="btn btn-outline-secondary btn-xs px-2" data-test="goto-first-page" onClick={() => this.goToFirstPage()}>
<i className="fa fa-caret-left fa-lg"></i>
</button>
<button className="btn btn-outline-secondary btn-xs px-2" data-test="goto-last-page" onClick={() => this.goToLastPage()}>
<i className="fa fa-caret-right fa-lg"></i>
</button>
</span>
<span style={{ marginLeft: '10px' }}>
<label>OData Version: </label>
<span data-test="radioVersion">
<label className="radio-inline control-label" htmlFor="radio2">
<input type="radio" name="inlineRadioOptions" data-test="version2" id="radio2" defaultChecked={true} value="2"
onChange={() => this.setOdataVersion(2)} /> 2
</label>
<label className="radio-inline control-label" htmlFor="radio4">
<input type="radio" name="inlineRadioOptions" data-test="version4" id="radio4" value="4"
onChange={() => this.setOdataVersion(4)} /> 4
</label>
</span>
<label className="checkbox-inline control-label" htmlFor="enableCount" style={{ marginLeft: '20px' }}>
<input type="checkbox" id="enableCount" data-test="enable-count" defaultChecked={this.state.isCountEnabled}
onChange={() => this.changeCountEnableFlag()} />
<span style={{ fontWeight: 'bold' }}> Enable Count</span> (add to OData query)
</label>
<label className="checkbox-inline control-label" htmlFor="enableSelect" style={{ marginLeft: '20px' }}>
<input type="checkbox" id="enableSelect" data-test="enable-select" defaultChecked={this.state.isSelectEnabled}
onChange={() => this.changeEnableSelectFlag()} />
<span style={{ fontWeight: 'bold' }}> Enable Select</span> (add to OData query)
</label>
<label className="checkbox-inline control-label" htmlFor="enableExpand" style={{ marginLeft: '20px' }}>
<input type="checkbox" id="enableExpand" data-test="enable-expand" defaultChecked={this.state.isExpandEnabled}
onChange={() => this.changeEnableExpandFlag()} />
<span style={{ fontWeight: 'bold' }}> Enable Expand</span> (add to OData query)
</label>
</span>
</div>
<div className="row" style={{ marginTop: '5px' }}>
<div className="col-md-10">
<div className="alert alert-info" data-test="alert-odata-query">
<strong>OData Query:</strong> <span data-test="odata-query-result">{this.state.odataQuery}</span>
</div>
</div>
<div className={this.state.status.class} role="alert" data-test="status">
<strong>Status: </strong> {this.state.status.text}
</div>
</div>
<SlickgridReact gridId="grid31"
columnDefinitions={this.state.columnDefinitions}
gridOptions={this.state.gridOptions}
dataset={this.state.dataset}
paginationOptions={this.state.paginationOptions}
onReactGridCreated={$event => this.reactGridReady($event.detail)}
onGridStateChanged={$event => this.gridStateChanged($event.detail)}
onBeforeSort={$event => this.handleOnBeforeSort($event.detail.eventData)}
onBeforeSearchChange={$event => this.handleOnBeforeSearchChange($event.detail.eventData)}
onBeforePaginationChange={$event => this.handleOnBeforePaginationChange($event.detail.eventData)}
/>
</div>
);
}
}