forked from mui/mui-x
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ServerSideDataGridTTL.js
60 lines (54 loc) · 1.65 KB
/
ServerSideDataGridTTL.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
import * as React from 'react';
import { DataGridPro, GridDataSourceCacheDefault } from '@mui/x-data-grid-pro';
import { useMockServer } from '@mui/x-data-grid-generator';
const lowTTLCache = new GridDataSourceCacheDefault({ ttl: 1000 * 10 }); // 10 seconds
function ServerSideDataGridTTL() {
const { columns, initialState, fetchRows } = useMockServer(
{},
{ useCursorPagination: false },
);
const dataSource = React.useMemo(
() => ({
getRows: async (params) => {
const urlParams = new URLSearchParams({
paginationModel: encodeURIComponent(
JSON.stringify(params.paginationModel),
),
filterModel: encodeURIComponent(JSON.stringify(params.filterModel)),
sortModel: encodeURIComponent(JSON.stringify(params.sortModel)),
});
const getRowsResponse = await fetchRows(
`https://mui.com/x/api/data-grid?${urlParams.toString()}`,
);
return {
rows: getRowsResponse.rows,
rowCount: getRowsResponse.rowCount,
};
},
}),
[fetchRows],
);
const initialStateWithPagination = React.useMemo(
() => ({
...initialState,
pagination: {
paginationModel: { pageSize: 10, page: 0 },
rowCount: 0,
},
}),
[initialState],
);
return (
<div style={{ width: '100%', height: 400 }}>
<DataGridPro
columns={columns}
unstable_dataSource={dataSource}
unstable_dataSourceCache={lowTTLCache}
pagination
initialState={initialStateWithPagination}
pageSizeOptions={[10, 20, 50]}
/>
</div>
);
}
export default ServerSideDataGridTTL;