Backend API
import React, { useCallback, useRef } from 'react';
import Button from 'devextreme-react/button';
import TabPanel, { Item } from 'devextreme-react/tab-panel';
import DataGrid, { Column } from 'devextreme-react/data-grid';
import type { DataGridRef } from 'devextreme-react/data-grid';
import { exportDataGrid } from 'devextreme-react/common/export/pdf';
import type { DataGridCell } from 'devextreme-react/common/export/pdf';
import { ArrayStore, DataSource } from 'devextreme-react/common/data';
import { jsPDF } from 'jspdf';
import { products, type Product } from './data.ts';
const store = new ArrayStore<Product, number>({
data: products,
key: 'Product_ID',
});
const priceDataSource = new DataSource<Product, number>({
store,
select: ['Product_ID', 'Product_Name', 'Product_Sale_Price', 'Product_Retail_Price'],
filter: ['Product_ID', '<', 10],
});
const ratingDataSource = new DataSource<Product, number>({
store,
select: ['Product_ID', 'Product_Name', 'Product_Consumer_Rating', 'Product_Category'],
filter: ['Product_ID', '<', 10],
});
interface PDFCell {
backgroundColor?: string;
}
const setAlternatingRowsBackground = (dataGrid: DataGridRef | null, gridCell: DataGridCell, pdfCell: PDFCell) => {
if (dataGrid && gridCell?.rowType === 'data') {
const rowIndex = dataGrid.instance().getRowIndexByKey(gridCell.data.Product_ID);
if (rowIndex % 2 === 0) {
pdfCell.backgroundColor = '#D3D3D3';
}
}
};
const App = () => {
const priceGridRef = useRef<DataGridRef>(null);
const ratingGridRef = useRef<DataGridRef>(null);
const exportGrids = useCallback(() => {
const doc = new jsPDF();
exportDataGrid({
jsPDFDocument: doc,
component: priceGridRef.current?.instance(),
topLeft: { x: 7, y: 5 },
columnWidths: [20, 50, 50, 50],
customizeCell: ({ gridCell, pdfCell }) => {
if (gridCell && pdfCell) {
setAlternatingRowsBackground(priceGridRef.current, gridCell, pdfCell);
}
},
}).then(() => {
doc.addPage();
exportDataGrid({
jsPDFDocument: doc,
component: ratingGridRef.current?.instance(),
topLeft: { x: 7, y: 5 },
columnWidths: [20, 50, 50, 50],
customizeCell: ({ gridCell, pdfCell }) => {
if (gridCell && pdfCell) {
setAlternatingRowsBackground(ratingGridRef.current, gridCell, pdfCell);
}
},
}).then(() => {
doc.save('MultipleGrids.pdf');
});
});
}, []);
return (
<div>
<div id="exportContainer">
<Button
text="Export multiple grids"
icon="exportpdf"
onClick={exportGrids}
/>
</div>
<TabPanel id="tabPanel" deferRendering={false}>
<Item title="Price">
<DataGrid
id="priceDataGrid"
ref={priceGridRef}
dataSource={priceDataSource}
showBorders={true}
rowAlternationEnabled={true}
>
<Column dataField="Product_ID" caption="ID" width={50} />
<Column dataField="Product_Name" caption="Name" />
<Column dataField="Product_Sale_Price" caption="Sale Price" dataType="number" format="currency" />
<Column dataField="Product_Retail_Price" caption="Retail Price" dataType="number" format="currency" />
</DataGrid>
</Item>
<Item title="Rating">
<DataGrid
id="ratingDataGrid"
ref={ratingGridRef}
dataSource={ratingDataSource}
showBorders={true}
rowAlternationEnabled={true}
>
<Column dataField="Product_ID" caption="ID" width={50} />
<Column dataField="Product_Name" caption="Name" />
<Column dataField="Product_Consumer_Rating" caption="Rating" />
<Column dataField="Product_Category" caption="Category" />
</DataGrid>
</Item>
</TabPanel>
</div>
);
};
export default App;
import React, { useCallback, useRef } from 'react';
import Button from 'devextreme-react/button';
import TabPanel, { Item } from 'devextreme-react/tab-panel';
import DataGrid, { Column } from 'devextreme-react/data-grid';
import { exportDataGrid } from 'devextreme-react/common/export/pdf';
import { ArrayStore, DataSource } from 'devextreme-react/common/data';
import { jsPDF } from 'jspdf';
import { products } from './data.js';
const store = new ArrayStore({
data: products,
key: 'Product_ID',
});
const priceDataSource = new DataSource({
store,
select: ['Product_ID', 'Product_Name', 'Product_Sale_Price', 'Product_Retail_Price'],
filter: ['Product_ID', '<', 10],
});
const ratingDataSource = new DataSource({
store,
select: ['Product_ID', 'Product_Name', 'Product_Consumer_Rating', 'Product_Category'],
filter: ['Product_ID', '<', 10],
});
const setAlternatingRowsBackground = (dataGrid, gridCell, pdfCell) => {
if (dataGrid && gridCell?.rowType === 'data') {
const rowIndex = dataGrid.instance().getRowIndexByKey(gridCell.data.Product_ID);
if (rowIndex % 2 === 0) {
pdfCell.backgroundColor = '#D3D3D3';
}
}
};
const App = () => {
const priceGridRef = useRef(null);
const ratingGridRef = useRef(null);
const exportGrids = useCallback(() => {
const doc = new jsPDF();
exportDataGrid({
jsPDFDocument: doc,
component: priceGridRef.current?.instance(),
topLeft: { x: 7, y: 5 },
columnWidths: [20, 50, 50, 50],
customizeCell: ({ gridCell, pdfCell }) => {
if (gridCell && pdfCell) {
setAlternatingRowsBackground(priceGridRef.current, gridCell, pdfCell);
}
},
}).then(() => {
doc.addPage();
exportDataGrid({
jsPDFDocument: doc,
component: ratingGridRef.current?.instance(),
topLeft: { x: 7, y: 5 },
columnWidths: [20, 50, 50, 50],
customizeCell: ({ gridCell, pdfCell }) => {
if (gridCell && pdfCell) {
setAlternatingRowsBackground(ratingGridRef.current, gridCell, pdfCell);
}
},
}).then(() => {
doc.save('MultipleGrids.pdf');
});
});
}, []);
return (
<div>
<div id="exportContainer">
<Button
text="Export multiple grids"
icon="exportpdf"
onClick={exportGrids}
/>
</div>
<TabPanel
id="tabPanel"
deferRendering={false}
>
<Item title="Price">
<DataGrid
id="priceDataGrid"
ref={priceGridRef}
dataSource={priceDataSource}
showBorders={true}
rowAlternationEnabled={true}
>
<Column
dataField="Product_ID"
caption="ID"
width={50}
/>
<Column
dataField="Product_Name"
caption="Name"
/>
<Column
dataField="Product_Sale_Price"
caption="Sale Price"
dataType="number"
format="currency"
/>
<Column
dataField="Product_Retail_Price"
caption="Retail Price"
dataType="number"
format="currency"
/>
</DataGrid>
</Item>
<Item title="Rating">
<DataGrid
id="ratingDataGrid"
ref={ratingGridRef}
dataSource={ratingDataSource}
showBorders={true}
rowAlternationEnabled={true}
>
<Column
dataField="Product_ID"
caption="ID"
width={50}
/>
<Column
dataField="Product_Name"
caption="Name"
/>
<Column
dataField="Product_Consumer_Rating"
caption="Rating"
/>
<Column
dataField="Product_Category"
caption="Category"
/>
</DataGrid>
</Item>
</TabPanel>
</div>
);
};
export default App;
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.tsx';
ReactDOM.render(
<App />,
document.getElementById('app'),
);
export interface Product {
Product_ID: number;
Product_Name: string;
Product_Sale_Price: number;
Product_Retail_Price: number;
Product_Consumer_Rating: number;
Product_Category: string;
}
export const products: Product[] = [
{
Product_ID: 1,
Product_Name: "HD Video Player",
Product_Sale_Price: 220.00,
Product_Retail_Price: 330.00,
Product_Consumer_Rating: 4,
Product_Category: "Video Players",
},
{
Product_ID: 2,
Product_Name: "SuperHD Video Player",
Product_Sale_Price: 275.00,
Product_Retail_Price: 400.00,
Product_Consumer_Rating: 4,
Product_Category: "Video Players",
},
{
Product_ID: 3,
Product_Name: "SuperPlasma 50",
Product_Sale_Price: 1800.00,
Product_Retail_Price: 2400.00,
Product_Consumer_Rating: 4.5,
Product_Category: "Televisions",
},
{
Product_ID: 4,
Product_Name: "SuperLED 50",
Product_Sale_Price: 1100.00,
Product_Retail_Price: 1600.00,
Product_Consumer_Rating: 5,
Product_Category: "Televisions",
},
{
Product_ID: 5,
Product_Name: "SuperLED 42",
Product_Sale_Price: 1050.00,
Product_Retail_Price: 1450.00,
Product_Consumer_Rating: 5,
Product_Category: "Televisions",
},
{
Product_ID: 6,
Product_Name: "SuperLCD 55",
Product_Sale_Price: 1045.00,
Product_Retail_Price: 1350.00,
Product_Consumer_Rating: 4.5,
Product_Category: "Televisions",
},
{
Product_ID: 7,
Product_Name: "SuperLCD 42",
Product_Sale_Price: 999.00,
Product_Retail_Price: 1200.00,
Product_Consumer_Rating: 4,
Product_Category: "Televisions",
},
{
Product_ID: 8,
Product_Name: "SuperPlasma 65",
Product_Sale_Price: 2900.00,
Product_Retail_Price: 3500.00,
Product_Consumer_Rating: 3,
Product_Category: "Televisions",
},
{
Product_ID: 9,
Product_Name: "SuperLCD 70",
Product_Sale_Price: 3200.00,
Product_Retail_Price: 4000.00,
Product_Consumer_Rating: 4,
Product_Category: "Televisions",
},
];
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.js';
ReactDOM.render(<App />, document.getElementById('app'));
export const products = [
{
Product_ID: 1,
Product_Name: 'HD Video Player',
Product_Sale_Price: 220.0,
Product_Retail_Price: 330.0,
Product_Consumer_Rating: 4,
Product_Category: 'Video Players',
},
{
Product_ID: 2,
Product_Name: 'SuperHD Video Player',
Product_Sale_Price: 275.0,
Product_Retail_Price: 400.0,
Product_Consumer_Rating: 4,
Product_Category: 'Video Players',
},
{
Product_ID: 3,
Product_Name: 'SuperPlasma 50',
Product_Sale_Price: 1800.0,
Product_Retail_Price: 2400.0,
Product_Consumer_Rating: 4.5,
Product_Category: 'Televisions',
},
{
Product_ID: 4,
Product_Name: 'SuperLED 50',
Product_Sale_Price: 1100.0,
Product_Retail_Price: 1600.0,
Product_Consumer_Rating: 5,
Product_Category: 'Televisions',
},
{
Product_ID: 5,
Product_Name: 'SuperLED 42',
Product_Sale_Price: 1050.0,
Product_Retail_Price: 1450.0,
Product_Consumer_Rating: 5,
Product_Category: 'Televisions',
},
{
Product_ID: 6,
Product_Name: 'SuperLCD 55',
Product_Sale_Price: 1045.0,
Product_Retail_Price: 1350.0,
Product_Consumer_Rating: 4.5,
Product_Category: 'Televisions',
},
{
Product_ID: 7,
Product_Name: 'SuperLCD 42',
Product_Sale_Price: 999.0,
Product_Retail_Price: 1200.0,
Product_Consumer_Rating: 4,
Product_Category: 'Televisions',
},
{
Product_ID: 8,
Product_Name: 'SuperPlasma 65',
Product_Sale_Price: 2900.0,
Product_Retail_Price: 3500.0,
Product_Consumer_Rating: 3,
Product_Category: 'Televisions',
},
{
Product_ID: 9,
Product_Name: 'SuperLCD 70',
Product_Sale_Price: 3200.0,
Product_Retail_Price: 4000.0,
Product_Consumer_Rating: 4,
Product_Category: 'Televisions',
},
];
<!DOCTYPE html>
<html lang="en">
<head>
<title>DevExtreme Demo</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0" />
<link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/26.1.5/css/dx.light.css" />
<link rel="stylesheet" type="text/css" href="./bundle.css" />
</head>
<body class="dx-viewport">
<div class="demo-container">
<div id="app"></div>
</div>
<script src="bundles/vendor/react.vendor.js"></script>
<script src="./bundle.js"></script>
</body>
</html>
#priceDataGrid,
#ratingDataGrid {
padding: 10px;
}
#exportContainer {
text-align: right;
}
#tabPanel {
padding-top: 10px;
}