-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DataGridPro] Server side data source lazy loading (#13878)
Signed-off-by: Armin Mehinovic <4390250+arminmeh@users.noreply.github.com> Co-authored-by: Kenan Yusuf <kenan.m.yusuf@gmail.com> Co-authored-by: Bilal Shafi <bilalshafidev@gmail.com> Co-authored-by: Sycamore <71297412+samuelsycamore@users.noreply.github.com>
- Loading branch information
1 parent
65ca208
commit 659dc5f
Showing
53 changed files
with
2,314 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
docs/data/data-grid/server-side-data/ServerSideLazyLoadingErrorHandling.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import * as React from 'react'; | ||
import { | ||
DataGridPro, | ||
useGridApiRef, | ||
GridToolbar, | ||
GRID_ROOT_GROUP_ID, | ||
} from '@mui/x-data-grid-pro'; | ||
import Checkbox from '@mui/material/Checkbox'; | ||
import FormControlLabel from '@mui/material/FormControlLabel'; | ||
import { useMockServer } from '@mui/x-data-grid-generator'; | ||
import Alert from '@mui/material/Alert'; | ||
import Button from '@mui/material/Button'; | ||
import Snackbar from '@mui/material/Snackbar'; | ||
|
||
function ErrorSnackbar(props) { | ||
const { onRetry, ...rest } = props; | ||
return ( | ||
<Snackbar {...rest}> | ||
<Alert | ||
severity="error" | ||
variant="filled" | ||
sx={{ width: '100%' }} | ||
action={ | ||
<Button color="inherit" size="small" onClick={onRetry}> | ||
Retry | ||
</Button> | ||
} | ||
> | ||
Failed to fetch row data | ||
</Alert> | ||
</Snackbar> | ||
); | ||
} | ||
|
||
function ServerSideLazyLoadingErrorHandling() { | ||
const apiRef = useGridApiRef(); | ||
const [retryParams, setRetryParams] = React.useState(null); | ||
const [shouldRequestsFail, setShouldRequestsFail] = React.useState(false); | ||
|
||
const { fetchRows, ...props } = useMockServer( | ||
{ rowLength: 100 }, | ||
{ useCursorPagination: false, minDelay: 300, maxDelay: 800 }, | ||
shouldRequestsFail, | ||
); | ||
|
||
const dataSource = React.useMemo( | ||
() => ({ | ||
getRows: async (params) => { | ||
const urlParams = new URLSearchParams({ | ||
filterModel: JSON.stringify(params.filterModel), | ||
sortModel: JSON.stringify(params.sortModel), | ||
start: `${params.start}`, | ||
end: `${params.end}`, | ||
}); | ||
const getRowsResponse = await fetchRows( | ||
`https://mui.com/x/api/data-grid?${urlParams.toString()}`, | ||
); | ||
|
||
// Reset the retryParams when new rows are fetched | ||
setRetryParams(null); | ||
return { | ||
rows: getRowsResponse.rows, | ||
rowCount: getRowsResponse.rowCount, | ||
}; | ||
}, | ||
}), | ||
[fetchRows], | ||
); | ||
|
||
return ( | ||
<div style={{ width: '100%' }}> | ||
<FormControlLabel | ||
control={ | ||
<Checkbox | ||
checked={shouldRequestsFail} | ||
onChange={(event) => setShouldRequestsFail(event.target.checked)} | ||
/> | ||
} | ||
label="Make the requests fail" | ||
/> | ||
<div style={{ height: 400, position: 'relative' }}> | ||
{retryParams && ( | ||
<ErrorSnackbar | ||
open={!!retryParams} | ||
onRetry={() => { | ||
apiRef.current.unstable_dataSource.fetchRows( | ||
GRID_ROOT_GROUP_ID, | ||
retryParams, | ||
); | ||
setRetryParams(null); | ||
}} | ||
/> | ||
)} | ||
<DataGridPro | ||
{...props} | ||
apiRef={apiRef} | ||
unstable_dataSource={dataSource} | ||
unstable_onDataSourceError={(_, params) => setRetryParams(params)} | ||
unstable_dataSourceCache={null} | ||
unstable_lazyLoading | ||
paginationModel={{ page: 0, pageSize: 10 }} | ||
slots={{ toolbar: GridToolbar }} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default ServerSideLazyLoadingErrorHandling; |
113 changes: 113 additions & 0 deletions
113
docs/data/data-grid/server-side-data/ServerSideLazyLoadingErrorHandling.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import * as React from 'react'; | ||
import { | ||
DataGridPro, | ||
useGridApiRef, | ||
GridToolbar, | ||
GridDataSource, | ||
GridGetRowsParams, | ||
GRID_ROOT_GROUP_ID, | ||
} from '@mui/x-data-grid-pro'; | ||
import Checkbox from '@mui/material/Checkbox'; | ||
import FormControlLabel from '@mui/material/FormControlLabel'; | ||
import { useMockServer } from '@mui/x-data-grid-generator'; | ||
import Alert from '@mui/material/Alert'; | ||
import Button from '@mui/material/Button'; | ||
import Snackbar, { SnackbarProps } from '@mui/material/Snackbar'; | ||
|
||
function ErrorSnackbar(props: SnackbarProps & { onRetry: () => void }) { | ||
const { onRetry, ...rest } = props; | ||
return ( | ||
<Snackbar {...rest}> | ||
<Alert | ||
severity="error" | ||
variant="filled" | ||
sx={{ width: '100%' }} | ||
action={ | ||
<Button color="inherit" size="small" onClick={onRetry}> | ||
Retry | ||
</Button> | ||
} | ||
> | ||
Failed to fetch row data | ||
</Alert> | ||
</Snackbar> | ||
); | ||
} | ||
|
||
function ServerSideLazyLoadingErrorHandling() { | ||
const apiRef = useGridApiRef(); | ||
const [retryParams, setRetryParams] = React.useState<GridGetRowsParams | null>( | ||
null, | ||
); | ||
const [shouldRequestsFail, setShouldRequestsFail] = React.useState(false); | ||
|
||
const { fetchRows, ...props } = useMockServer( | ||
{ rowLength: 100 }, | ||
{ useCursorPagination: false, minDelay: 300, maxDelay: 800 }, | ||
shouldRequestsFail, | ||
); | ||
|
||
const dataSource: GridDataSource = React.useMemo( | ||
() => ({ | ||
getRows: async (params) => { | ||
const urlParams = new URLSearchParams({ | ||
filterModel: JSON.stringify(params.filterModel), | ||
sortModel: JSON.stringify(params.sortModel), | ||
start: `${params.start}`, | ||
end: `${params.end}`, | ||
}); | ||
const getRowsResponse = await fetchRows( | ||
`https://mui.com/x/api/data-grid?${urlParams.toString()}`, | ||
); | ||
|
||
// Reset the retryParams when new rows are fetched | ||
setRetryParams(null); | ||
return { | ||
rows: getRowsResponse.rows, | ||
rowCount: getRowsResponse.rowCount, | ||
}; | ||
}, | ||
}), | ||
[fetchRows], | ||
); | ||
|
||
return ( | ||
<div style={{ width: '100%' }}> | ||
<FormControlLabel | ||
control={ | ||
<Checkbox | ||
checked={shouldRequestsFail} | ||
onChange={(event) => setShouldRequestsFail(event.target.checked)} | ||
/> | ||
} | ||
label="Make the requests fail" | ||
/> | ||
<div style={{ height: 400, position: 'relative' }}> | ||
{retryParams && ( | ||
<ErrorSnackbar | ||
open={!!retryParams} | ||
onRetry={() => { | ||
apiRef.current.unstable_dataSource.fetchRows( | ||
GRID_ROOT_GROUP_ID, | ||
retryParams, | ||
); | ||
setRetryParams(null); | ||
}} | ||
/> | ||
)} | ||
<DataGridPro | ||
{...props} | ||
apiRef={apiRef} | ||
unstable_dataSource={dataSource} | ||
unstable_onDataSourceError={(_, params) => setRetryParams(params)} | ||
unstable_dataSourceCache={null} | ||
unstable_lazyLoading | ||
paginationModel={{ page: 0, pageSize: 10 }} | ||
slots={{ toolbar: GridToolbar }} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default ServerSideLazyLoadingErrorHandling; |
44 changes: 44 additions & 0 deletions
44
docs/data/data-grid/server-side-data/ServerSideLazyLoadingInfinite.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as React from 'react'; | ||
import { DataGridPro } from '@mui/x-data-grid-pro'; | ||
import { useMockServer } from '@mui/x-data-grid-generator'; | ||
|
||
function ServerSideLazyLoadingInfinite() { | ||
const { fetchRows, ...props } = useMockServer( | ||
{ rowLength: 100 }, | ||
{ useCursorPagination: false, minDelay: 200, maxDelay: 500 }, | ||
); | ||
|
||
const dataSource = React.useMemo( | ||
() => ({ | ||
getRows: async (params) => { | ||
const urlParams = new URLSearchParams({ | ||
filterModel: JSON.stringify(params.filterModel), | ||
sortModel: JSON.stringify(params.sortModel), | ||
start: `${params.start}`, | ||
end: `${params.end}`, | ||
}); | ||
const getRowsResponse = await fetchRows( | ||
`https://mui.com/x/api/data-grid?${urlParams.toString()}`, | ||
); | ||
|
||
return { | ||
rows: getRowsResponse.rows, | ||
}; | ||
}, | ||
}), | ||
[fetchRows], | ||
); | ||
|
||
return ( | ||
<div style={{ width: '100%', height: 400 }}> | ||
<DataGridPro | ||
{...props} | ||
unstable_dataSource={dataSource} | ||
unstable_lazyLoading | ||
paginationModel={{ page: 0, pageSize: 15 }} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
export default ServerSideLazyLoadingInfinite; |
48 changes: 48 additions & 0 deletions
48
docs/data/data-grid/server-side-data/ServerSideLazyLoadingInfinite.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as React from 'react'; | ||
import { | ||
DataGridPro, | ||
GridDataSource, | ||
GridGetRowsParams, | ||
} from '@mui/x-data-grid-pro'; | ||
import { useMockServer } from '@mui/x-data-grid-generator'; | ||
|
||
function ServerSideLazyLoadingInfinite() { | ||
const { fetchRows, ...props } = useMockServer( | ||
{ rowLength: 100 }, | ||
{ useCursorPagination: false, minDelay: 200, maxDelay: 500 }, | ||
); | ||
|
||
const dataSource: GridDataSource = React.useMemo( | ||
() => ({ | ||
getRows: async (params: GridGetRowsParams) => { | ||
const urlParams = new URLSearchParams({ | ||
filterModel: JSON.stringify(params.filterModel), | ||
sortModel: JSON.stringify(params.sortModel), | ||
start: `${params.start}`, | ||
end: `${params.end}`, | ||
}); | ||
const getRowsResponse = await fetchRows( | ||
`https://mui.com/x/api/data-grid?${urlParams.toString()}`, | ||
); | ||
|
||
return { | ||
rows: getRowsResponse.rows, | ||
}; | ||
}, | ||
}), | ||
[fetchRows], | ||
); | ||
|
||
return ( | ||
<div style={{ width: '100%', height: 400 }}> | ||
<DataGridPro | ||
{...props} | ||
unstable_dataSource={dataSource} | ||
unstable_lazyLoading | ||
paginationModel={{ page: 0, pageSize: 15 }} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
export default ServerSideLazyLoadingInfinite; |
Oops, something went wrong.