-
Notifications
You must be signed in to change notification settings - Fork 934
/
index.js
197 lines (175 loc) · 5.97 KB
/
index.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
import React from 'react';
import { CircularProgress, Typography } from '@mui/material';
import MUIDataTable from '../../src/';
class Example extends React.Component {
state = {
page: 0,
count: 1,
rowsPerPage: 5,
sortOrder: {},
data: [['Loading Data...']],
columns: [
{
name: 'fullName',
label: 'Full Name',
options: {
customBodyRender: (value, tableMeta, updateValue) => {
// Here you can render a more complex display.
// You're given access to tableMeta, which has
// the rowData (as well as the original object data).
// See the console for a detailed look at this object.
console.log('customBodyRender');
console.dir(tableMeta);
return value;
},
},
},
{
name: 'title',
label: 'Title',
options: {},
},
{
name: 'location',
label: 'Location',
options: {},
},
],
isLoading: false,
};
componentDidMount() {
this.getData('', 0);
}
// get data
getData = async (url, page) => {
this.setState({ isLoading: true });
const res = await this.xhrRequest(url, page);
this.setState({ data: res.data, isLoading: false, count: res.total });
};
getSrcData = () => {
return [
{ fullName: 'Gabby George', title: 'Business Analyst', location: 'Minneapolis' },
{ fullName: 'Aiden Lloyd', title: 'Business Consultant', location: 'Dallas' },
{ fullName: 'Jaden Collins', title: 'Attorney', location: 'Santa Ana' },
{ fullName: 'Franky Rees', title: 'Business Analyst', location: 'St. Petersburg' },
{ fullName: 'Aaren Rose', title: 'Business Analyst', location: 'Toledo' },
{ fullName: 'John George', title: 'Business Analyst', location: 'Washington DC' },
{ fullName: 'Pat Lloyd', title: 'Computer Programmer', location: 'Baltimore' },
{ fullName: 'Joe Joe Collins', title: 'Attorney', location: 'Las Cruces' },
{ fullName: 'Franky Hershy', title: 'Paper Boy', location: 'El Paso' },
{ fullName: 'Aaren Smalls', title: 'Business Analyst', location: 'Tokyo' },
{ fullName: 'Boogie G', title: 'Police Officer', location: 'Unknown' },
{ fullName: 'James Roulf', title: 'Business Consultant', location: 'Video Game Land' },
{ fullName: 'Mike Moocow', title: 'Burger King Employee', location: 'New York' },
{ fullName: 'Mimi Gerock', title: 'Business Analyst', location: 'McCloud' },
{ fullName: 'Jason Evans', title: 'Business Analyst', location: 'Mt Shasta' },
{ fullName: 'Simple Sam', title: 'Business Analyst', location: 'Mt Shasta' },
{ fullName: 'Marky Mark', title: 'Business Consultant', location: 'Las Cruces' },
{ fullName: 'Jaden Jam', title: 'Attorney', location: 'El Paso' },
{ fullName: 'Holly Jo', title: 'Business Analyst', location: 'St. Petersburg' },
{ fullName: 'Suzie Q', title: 'Business Analyst', location: 'New York' },
];
};
sort = (page, sortOrder) => {
this.setState({ isLoading: true });
this.xhrRequest('', page, sortOrder).then(res => {
this.setState({
data: res.data,
page: res.page,
sortOrder,
isLoading: false,
count: res.total,
});
});
};
// mock async function
xhrRequest = (url, page, sortOrder = {}) => {
return new Promise((resolve, reject) => {
// mock page data
let fullData = this.getSrcData();
const total = fullData.length; // mock record count from server - normally this would be a number attached to the return data
let sortField = sortOrder.name;
let sortDir = sortOrder.direction;
if (sortField) {
fullData = fullData.sort((a, b) => {
if (a[sortField] < b[sortField]) {
return 1 * (sortDir === 'asc' ? -1 : 1);
} else if (a[sortField] > b[sortField]) {
return -1 * (sortDir === 'asc' ? -1 : 1);
} else {
return 0;
}
});
}
const srcData = fullData.slice(page * this.state.rowsPerPage, (page + 1) * this.state.rowsPerPage);
let data = srcData;
setTimeout(() => {
resolve({
data,
total,
page,
});
}, 500);
});
};
changePage = (page, sortOrder) => {
this.setState({
isLoading: true,
});
this.xhrRequest(`/myApiServer?page=${page}`, page, sortOrder).then(res => {
this.setState({
isLoading: false,
page: res.page,
sortOrder,
data: res.data,
count: res.total,
});
});
};
render() {
const { data, page, count, isLoading, rowsPerPage, sortOrder } = this.state;
const options = {
filter: true,
filterType: 'dropdown',
responsive: 'vertical',
serverSide: true,
count: count,
rowsPerPage: rowsPerPage,
rowsPerPageOptions: [],
sortOrder: sortOrder,
onTableChange: (action, tableState) => {
console.log(action, tableState);
// a developer could react to change on an action basis or
// examine the state as a whole and do whatever they want
switch (action) {
case 'changePage':
this.changePage(tableState.page, tableState.sortOrder);
break;
case 'sort':
this.sort(tableState.page, tableState.sortOrder);
break;
default:
console.log('action not handled.');
}
},
};
console.log('COLUMNS');
console.dir(JSON.parse(JSON.stringify(this.state.columns)));
return (
<div>
<MUIDataTable
title={
<Typography variant="h6">
ACME Employee list
{isLoading && <CircularProgress size={24} style={{ marginLeft: 15, position: 'relative', top: 4 }} />}
</Typography>
}
data={data}
columns={this.state.columns}
options={options}
/>
</div>
);
}
}
export default Example;