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

[DataGrid] Add support for getRowsToExport option to print export #10084

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as React from 'react';
import { unstable_ownerDocument as ownerDocument } from '@mui/utils';
import { GridPrivateApiCommunity } from '../../../models/api/gridApiCommunity';
import { GridApiCommunity, GridPrivateApiCommunity } from '../../../models/api/gridApiCommunity';
import { GridPrintExportApi } from '../../../models/api/gridPrintExportApi';
import { useGridLogger } from '../../utils/useGridLogger';
import { gridExpandedRowCountSelector } from '../filter/gridFilterSelector';
import { DataGridProcessedProps } from '../../../models/props/DataGridProps';
import { GridPrintExportOptions } from '../../../models/gridExport';
import { GridPrintExportOptions, GridPrintGetRowsToExportParams } from '../../../models/gridExport';
import { GridRowId, GridValidRowModel } from '../../../models/gridRows';
import { GridInitialStateCommunity } from '../../../models/gridStateCommunity';
import {
gridColumnDefinitionsSelector,
Expand All @@ -14,7 +15,7 @@ import {
import { gridClasses } from '../../../constants/gridClasses';
import { useGridApiMethod } from '../../utils/useGridApiMethod';
import { gridRowsMetaSelector } from '../rows/gridRowsMetaSelector';
import { getColumnsToExport } from './utils';
import { getColumnsToExport, defaultGetRowsToExport } from './utils';
import { mergeStateWithPaginationModel } from '../pagination/useGridPagination';
import { GridPipeProcessor, useGridRegisterPipeProcessor } from '../../core/pipeProcessing';
import {
Expand Down Expand Up @@ -62,6 +63,7 @@ export const useGridPrintExport = (
const doc = React.useRef<Document | null>(null);
const previousGridState = React.useRef<GridInitialStateCommunity | null>(null);
const previousColumnVisibility = React.useRef<{ [key: string]: boolean }>({});
const previousRows = React.useRef<GridValidRowModel[]>([]);

React.useEffect(() => {
doc.current = ownerDocument(apiRef.current.rootElementRef!.current!);
Expand Down Expand Up @@ -90,6 +92,20 @@ export const useGridPrintExport = (
[apiRef],
);

const updateGridRowsForPrint = React.useCallback(
(rowsToExport?: (params: GridPrintGetRowsToExportParams<GridApiCommunity>) => GridRowId[]) =>
new Promise<void>((resolve) => {
zreecespieces marked this conversation as resolved.
Show resolved Hide resolved
const getRowsToExport = rowsToExport ?? defaultGetRowsToExport;
const rowsToExportIds = getRowsToExport({ apiRef });

const newRows = rowsToExportIds.map((id) => apiRef.current.getRow(id));

apiRef.current.setRows(newRows);
resolve();
}),
[apiRef],
);

const handlePrintWindowLoad: PrintWindowOnLoad = React.useCallback(
(printWindow, options): void => {
const normalizeOptions = {
Expand Down Expand Up @@ -245,10 +261,12 @@ export const useGridPrintExport = (
}

apiRef.current.unstable_enableVirtualization();
apiRef.current.setRows(previousRows.current);

// Clear local state
previousGridState.current = null;
previousColumnVisibility.current = {};
previousRows.current = [];
},
[apiRef],
);
Expand All @@ -264,6 +282,7 @@ export const useGridPrintExport = (
previousGridState.current = apiRef.current.exportState();
// It appends that the visibility model is not exported, especially if columnVisibility is not controlled
previousColumnVisibility.current = gridColumnVisibilityModelSelector(apiRef);
previousRows.current = apiRef.current.getSortedRows();

if (props.pagination) {
const visibleRowCount = gridExpandedRowCountSelector(apiRef);
Expand All @@ -280,6 +299,7 @@ export const useGridPrintExport = (
}

await updateGridColumnsForPrint(options?.fields, options?.allColumns);
await updateGridRowsForPrint(options?.getRowsToExport);
apiRef.current.unstable_disableVirtualization();
await raf(); // wait for the state changes to take action
const printWindow = buildPrintWindow(options?.fileName);
Expand Down Expand Up @@ -310,6 +330,7 @@ export const useGridPrintExport = (
handlePrintWindowLoad,
handlePrintWindowAfterPrint,
updateGridColumnsForPrint,
updateGridRowsForPrint,
],
);

Expand Down
9 changes: 9 additions & 0 deletions packages/grid/x-data-grid/src/models/gridExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export interface GridGetRowsToExportParams<Api extends GridApiCommon = GridApiCo
export interface GridCsvGetRowsToExportParams<Api extends GridApiCommon = GridApiCommunity>
extends GridGetRowsToExportParams<Api> {}

export interface GridPrintGetRowsToExportParams<Api extends GridApiCommon = GridApiCommunity>
extends GridGetRowsToExportParams<Api> {}

/**
* The options to apply on the CSV export.
* @demos
Expand Down Expand Up @@ -129,6 +132,12 @@ export interface GridPrintExportOptions extends GridExportOptions {
* Provide Print specific styles to the print window.
*/
pageStyle?: string | Function;
/**
* Function that returns the id of the rows to export in the order they should be exported.
* @param {GridPrintGetRowsToExportParams} params With all properties from [[GridPrintGetRowsToExportParams]].
* @returns {GridRowId[]} The id of the rows to export.
*/
getRowsToExport?: (params: GridPrintGetRowsToExportParams) => GridRowId[];
}

/**
Expand Down