-
Notifications
You must be signed in to change notification settings - Fork 0
/
MUIDataGrid
60 lines (53 loc) · 1.62 KB
/
MUIDataGrid
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 React, { useEffect, useState } from "react";
import { DataGrid } from "@mui/x-data-grid";
import axios from "axios";
const DataGridWithDynamicPagination = () => {
const [page, setPage] = useState(0);
const [pageSize, setPageSize] = useState(5);
const [rowCount, setRowCount] = useState(0);
const [data, setData] = useState([]);
const [loading, setLoading] = useState(false);
useEffect(() => {
fetchData();
}, [page, pageSize]);
const fetchData = async () => {
setLoading(true);
try {
const response = await axios.get(
`https://your-api-endpoint/data?page=${page}&pageSize=${pageSize}`
);
setData(response.data.items);
setRowCount(response.data.totalCount); // Assuming your API returns total row count
} catch (error) {
console.error("Error fetching data: ", error);
} finally {
setLoading(false);
}
};
const handlePageChange = (newPage) => {
setPage(newPage);
};
const handlePageSizeChange = (newPageSize) => {
setPageSize(newPageSize);
};
return (
<div style={{ height: 400, width: "100%" }}>
<DataGrid
rows={data}
columns={[
{ field: "id", headerName: "ID", width: 90 },
{ field: "name", headerName: "Name", width: 150 },
{ field: "age", headerName: "Age", width: 150 },
]}
pagination
paginationMode="server"
rowCount={rowCount}
pageSize={pageSize}
onPageSizeChange={handlePageSizeChange}
onPageChange={handlePageChange}
loading={loading}
/>
</div>
);
};
export default DataGridWithDynamicPagination;