-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[DataGridPremium] Server-side aggregation with data source #15741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 32 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
ef16446
[DataGridPremium] Server-side aggregation with data source
MBilalShafi 7d29347
Merge branch 'master' into datasource-aggregation
MBilalShafi f390b5b
Merge branch 'master' into datasource-aggregation
MBilalShafi 681ad6b
Add initial documentation
MBilalShafi 9566e93
Add scope demo
MBilalShafi 972532a
Fix the broken link
MBilalShafi b6637ae
Add backlink
MBilalShafi 49fcad7
Move relevant code to Premium package
MBilalShafi 89cc04d
Rename API method
MBilalShafi fae6dfc
Extend the interface
MBilalShafi 7a959e2
Documentation improvements suggested by @arminmeh
MBilalShafi 86d4c87
Merge branch 'master' into datasource-aggregation
MBilalShafi d3bda4d
Regenerate docs
MBilalShafi ee6b3c8
Remove aggregationRowsScope support
MBilalShafi 7610ea6
Merge branch 'master' into datasource-aggregation
MBilalShafi 23f366a
Make cache work with aggregation
MBilalShafi 406766e
Simplify
MBilalShafi 20dc5f7
Merge branch 'master' into datasource-aggregation
MBilalShafi 38a0a6b
Update
MBilalShafi 0a28c3b
Fix custom cache test
MBilalShafi f9f1280
Add tests for aggregation
MBilalShafi 57405e8
Merge 'master' into datasource-aggregation
MBilalShafi 8387664
Add pipe processing for aggregation processing
MBilalShafi e6a6377
prettier
MBilalShafi acfe113
Merge branch 'master' into datasource-aggregation
MBilalShafi 0dc21c0
Test
MBilalShafi 56af4ff
Documentation updates
MBilalShafi d4b6771
Add lazy-loading demo
MBilalShafi 254f903
Merge branch 'master' into datasource-aggregation
MBilalShafi 4ea37b8
Test restore
MBilalShafi 22ea6fa
Restore the spy before assigning
MBilalShafi f51a062
Merge branch 'master' into datasource-aggregation
MBilalShafi 564c311
Docs improvements
MBilalShafi 2746959
Merge branch 'master' into datasource-aggregation
MBilalShafi 280a539
Merge branch 'master' into datasource-aggregation
MBilalShafi 9a76c97
Improve
MBilalShafi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
70 changes: 70 additions & 0 deletions
70
docs/data/data-grid/server-side-data/ServerSideDataGridAggregation.js
This file contains hidden or 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 } from '@mui/x-data-grid-premium'; | ||
import { useMockServer } from '@mui/x-data-grid-generator'; | ||
|
||
const aggregationFunctions = { | ||
sum: { columnTypes: ['number'] }, | ||
avg: { columnTypes: ['number'] }, | ||
min: { columnTypes: ['number', 'date', 'dateTime'] }, | ||
max: { columnTypes: ['number', 'date', 'dateTime'] }, | ||
size: {}, | ||
}; | ||
|
||
export default function ServerSideDataGridAggregation() { | ||
const { columns, initialState, fetchRows } = useMockServer( | ||
{}, | ||
{ useCursorPagination: false }, | ||
); | ||
|
||
const dataSource = React.useMemo( | ||
() => ({ | ||
getRows: async (params) => { | ||
const urlParams = new URLSearchParams({ | ||
paginationModel: JSON.stringify(params.paginationModel), | ||
filterModel: JSON.stringify(params.filterModel), | ||
sortModel: JSON.stringify(params.sortModel), | ||
aggregationModel: JSON.stringify(params.aggregationModel), | ||
}); | ||
const getRowsResponse = await fetchRows( | ||
`https://mui.com/x/api/data-grid?${urlParams.toString()}`, | ||
); | ||
return { | ||
rows: getRowsResponse.rows, | ||
rowCount: getRowsResponse.rowCount, | ||
aggregateRow: getRowsResponse.aggregateRow, | ||
}; | ||
}, | ||
getAggregatedValue: (row, field) => { | ||
return row[`${field}Aggregate`]; | ||
}, | ||
}), | ||
[fetchRows], | ||
); | ||
|
||
const initialStateWithPagination = React.useMemo( | ||
() => ({ | ||
...initialState, | ||
pagination: { | ||
paginationModel: { pageSize: 10, page: 0 }, | ||
rowCount: 0, | ||
}, | ||
aggregation: { | ||
model: { commodity: 'size', quantity: 'sum' }, | ||
}, | ||
}), | ||
[initialState], | ||
); | ||
|
||
return ( | ||
<div style={{ width: '100%', height: 400 }}> | ||
<DataGridPremium | ||
columns={columns} | ||
unstable_dataSource={dataSource} | ||
pagination | ||
initialState={initialStateWithPagination} | ||
pageSizeOptions={[10, 20, 50]} | ||
aggregationFunctions={aggregationFunctions} | ||
/> | ||
</div> | ||
); | ||
} |
70 changes: 70 additions & 0 deletions
70
docs/data/data-grid/server-side-data/ServerSideDataGridAggregation.tsx
This file contains hidden or 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 } from '@mui/x-data-grid-premium'; | ||
import { useMockServer } from '@mui/x-data-grid-generator'; | ||
|
||
const aggregationFunctions = { | ||
sum: { columnTypes: ['number'] }, | ||
avg: { columnTypes: ['number'] }, | ||
min: { columnTypes: ['number', 'date', 'dateTime'] }, | ||
max: { columnTypes: ['number', 'date', 'dateTime'] }, | ||
size: {}, | ||
}; | ||
|
||
export default function ServerSideDataGridAggregation() { | ||
const { columns, initialState, fetchRows } = useMockServer( | ||
{}, | ||
{ useCursorPagination: false }, | ||
); | ||
|
||
const dataSource: GridDataSource = React.useMemo( | ||
() => ({ | ||
getRows: async (params) => { | ||
const urlParams = new URLSearchParams({ | ||
paginationModel: JSON.stringify(params.paginationModel), | ||
filterModel: JSON.stringify(params.filterModel), | ||
sortModel: JSON.stringify(params.sortModel), | ||
aggregationModel: JSON.stringify(params.aggregationModel), | ||
}); | ||
const getRowsResponse = await fetchRows( | ||
`https://mui.com/x/api/data-grid?${urlParams.toString()}`, | ||
); | ||
return { | ||
rows: getRowsResponse.rows, | ||
rowCount: getRowsResponse.rowCount, | ||
aggregateRow: getRowsResponse.aggregateRow, | ||
}; | ||
}, | ||
getAggregatedValue: (row, field) => { | ||
return row[`${field}Aggregate`]; | ||
}, | ||
}), | ||
[fetchRows], | ||
); | ||
|
||
const initialStateWithPagination = React.useMemo( | ||
() => ({ | ||
...initialState, | ||
pagination: { | ||
paginationModel: { pageSize: 10, page: 0 }, | ||
rowCount: 0, | ||
}, | ||
aggregation: { | ||
model: { commodity: 'size', quantity: 'sum' }, | ||
}, | ||
}), | ||
[initialState], | ||
); | ||
|
||
return ( | ||
<div style={{ width: '100%', height: 400 }}> | ||
<DataGridPremium | ||
columns={columns} | ||
unstable_dataSource={dataSource} | ||
pagination | ||
initialState={initialStateWithPagination} | ||
pageSizeOptions={[10, 20, 50]} | ||
aggregationFunctions={aggregationFunctions} | ||
/> | ||
</div> | ||
); | ||
} |
8 changes: 8 additions & 0 deletions
8
docs/data/data-grid/server-side-data/ServerSideDataGridAggregation.tsx.preview
This file contains hidden or 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,8 @@ | ||
<DataGridPremium | ||
columns={columns} | ||
unstable_dataSource={dataSource} | ||
pagination | ||
initialState={initialStateWithPagination} | ||
pageSizeOptions={[10, 20, 50]} | ||
aggregationFunctions={aggregationFunctions} | ||
/> |
71 changes: 71 additions & 0 deletions
71
docs/data/data-grid/server-side-data/ServerSideDataGridAggregationLazyLoading.js
This file contains hidden or 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,71 @@ | ||
import * as React from 'react'; | ||
import { DataGridPremium } from '@mui/x-data-grid-premium'; | ||
import { useMockServer } from '@mui/x-data-grid-generator'; | ||
|
||
const aggregationFunctions = { | ||
sum: { columnTypes: ['number'] }, | ||
avg: { columnTypes: ['number'] }, | ||
min: { columnTypes: ['number', 'date', 'dateTime'] }, | ||
max: { columnTypes: ['number', 'date', 'dateTime'] }, | ||
size: {}, | ||
}; | ||
|
||
export default function ServerSideDataGridAggregationLazyLoading() { | ||
const { | ||
columns, | ||
initialState: initState, | ||
fetchRows, | ||
} = useMockServer({}, { useCursorPagination: false }); | ||
|
||
const dataSource = React.useMemo( | ||
() => ({ | ||
getRows: async (params) => { | ||
const urlParams = new URLSearchParams({ | ||
filterModel: JSON.stringify(params.filterModel), | ||
sortModel: JSON.stringify(params.sortModel), | ||
aggregationModel: JSON.stringify(params.aggregationModel), | ||
start: `${params.start}`, | ||
end: `${params.end}`, | ||
}); | ||
const getRowsResponse = await fetchRows( | ||
`https://mui.com/x/api/data-grid?${urlParams.toString()}`, | ||
); | ||
return { | ||
rows: getRowsResponse.rows, | ||
rowCount: getRowsResponse.rowCount, | ||
aggregateRow: getRowsResponse.aggregateRow, | ||
}; | ||
}, | ||
getAggregatedValue: (row, field) => { | ||
return row[`${field}Aggregate`]; | ||
}, | ||
}), | ||
[fetchRows], | ||
); | ||
|
||
const initialState = React.useMemo( | ||
() => ({ | ||
...initState, | ||
pagination: { | ||
paginationModel: { pageSize: 10, page: 0 }, | ||
rowCount: 0, | ||
}, | ||
aggregation: { | ||
model: { commodity: 'size', quantity: 'sum' }, | ||
}, | ||
}), | ||
[initState], | ||
); | ||
|
||
return ( | ||
<div style={{ width: '100%', height: 400 }}> | ||
<DataGridPremium | ||
columns={columns} | ||
unstable_dataSource={dataSource} | ||
initialState={initialState} | ||
unstable_lazyLoading | ||
aggregationFunctions={aggregationFunctions} | ||
/> | ||
</div> | ||
); | ||
} |
71 changes: 71 additions & 0 deletions
71
docs/data/data-grid/server-side-data/ServerSideDataGridAggregationLazyLoading.tsx
This file contains hidden or 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,71 @@ | ||
import * as React from 'react'; | ||
import { DataGridPremium, GridDataSource } from '@mui/x-data-grid-premium'; | ||
import { useMockServer } from '@mui/x-data-grid-generator'; | ||
|
||
const aggregationFunctions = { | ||
sum: { columnTypes: ['number'] }, | ||
avg: { columnTypes: ['number'] }, | ||
min: { columnTypes: ['number', 'date', 'dateTime'] }, | ||
max: { columnTypes: ['number', 'date', 'dateTime'] }, | ||
size: {}, | ||
}; | ||
|
||
export default function ServerSideDataGridAggregationLazyLoading() { | ||
const { | ||
columns, | ||
initialState: initState, | ||
fetchRows, | ||
} = useMockServer({}, { useCursorPagination: false }); | ||
|
||
const dataSource: GridDataSource = React.useMemo( | ||
() => ({ | ||
getRows: async (params) => { | ||
const urlParams = new URLSearchParams({ | ||
filterModel: JSON.stringify(params.filterModel), | ||
sortModel: JSON.stringify(params.sortModel), | ||
aggregationModel: JSON.stringify(params.aggregationModel), | ||
start: `${params.start}`, | ||
end: `${params.end}`, | ||
}); | ||
const getRowsResponse = await fetchRows( | ||
`https://mui.com/x/api/data-grid?${urlParams.toString()}`, | ||
); | ||
return { | ||
rows: getRowsResponse.rows, | ||
rowCount: getRowsResponse.rowCount, | ||
aggregateRow: getRowsResponse.aggregateRow, | ||
}; | ||
}, | ||
getAggregatedValue: (row, field) => { | ||
return row[`${field}Aggregate`]; | ||
}, | ||
}), | ||
[fetchRows], | ||
); | ||
|
||
const initialState = React.useMemo( | ||
() => ({ | ||
...initState, | ||
pagination: { | ||
paginationModel: { pageSize: 10, page: 0 }, | ||
rowCount: 0, | ||
}, | ||
aggregation: { | ||
model: { commodity: 'size', quantity: 'sum' }, | ||
}, | ||
}), | ||
[initState], | ||
); | ||
|
||
return ( | ||
<div style={{ width: '100%', height: 400 }}> | ||
<DataGridPremium | ||
columns={columns} | ||
unstable_dataSource={dataSource} | ||
initialState={initialState} | ||
unstable_lazyLoading | ||
aggregationFunctions={aggregationFunctions} | ||
/> | ||
</div> | ||
); | ||
} |
7 changes: 7 additions & 0 deletions
7
docs/data/data-grid/server-side-data/ServerSideDataGridAggregationLazyLoading.tsx.preview
This file contains hidden or 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,7 @@ | ||
<DataGridPremium | ||
columns={columns} | ||
unstable_dataSource={dataSource} | ||
initialState={initialState} | ||
unstable_lazyLoading | ||
aggregationFunctions={aggregationFunctions} | ||
/> |
76 changes: 76 additions & 0 deletions
76
docs/data/data-grid/server-side-data/ServerSideDataGridAggregationRowGrouping.js
This file contains hidden or 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,76 @@ | ||
import * as React from 'react'; | ||
import { | ||
DataGridPremium, | ||
useKeepGroupedColumnsHidden, | ||
useGridApiRef, | ||
} from '@mui/x-data-grid-premium'; | ||
import { useMockServer } from '@mui/x-data-grid-generator'; | ||
|
||
const aggregationFunctions = { | ||
sum: { columnTypes: ['number'] }, | ||
avg: { columnTypes: ['number'] }, | ||
min: { columnTypes: ['number', 'date', 'dateTime'] }, | ||
max: { columnTypes: ['number', 'date', 'dateTime'] }, | ||
size: {}, | ||
}; | ||
|
||
export default function ServerSideDataGridAggregationRowGrouping() { | ||
const apiRef = useGridApiRef(); | ||
const { | ||
columns, | ||
initialState: initState, | ||
fetchRows, | ||
} = useMockServer({ rowGrouping: true }); | ||
|
||
const dataSource = React.useMemo( | ||
() => ({ | ||
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), | ||
aggregationModel: JSON.stringify(params.aggregationModel), | ||
}); | ||
const getRowsResponse = await fetchRows( | ||
`https://mui.com/x/api/data-grid?${urlParams.toString()}`, | ||
); | ||
return { | ||
rows: getRowsResponse.rows, | ||
rowCount: getRowsResponse.rowCount, | ||
aggregateRow: getRowsResponse.aggregateRow, | ||
}; | ||
}, | ||
getGroupKey: (row) => row.group, | ||
getChildrenCount: (row) => row.descendantCount, | ||
getAggregatedValue: (row, field) => row[`${field}Aggregate`], | ||
}), | ||
[fetchRows], | ||
); | ||
|
||
const initialState = useKeepGroupedColumnsHidden({ | ||
apiRef, | ||
initialState: { | ||
...initState, | ||
rowGrouping: { | ||
model: ['company', 'director'], | ||
}, | ||
aggregation: { | ||
model: { title: 'size', gross: 'sum', year: 'max' }, | ||
}, | ||
}, | ||
}); | ||
|
||
return ( | ||
<div style={{ width: '100%', height: 400 }}> | ||
<DataGridPremium | ||
apiRef={apiRef} | ||
columns={columns} | ||
unstable_dataSource={dataSource} | ||
initialState={initialState} | ||
aggregationFunctions={aggregationFunctions} | ||
/> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.