-
-
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.
[DataGridPremium] Server-side data source with row grouping (#13826)
Co-authored-by: Armin Mehinovic <4390250+arminmeh@users.noreply.github.com>
- Loading branch information
1 parent
ab4823d
commit ae72b8c
Showing
36 changed files
with
1,557 additions
and
116 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
69 changes: 69 additions & 0 deletions
69
docs/data/data-grid/server-side-data/ServerSideRowGroupingDataGrid.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,69 @@ | ||
import * as React from 'react'; | ||
import { | ||
DataGridPremium, | ||
useGridApiRef, | ||
useKeepGroupedColumnsHidden, | ||
} from '@mui/x-data-grid-premium'; | ||
import { useMockServer } from '@mui/x-data-grid-generator'; | ||
import Button from '@mui/material/Button'; | ||
|
||
export default function ServerSideRowGroupingDataGrid() { | ||
const apiRef = useGridApiRef(); | ||
|
||
const { fetchRows, columns } = useMockServer({ | ||
rowGrouping: true, | ||
}); | ||
|
||
const dataSource = React.useMemo(() => { | ||
return { | ||
getRows: async (params) => { | ||
const urlParams = new URLSearchParams({ | ||
paginationModel: JSON.stringify(params.paginationModel), | ||
filterModel: JSON.stringify(params.filterModel), | ||
sortModel: JSON.stringify(params.sortModel), | ||
groupKeys: JSON.stringify(params.groupKeys), | ||
groupFields: JSON.stringify(params.groupFields), | ||
}); | ||
const getRowsResponse = await fetchRows( | ||
`https://mui.com/x/api/data-grid?${urlParams.toString()}`, | ||
); | ||
return { | ||
rows: getRowsResponse.rows, | ||
rowCount: getRowsResponse.rowCount, | ||
}; | ||
}, | ||
getGroupKey: (row) => row.group, | ||
getChildrenCount: (row) => row.descendantCount, | ||
}; | ||
}, [fetchRows]); | ||
|
||
const initialState = useKeepGroupedColumnsHidden({ | ||
apiRef, | ||
initialState: { | ||
rowGrouping: { | ||
model: ['company', 'director'], | ||
}, | ||
}, | ||
}); | ||
|
||
return ( | ||
<div style={{ width: '100%' }}> | ||
<Button | ||
onClick={() => { | ||
apiRef.current.unstable_dataSource.cache.clear(); | ||
}} | ||
> | ||
Clear cache | ||
</Button> | ||
|
||
<div style={{ height: 400, position: 'relative' }}> | ||
<DataGridPremium | ||
columns={columns} | ||
unstable_dataSource={dataSource} | ||
apiRef={apiRef} | ||
initialState={initialState} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} |
70 changes: 70 additions & 0 deletions
70
docs/data/data-grid/server-side-data/ServerSideRowGroupingDataGrid.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,70 @@ | ||
import * as React from 'react'; | ||
import { | ||
DataGridPremium, | ||
GridDataSource, | ||
useGridApiRef, | ||
useKeepGroupedColumnsHidden, | ||
} from '@mui/x-data-grid-premium'; | ||
import { useMockServer } from '@mui/x-data-grid-generator'; | ||
import Button from '@mui/material/Button'; | ||
|
||
export default function ServerSideRowGroupingDataGrid() { | ||
const apiRef = useGridApiRef(); | ||
|
||
const { fetchRows, columns } = useMockServer({ | ||
rowGrouping: true, | ||
}); | ||
|
||
const dataSource: GridDataSource = React.useMemo(() => { | ||
return { | ||
getRows: async (params) => { | ||
const urlParams = new URLSearchParams({ | ||
paginationModel: JSON.stringify(params.paginationModel), | ||
filterModel: JSON.stringify(params.filterModel), | ||
sortModel: JSON.stringify(params.sortModel), | ||
groupKeys: JSON.stringify(params.groupKeys), | ||
groupFields: JSON.stringify(params.groupFields), | ||
}); | ||
const getRowsResponse = await fetchRows( | ||
`https://mui.com/x/api/data-grid?${urlParams.toString()}`, | ||
); | ||
return { | ||
rows: getRowsResponse.rows, | ||
rowCount: getRowsResponse.rowCount, | ||
}; | ||
}, | ||
getGroupKey: (row) => row.group, | ||
getChildrenCount: (row) => row.descendantCount, | ||
}; | ||
}, [fetchRows]); | ||
|
||
const initialState = useKeepGroupedColumnsHidden({ | ||
apiRef, | ||
initialState: { | ||
rowGrouping: { | ||
model: ['company', 'director'], | ||
}, | ||
}, | ||
}); | ||
|
||
return ( | ||
<div style={{ width: '100%' }}> | ||
<Button | ||
onClick={() => { | ||
apiRef.current.unstable_dataSource.cache.clear(); | ||
}} | ||
> | ||
Clear cache | ||
</Button> | ||
|
||
<div style={{ height: 400, position: 'relative' }}> | ||
<DataGridPremium | ||
columns={columns} | ||
unstable_dataSource={dataSource} | ||
apiRef={apiRef} | ||
initialState={initialState} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} |
16 changes: 16 additions & 0 deletions
16
docs/data/data-grid/server-side-data/ServerSideRowGroupingDataGrid.tsx.preview
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,16 @@ | ||
<Button | ||
onClick={() => { | ||
apiRef.current.unstable_dataSource.cache.clear(); | ||
}} | ||
> | ||
Clear cache | ||
</Button> | ||
|
||
<div style={{ height: 400, position: 'relative' }}> | ||
<DataGridPremium | ||
columns={columns} | ||
unstable_dataSource={dataSource} | ||
apiRef={apiRef} | ||
initialState={initialState} | ||
/> | ||
</div> |
137 changes: 137 additions & 0 deletions
137
docs/data/data-grid/server-side-data/ServerSideRowGroupingErrorHandling.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,137 @@ | ||
import * as React from 'react'; | ||
import { | ||
DataGridPremium, | ||
useGridApiRef, | ||
useKeepGroupedColumnsHidden, | ||
} from '@mui/x-data-grid-premium'; | ||
import { useMockServer } from '@mui/x-data-grid-generator'; | ||
import Snackbar from '@mui/material/Snackbar'; | ||
import Button from '@mui/material/Button'; | ||
import Checkbox from '@mui/material/Checkbox'; | ||
import FormControlLabel from '@mui/material/FormControlLabel'; | ||
import { alpha, styled, darken, lighten } from '@mui/material/styles'; | ||
|
||
export default function ServerSideRowGroupingErrorHandling() { | ||
const apiRef = useGridApiRef(); | ||
const [rootError, setRootError] = React.useState(); | ||
const [childrenError, setChildrenError] = React.useState(); | ||
const [shouldRequestsFail, setShouldRequestsFail] = React.useState(false); | ||
|
||
const { fetchRows, columns } = useMockServer( | ||
{ | ||
rowGrouping: true, | ||
}, | ||
{}, | ||
shouldRequestsFail, | ||
); | ||
|
||
const dataSource = React.useMemo(() => { | ||
return { | ||
getRows: async (params) => { | ||
const urlParams = new URLSearchParams({ | ||
paginationModel: JSON.stringify(params.paginationModel), | ||
filterModel: JSON.stringify(params.filterModel), | ||
sortModel: JSON.stringify(params.sortModel), | ||
groupKeys: JSON.stringify(params.groupKeys), | ||
groupFields: JSON.stringify(params.groupFields), | ||
}); | ||
const getRowsResponse = await fetchRows( | ||
`https://mui.com/x/api/data-grid?${urlParams.toString()}`, | ||
); | ||
return { | ||
rows: getRowsResponse.rows, | ||
rowCount: getRowsResponse.rowCount, | ||
}; | ||
}, | ||
getGroupKey: (row) => row.group, | ||
getChildrenCount: (row) => row.descendantCount, | ||
}; | ||
}, [fetchRows]); | ||
|
||
const initialState = useKeepGroupedColumnsHidden({ | ||
apiRef, | ||
initialState: { | ||
rowGrouping: { | ||
model: ['company', 'director'], | ||
}, | ||
}, | ||
}); | ||
|
||
return ( | ||
<div style={{ width: '100%' }}> | ||
<div style={{ display: 'flex', justifyContent: 'space-between' }}> | ||
<Button | ||
onClick={() => { | ||
setRootError(''); | ||
apiRef.current.unstable_dataSource.fetchRows(); | ||
}} | ||
> | ||
Refetch rows | ||
</Button> | ||
<FormControlLabel | ||
control={ | ||
<Checkbox | ||
checked={shouldRequestsFail} | ||
onChange={(event) => setShouldRequestsFail(event.target.checked)} | ||
/> | ||
} | ||
label="Make the requests fail" | ||
/> | ||
</div> | ||
<div style={{ height: 400, position: 'relative' }}> | ||
<DataGridPremium | ||
columns={columns} | ||
unstable_dataSource={dataSource} | ||
unstable_onDataSourceError={(error, params) => { | ||
if (!params.groupKeys || params.groupKeys.length === 0) { | ||
setRootError(error.message); | ||
} else { | ||
setChildrenError( | ||
`${error.message} (Requested level: ${params.groupKeys.join(' > ')})`, | ||
); | ||
} | ||
}} | ||
unstable_dataSourceCache={null} | ||
apiRef={apiRef} | ||
initialState={initialState} | ||
/> | ||
{rootError && <ErrorOverlay error={rootError} />} | ||
<Snackbar | ||
open={!!childrenError} | ||
autoHideDuration={3000} | ||
onClose={() => setChildrenError('')} | ||
message={childrenError} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
function getBorderColor(theme) { | ||
if (theme.palette.mode === 'light') { | ||
return lighten(alpha(theme.palette.divider, 1), 0.88); | ||
} | ||
return darken(alpha(theme.palette.divider, 1), 0.68); | ||
} | ||
|
||
const StyledDiv = styled('div')(({ theme: t }) => ({ | ||
position: 'absolute', | ||
zIndex: 10, | ||
fontSize: '0.875em', | ||
top: 0, | ||
height: '100%', | ||
width: '100%', | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
borderRadius: '4px', | ||
border: `1px solid ${getBorderColor(t)}`, | ||
backgroundColor: t.palette.background.default, | ||
})); | ||
|
||
function ErrorOverlay({ error }) { | ||
if (!error) { | ||
return null; | ||
} | ||
return <StyledDiv>{error}</StyledDiv>; | ||
} |
Oops, something went wrong.