Skip to content
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

[DataGridPremium] Server-side aggregation with data source #15741

Merged
merged 36 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 8 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 Dec 5, 2024
7d29347
Merge branch 'master' into datasource-aggregation
MBilalShafi Dec 5, 2024
f390b5b
Merge branch 'master' into datasource-aggregation
MBilalShafi Dec 10, 2024
681ad6b
Add initial documentation
MBilalShafi Dec 10, 2024
9566e93
Add scope demo
MBilalShafi Dec 10, 2024
972532a
Fix the broken link
MBilalShafi Dec 10, 2024
b6637ae
Add backlink
MBilalShafi Dec 10, 2024
49fcad7
Move relevant code to Premium package
MBilalShafi Dec 11, 2024
89cc04d
Rename API method
MBilalShafi Dec 18, 2024
fae6dfc
Extend the interface
MBilalShafi Dec 18, 2024
7a959e2
Documentation improvements suggested by @arminmeh
MBilalShafi Dec 18, 2024
86d4c87
Merge branch 'master' into datasource-aggregation
MBilalShafi Dec 18, 2024
d3bda4d
Regenerate docs
MBilalShafi Dec 18, 2024
ee6b3c8
Remove aggregationRowsScope support
MBilalShafi Dec 18, 2024
7610ea6
Merge branch 'master' into datasource-aggregation
MBilalShafi Dec 20, 2024
23f366a
Make cache work with aggregation
MBilalShafi Dec 23, 2024
406766e
Simplify
MBilalShafi Dec 23, 2024
20dc5f7
Merge branch 'master' into datasource-aggregation
MBilalShafi Dec 23, 2024
38a0a6b
Update
MBilalShafi Dec 23, 2024
0a28c3b
Fix custom cache test
MBilalShafi Dec 23, 2024
f9f1280
Add tests for aggregation
MBilalShafi Dec 26, 2024
57405e8
Merge 'master' into datasource-aggregation
MBilalShafi Dec 28, 2024
8387664
Add pipe processing for aggregation processing
MBilalShafi Jan 6, 2025
e6a6377
prettier
MBilalShafi Jan 6, 2025
acfe113
Merge branch 'master' into datasource-aggregation
MBilalShafi Jan 6, 2025
0dc21c0
Test
MBilalShafi Jan 6, 2025
56af4ff
Documentation updates
MBilalShafi Jan 7, 2025
d4b6771
Add lazy-loading demo
MBilalShafi Jan 7, 2025
254f903
Merge branch 'master' into datasource-aggregation
MBilalShafi Jan 7, 2025
4ea37b8
Test restore
MBilalShafi Jan 7, 2025
22ea6fa
Restore the spy before assigning
MBilalShafi Jan 7, 2025
f51a062
Merge branch 'master' into datasource-aggregation
MBilalShafi Jan 7, 2025
564c311
Docs improvements
MBilalShafi Jan 9, 2025
2746959
Merge branch 'master' into datasource-aggregation
MBilalShafi Jan 9, 2025
280a539
Merge branch 'master' into datasource-aggregation
MBilalShafi Jan 9, 2025
9a76c97
Improve
MBilalShafi Jan 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/data/data-grid/aggregation/aggregation.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ The aggregated values are rendered in a footer row at the bottom of the Data Gri

{{"demo": "AggregationInitialState.js", "bg": "inline", "defaultCodeOpen": false}}

:::info
If you are looking for aggregation on the server-side, see [server-side aggregation](/x/react-data-grid/server-side-data/aggregation/).
MBilalShafi marked this conversation as resolved.
Show resolved Hide resolved
:::

## Pass aggregation to the Data Grid

### Structure of the model
Expand Down
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>
);
}
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>
);
}
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}
/>
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>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as React from 'react';
import {
DataGridPremium,
useKeepGroupedColumnsHidden,
useGridApiRef,
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 ServerSideDataGridAggregationRowGrouping() {
const apiRef = useGridApiRef();
const {
columns,
initialState: initState,
fetchRows,
} = useMockServer({ rowGrouping: true });

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),
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>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<DataGridPremium
apiRef={apiRef}
columns={columns}
unstable_dataSource={dataSource}
initialState={initialState}
aggregationFunctions={aggregationFunctions}
/>
Loading
Loading