-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Example21.tsx
265 lines (238 loc) · 8.53 KB
/
Example21.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
import {
type Column,
FieldType,
Formatters,
type GridOption,
type GroupingGetterFunction,
type OperatorString,
SlickgridReact,
type SlickgridReactInstance,
} from '../../slickgrid-react';
import React from 'react';
import type BaseSlickGridState from './state-slick-grid-base';
import './example21.scss';
interface Props { }
interface State extends BaseSlickGridState {
selectedColumn?: Column;
selectedOperator: string;
searchValue: string;
reactGrid?: SlickgridReactInstance;
}
export default class Example21 extends React.Component<Props, State> {
title = 'Example 21: Grid AutoHeight';
subTitle = `
The SlickGrid option "autoHeight" can be used if you wish to keep the full height of the grid without any scrolling
<ul>
<li>You define a fixed grid width via "gridWidth" in the View</li>
<li>You can still use the "autoResize" for the width to be resized automatically (the height will never change in this case)</li>
<li>This dataset has 25 rows, if you scroll down the page you can see the entire set is shown without any grid scrolling (though you might have browser scrolling)</li>
</ul>
`;
selectedGroupingFields: Array<string | GroupingGetterFunction> = ['', '', ''];
reactGrid!: SlickgridReactInstance;
operatorList: OperatorString[] = ['=', '<', '<=', '>', '>=', '<>', 'StartsWith', 'EndsWith'];
constructor(public readonly props: Props) {
super(props);
this.state = {
gridOptions: undefined,
columnDefinitions: [],
dataset: [],
selectedColumn: undefined,
selectedOperator: '',
searchValue: '',
};
}
componentDidMount() {
document.title = this.title;
// define the grid options & columns and then create the grid itself
this.defineGrid();
}
reactGridReady(reactGrid: SlickgridReactInstance) {
this.reactGrid = reactGrid;
}
/* Define grid Options and Columns */
defineGrid() {
const columnDefinitions: Column[] = [
{
id: 'title', name: 'Title', field: 'title',
width: 100, sortable: true,
type: FieldType.string
},
{
id: 'duration', name: 'Duration (days)', field: 'duration',
width: 100, sortable: true,
type: FieldType.number
},
{
id: 'complete', name: '% Complete', field: 'percentComplete',
width: 100, sortable: true,
formatter: Formatters.percentCompleteBar,
type: FieldType.number
},
{
id: 'start', name: 'Start', field: 'start',
width: 100, sortable: true,
formatter: Formatters.dateIso,
type: FieldType.date
},
{
id: 'finish', name: 'Finish', field: 'finish',
width: 100,
formatter: Formatters.dateIso, sortable: true,
type: FieldType.date
},
{
id: 'effort-driven', name: 'Effort Driven', field: 'effortDriven',
width: 100, sortable: true,
formatter: Formatters.checkmarkMaterial,
type: FieldType.number
}
];
const gridOptions: GridOption = {
// if you want to disable autoResize and use a fixed width which requires horizontal scrolling
// it's advised to disable the autoFitColumnsOnFirstLoad as well
// enableAutoResize: false,
// autoFitColumnsOnFirstLoad: false,
autoHeight: true,
autoResize: {
container: '#demo-container',
rightPadding: 10
},
// enable the filtering but hide the user filter row since we use our own single filter
enableFiltering: true,
showHeaderRow: false, // hide the filter row (header row)
alwaysShowVerticalScroll: false,
enableColumnPicker: true,
enableCellNavigation: true,
enableRowSelection: true
};
this.setState((state: State) => ({
...state,
gridOptions,
columnDefinitions,
dataset: this.getData(),
}));
}
getData() {
// mock a dataset
const mockedDataset: any[] = [];
for (let i = 0; i < 25; i++) {
const randomYear = 2000 + Math.floor(Math.random() * 10);
const randomMonth = Math.floor(Math.random() * 11);
const randomDay = Math.floor((Math.random() * 29));
const randomPercent = Math.round(Math.random() * 100);
mockedDataset[i] = {
id: i,
title: 'Task ' + i,
duration: Math.round(Math.random() * 100) + '',
percentComplete: randomPercent,
percentCompleteNumber: randomPercent,
start: new Date(randomYear, randomMonth, randomDay),
finish: new Date(randomYear, (randomMonth + 1), randomDay),
effortDriven: (i % 5 === 0)
};
}
return mockedDataset;
}
//
// -- if any of the Search form input changes, we'll call the updateFilter() method
//
clearGridSearchInput() {
this.setState((state: State) => {
return {
...state,
searchValue: '',
};
}, () => this.updateFilter());
}
selectedOperatorChanged(e: React.FormEvent<HTMLSelectElement>) {
this.setState((state: State) => {
return {
...state,
selectedOperator: (e.target as HTMLSelectElement)?.value ?? '',
};
}, () => this.updateFilter());
}
selectedColumnChanged(e: React.ChangeEvent<HTMLSelectElement>) {
const selectedVal = (e.target as HTMLSelectElement)?.value ?? '';
const selectedColumn = this.state.columnDefinitions.find(c => c.id === selectedVal);
this.setState((state: State) => {
return { ...state, selectedColumn };
}, () => this.updateFilter());
}
searchValueChanged(e: React.FormEvent<HTMLInputElement>) {
this.setState((state: State) => {
return { ...state, searchValue: (e.target as HTMLInputElement)?.value ?? '' };
}, () => this.updateFilter());
}
updateFilter() {
this.reactGrid?.filterService.updateSingleFilter({
columnId: `${this.state.selectedColumn?.id ?? ''}`,
operator: this.state.selectedOperator as OperatorString,
searchTerms: [this.state.searchValue || '']
});
}
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/Example21.tsx">
<span className="mdi mdi-link-variant"></span> code
</a>
</span>
</h2>
<div className="subtitle" dangerouslySetInnerHTML={{ __html: this.subTitle }}></div>
<div className="row row-cols-lg-auto g-1 align-items-center">
<div className="col">
<label htmlFor="columnSelect">Single Search:</label>
</div>
<div className="col">
<select className="form-select" data-test="search-column-list" name="selectedColumn" onChange={($event) => this.selectedColumnChanged($event)}>
<option value="''">...</option>
{
this.state.columnDefinitions.map((column) =>
<option value={column.id} key={column.id}>{column.name as string}</option>
)
}
</select>
</div>
<div className="col">
<select className="form-select" data-test="search-operator-list" name="selectedOperator" onChange={($event) => this.selectedOperatorChanged($event)}>
<option value="''">...</option>
{
this.operatorList.map((operator) =>
<option value={operator} key={operator}>{operator}</option>
)
}
</select>
</div>
<div className="col">
<div className="input-group">
<input type="text"
className="form-control"
placeholder="search value"
data-test="search-value-input"
value={this.state.searchValue}
onInput={($event) => this.searchValueChanged($event)} />
<button className="btn btn-outline-secondary d-flex align-items-center pl-2 pr-2" data-test="clear-search-value"
onClick={() => this.clearGridSearchInput()}>
<span className="mdi mdi-close m-1"></span>
</button>
</div>
</div>
</div >
<hr />
<SlickgridReact gridId="grid21"
columnDefinitions={this.state.columnDefinitions}
gridOptions={this.state.gridOptions}
dataset={this.state.dataset}
onReactGridCreated={$event => this.reactGridReady($event.detail)}
/>
</div >
);
}
}