-
-
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 aggregation with data source (#15741)
Co-authored-by: Armin Mehinovic <4390250+arminmeh@users.noreply.github.com>
- Loading branch information
1 parent
3b0bba2
commit 296a499
Showing
57 changed files
with
1,579 additions
and
123 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
70 changes: 70 additions & 0 deletions
70
docs/data/data-grid/server-side-data/ServerSideDataGridAggregation.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,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 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 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 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 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 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 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.