Skip to content

Commit

Permalink
[DataGridPremium] Server-side aggregation with data source (#15741)
Browse files Browse the repository at this point in the history
Co-authored-by: Armin Mehinovic <4390250+arminmeh@users.noreply.github.com>
  • Loading branch information
MBilalShafi and arminmeh authored Jan 9, 2025
1 parent 3b0bba2 commit 296a499
Show file tree
Hide file tree
Showing 57 changed files with 1,579 additions and 123 deletions.
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're looking for aggregation on the server side, see [Server-side data—Aggregation](/x/react-data-grid/server-side-data/aggregation/).
:::

## 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,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>
);
}
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>
);
}
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}
/>
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>
);
}
Loading

0 comments on commit 296a499

Please sign in to comment.