-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
example21-grouping.js
226 lines (208 loc) · 5.63 KB
/
example21-grouping.js
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
const faker = require('faker');
const ReactDataGrid = require('react-data-grid');
const exampleWrapper = require('../components/exampleWrapper');
const React = require('react');
const {
ToolsPanel: { AdvancedToolbar: Toolbar, GroupedColumnsPanel },
Data: { Selectors },
Draggable: { Container: DraggableContainer },
Formatters: { ImageFormatter }
} = require('react-data-grid-addons');
import PropTypes from 'prop-types';
faker.locale = 'en_GB';
const createFakeRowObjectData = (index) => ({
id: 'id_' + index,
avartar: faker.image.avatar(),
county: faker.address.county(),
email: faker.internet.email(),
title: faker.name.prefix(),
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
street: faker.address.streetName(),
zipCode: faker.address.zipCode(),
date: faker.date.past().toLocaleDateString(),
bs: faker.company.bs(),
catchPhrase: faker.company.catchPhrase(),
companyName: faker.company.companyName(),
words: faker.lorem.words(),
sentence: faker.lorem.sentence()
});
const createRows = (numberOfRows) => {
let rows = [];
for (let i = 0; i < numberOfRows; i++) {
rows[i] = createFakeRowObjectData(i);
}
return rows;
};
const columns = [
{
key: 'id',
name: 'ID',
width: 80,
resizable: true
},
{
key: 'avartar',
name: 'Avartar',
width: 60,
formatter: ImageFormatter,
draggable: true
},
{
key: 'county',
name: 'County',
width: 200,
draggable: true
},
{
key: 'title',
name: 'Title',
width: 200,
draggable: true
},
{
key: 'firstName',
name: 'First Name',
width: 200,
draggable: true
},
{
key: 'lastName',
name: 'Last Name',
width: 200,
draggable: true
},
{
key: 'email',
name: 'Email',
width: 200,
draggable: true
},
{
key: 'street',
name: 'Street',
width: 200,
draggable: true
},
{
key: 'zipCode',
name: 'ZipCode',
width: 200,
draggable: true
},
{
key: 'date',
name: 'Date',
width: 200,
draggable: true
},
{
key: 'bs',
name: 'bs',
draggable: true,
width: 200
},
{
key: 'catchPhrase',
name: 'Catch Phrase',
width: 200,
draggable: true
},
{
key: 'companyName',
name: 'Company Name',
width: 200,
draggable: true
},
{
key: 'sentence',
name: 'Sentence',
width: 200,
draggable: true
}
];
class CustomToolbar extends React.Component {
static propTypes = {
groupBy: PropTypes.array.isRequired,
onColumnGroupAdded: PropTypes.func.isRequired,
onColumnGroupDeleted: PropTypes.func.isRequired
};
render() {
return (<Toolbar>
<GroupedColumnsPanel groupBy={this.props.groupBy} onColumnGroupAdded={this.props.onColumnGroupAdded} onColumnGroupDeleted={this.props.onColumnGroupDeleted}/>
</Toolbar>);
}
}
class Example extends React.Component {
constructor(props) {
super(props);
let fakeRows = createRows(2000);
this.state = {rows: fakeRows, groupBy: [], expandedRows: {}};
}
getRows = () => {
let rows = Selectors.getRows(this.state);
return rows;
};
getRowAt = (index) => {
let rows = this.getRows();
return rows[index];
};
getSize = () => {
return this.getRows().length;
};
onColumnGroupAdded = (colName) => {
let columnGroups = this.state.groupBy.slice(0);
let activeColumn = columns.find((c) => c.key === colName)
let isNotInGroups = columnGroups.find((c) => activeColumn.key === c.name) == null;
if (isNotInGroups) {
columnGroups.push({key: activeColumn.key, name: activeColumn.name});
}
this.setState({groupBy: columnGroups});
};
onColumnGroupDeleted = (name) => {
let columnGroups = this.state.groupBy.filter(function(g){
return typeof g === 'string' ? g !== name : g.key !== name;
});
this.setState({groupBy: columnGroups});
};
onRowExpandToggle = ({ columnGroupName, name, shouldExpand }) => {
let expandedRows = Object.assign({}, this.state.expandedRows);
expandedRows[columnGroupName] = Object.assign({}, expandedRows[columnGroupName]);
expandedRows[columnGroupName][name] = {isExpanded: shouldExpand};
this.setState({expandedRows: expandedRows});
};
render() {
return (
<DraggableContainer>
<ReactDataGrid
ref={ node => this.grid = node }
enableCellSelect={true}
enableDragAndDrop={true}
columns={columns}
rowGetter={this.getRowAt}
rowsCount={this.getSize()}
onRowExpandToggle={this.onRowExpandToggle}
toolbar={<CustomToolbar groupBy={this.state.groupBy} onColumnGroupAdded={this.onColumnGroupAdded} onColumnGroupDeleted={this.onColumnGroupDeleted}/>}
rowHeight={50}
minHeight={600}
/>
</DraggableContainer>
);
}
}
const exampleDescription = (
<div>
<p>This example demonstrates how to group rows by column name. Drag a column header to group rows by that column.</p>
<p>To expand and close a row group, you can use either the mouse or keyboard</p>
<p>Press <strong>Enter</strong> or <strong>Left Arrow</strong> or <strong>Right Arrow</strong> to toggle whether a row is expanded or not</p>
<p>This feature also supports a custom Renderer, by using a renderer you can render some fancy custom html in the row gorup.</p>
<p>To use a renderer just inject your component with <code>rowGroupRenderer</code> prop in the grid.</p>
</div>
);
module.exports = exampleWrapper({
WrappedComponent: Example,
exampleName: 'Row Grouping Example',
exampleDescription,
examplePath: './scripts/example21-grouping.js',
examplePlaygroundLink: undefined
});