The drill through functionality of the React Pivot Grid . This feature allows you to find the records in the data source which were used to calculate the value of a particular cell in the pivot grid. To drill through a specific grid cell double click on it and you will see a window displaying the source records. In addition to the pivot grid component, this example uses the window and datatable components. The example also demonstrates how to handle open and close events of the window component in React and how to enable or disable the selection functionality of the pivot grid component.
A live React example of jqxPivotGrid, the jQWidgets PivotGrid component, with its complete source code below. It is one of 12 jqxPivotGrid demos for React.
source property – Gets or sets pivot source adapter used to supply data to the pivot grid.multipleSelectionEnabled property – Gets or sets whether the multiple selection in the pivot grid is enabled or disabled.treeStyleRows property – Gets or sets the rows of the pivot grid are displayed as a tree structure or using classic OLAP style.autoResize property – Gets or sets if the size of pivot grid adjusts automatically to display the entire content.theme property – Sets the widget's theme.pivotcelldblclick event – This event is triggered on double click over a pivot grid cell.getInstance method – Returns the instance of the pivot grid componentgetPivotRows method – Return the pivot rows of the pivot gridgetPivotColumns method – Return the pivot columns of the pivot gridgetPivotCells method – Return the pivot cells of the pivot gridTo run it in your own app, install the package with npm install jqwidgets-scripts, add these files to a Vite React and TypeScript project, and import jqx.base.css and a theme in main.tsx. Getting started with jqxPivotGrid shows the setup step by step.
import * as React from 'react';
import { useRef, useEffect, useState } from 'react';
import JqxDataTable, { type IDataTableProps } from 'jqwidgets-scripts/jqwidgets-react-tsx/jqxdatatable';
import JqxPivotGrid, { type IPivotGridItem, jqx } from 'jqwidgets-scripts/jqwidgets-react-tsx/jqxpivotgrid';
import JqxWindow from 'jqwidgets-scripts/jqwidgets-react-tsx/jqxwindow';
type PivotGridRef = JqxPivotGrid | null;
type PivotWindowRef = JqxWindow | null;
type DataTableRef = JqxDataTable | null;
type SampleData = Array<Record<string, unknown>>;
function createPivotDataSource(jqxRef: typeof jqx, sampleDataRef: React.MutableRefObject<SampleData | null>) {
const data: SampleData = [];
const firstNames = [
'Andrew', 'Nancy', 'Shelley', 'Regina', 'Yoshi', 'Antoni', 'Mayumi', 'Ian', 'Peter', 'Lars', 'Petra', 'Martin', 'Sven', 'Elio', 'Beate', 'Cheryl', 'Michael', 'Guylene'
];
const lastNames = [
'Fuller', 'Davolio', 'Burke', 'Murphy', 'Nagase', 'Saavedra', 'Ohno', 'Devling', 'Wilson', 'Peterson', 'Winkler', 'Bein', 'Petersen', 'Rossi', 'Vileid', 'Saylor', 'Bjorn', 'Nodier'
];
const productNames = [
'Black Tea', 'Green Tea', 'Caffe Espresso', 'Doubleshot Espresso', 'Caffe Latte', 'White Chocolate Mocha', 'Cramel Latte', 'Caffe Americano', 'Cappuccino', 'Espresso Truffle', 'Espresso con Panna', 'Peppermint Mocha Twist'
];
const priceValues = [
'2.25', '1.5', '3.0', '3.3', '4.5', '3.6', '3.8', '2.5', '5.0', '1.75', '3.25', '4.0'
];
for (let i = 0; i < 500; i++) {
const row: Record<string, unknown> = {};
const productindex = Math.floor(Math.random() * productNames.length);
const price = parseFloat(priceValues[productindex]);
const quantity = 1 + Math.round(Math.random() * 10);
row.firstname = firstNames[Math.floor(Math.random() * firstNames.length)];
row.lastname = lastNames[Math.floor(Math.random() * lastNames.length)];
row.productname = productNames[productindex];
row.price = price;
row.quantity = quantity;
row.total = price * quantity;
data.push(row);
}
sampleDataRef.current = data;
const source = {
datafields: [
{ name: 'firstname', type: 'string' },
{ name: 'lastname', type: 'string' },
{ name: 'productname', type: 'string' },
{ name: 'quantity', type: 'number' },
{ name: 'price', type: 'number' },
{ name: 'total', type: 'number' }
],
datatype: 'array',
localdata: data
};
const dataAdapter = new jqxRef.dataAdapter(source);
dataAdapter.dataBind();
const pivotDataSource = new jqxRef.pivot(
dataAdapter,
{
columns: [{ dataField: 'productname' }],
pivotValuesOnRows: false,
rows: [{ dataField: 'firstname' }, { dataField: 'lastname' }],
totals: { rows: { subtotals: true, grandtotals: true }, columns: { subtotals: false, grandtotals: true } },
values: [
{ dataField: 'price', 'function': 'sum', text: 'sum', formatSettings: { prefix: '$', decimalPlaces: 2 } },
{ dataField: 'price', 'function': 'count', text: 'count' },
{ dataField: 'price', 'function': 'average', text: 'average', formatSettings: { prefix: '$', decimalPlaces: 2 } }
]
}
);
return pivotDataSource;
}
function App() {
const myPivotGrid = useRef<PivotGridRef>(null);
const drillThroughWindow = useRef<PivotWindowRef>(null);
const tableSrcRecords = useRef<DataTableRef>(null);
const sampleData = useRef<SampleData | null>(null);
const [pivotSource] = useState<unknown>(() => createPivotDataSource(jqx, sampleData));
const tableSrcRecordsColumns: IDataTableProps['columns'] = [
{ text: 'First name', dataField: 'firstname', width: 200 },
{ text: 'Last name', dataField: 'lastname', width: 200 },
{ text: 'Product', editable: false, dataField: 'productname', width: 180 },
{ text: 'Quantity', dataField: 'quantity', width: 80, cellsAlign: 'right', align: 'right' },
{ text: 'Unit Price', dataField: 'price', width: 90, cellsAlign: 'right', align: 'right', cellsFormat: 'c2' },
{ text: 'Total', dataField: 'total', cellsAlign: 'right', align: 'right', cellsFormat: 'c2' }
];
useEffect(() => {
if (
myPivotGrid.current &&
typeof myPivotGrid.current.getPivotRows === 'function' &&
typeof myPivotGrid.current.getPivotColumns === 'function'
) {
const pivotRows = myPivotGrid.current.getPivotRows().items;
const pivotColumns = myPivotGrid.current.getPivotColumns().items;
if (pivotRows && pivotRows.length > 0 && pivotColumns && pivotColumns.length > 0 && pivotColumns[0].valueItems && pivotColumns[0].valueItems.length > 0) {
drillThrough(pivotRows[0], pivotColumns[0].valueItems[0]);
}
}
}, []);
const onPivotCellDblClick = React.useCallback((event: {
args: { pivotRow: IPivotGridItem; pivotColumn: IPivotGridItem }
}) => {
drillThrough(event.args.pivotRow, event.args.pivotColumn);
}, []);
function drillThrough(pivotRow: IPivotGridItem, pivotColumn: IPivotGridItem): void {
if (
!myPivotGrid.current ||
typeof myPivotGrid.current.getInstance !== 'function' ||
!sampleData.current ||
!tableSrcRecords.current
) {
return;
}
const pivotGridInstance = myPivotGrid.current.getInstance();
if (
!pivotGridInstance ||
typeof pivotGridInstance.getPivotCells !== 'function' ||
typeof pivotGridInstance.getPivotCells().drillThroughCell !== 'function'
) {
return;
}
const rows: number[] = pivotGridInstance.getPivotCells().drillThroughCell(pivotRow, pivotColumn);
const drillThroughRows: SampleData = [];
for (const rowIdx of rows) {
drillThroughRows.push(sampleData.current[rowIdx]);
}
const drillThroughSrc = {
dataFields: [
{ name: 'firstname', type: 'string' },
{ name: 'lastname', type: 'string' },
{ name: 'productname', type: 'string' },
{ name: 'quantity', type: 'number' },
{ name: 'price', type: 'number' },
{ name: 'total', type: 'number' }
],
dataType: 'array',
localData: drillThroughRows
};
const drillThroughDataAdapter = new jqx.dataAdapter(drillThroughSrc);
tableSrcRecords.current.setOptions({ source: drillThroughDataAdapter });
if (drillThroughWindow.current && typeof drillThroughWindow.current.open === 'function') {
drillThroughWindow.current.open();
}
}
return (
<div>
<JqxPivotGrid
theme="tailwind-blue-dark"
ref={myPivotGrid}
style={{ width: 800, height: 400 }}
onPivotcelldblclick={onPivotCellDblClick}
source={pivotSource}
treeStyleRows={true}
autoResize={false}
multipleSelectionEnabled={true}
/>
<JqxWindow
theme="tailwind-blue-dark"
ref={drillThroughWindow}
width={500}
height={300}
autoOpen={false}
showCollapseButton={true}
maxHeight={400}
minHeight={200}
maxWidth={700}
minWidth={200}
>
<div>Drillthrough records</div>
<div style={{ overflow: 'hidden' }}>
<JqxDataTable
theme="tailwind-blue-dark"
ref={tableSrcRecords}
width="100%"
height="100%"
pageable={true}
pagerButtonsCount={10}
columns={tableSrcRecordsColumns}
columnsResize={true}
/>
</div>
</JqxWindow>
</div>
);
}
export default App;