Description This is an example of the drill through functionality of the Angular 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 Angular and how to enable or disable the selection functionality of the pivot grid component.
A live Angular example of jqxPivotGrid, the jQWidgets PivotGrid component, with its complete source code below. It is one of 12 jqxPivotGrid demos for Angular.
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.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-ng, add these files to a standalone Angular CLI app, and add jqx.base.css and a theme to the styles in angular.json. Getting started with jqxPivotGrid shows the setup step by step.
import { Component, AfterContentInit, AfterViewInit, viewChild } from '@angular/core';
import { jqxPivotGridComponent } from 'jqwidgets-ng/jqxpivotgrid';
import { jqxWindowComponent } from 'jqwidgets-ng/jqxwindow';
import { jqxDataTableComponent } from 'jqwidgets-ng/jqxdatatable';
import { CommonModule } from '@angular/common';
import { jqxWindowModule } from 'jqwidgets-ng/jqxwindow';
import { jqxDataTableModule } from 'jqwidgets-ng/jqxdatatable';
import { jqxPivotGridModule } from 'jqwidgets-ng/jqxpivotgrid';
@Component({
imports: [CommonModule, jqxWindowModule, jqxDataTableModule, jqxPivotGridModule],
selector: 'app-root',
templateUrl: './app.html',
styleUrl: './app.css'
})
export class App {
readonly pivotGrid1 = viewChild.required<jqxPivotGridComponent>('pivotGrid1');
readonly drillThroughWindow = viewChild.required<jqxWindowComponent>('drillThroughWindow');
readonly tableSrcRecords = viewChild.required<jqxDataTableComponent>('tableSrcRecords');
constructor()
{
this.pivotDataSource = this.createPivotDataSource();
}
getWidth() : any {
if (document.body.offsetWidth < 850) {
return '90%';
}
return 850;
}
ngAfterViewInit(): void {
let pivotRow = this.pivotGrid1().getPivotRows().items[0];
let pivotColumn = this.pivotGrid1().getPivotColumns().items[0].valueItems[0];
this.drillThrough(pivotRow, pivotColumn);
}
pivotDataSource: null;
sampleData: any = null;
tableSrcRecordsColumns: any[] = [
{ 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' }
];
createPivotDataSource(): any {
// prepare sample data
let data = new Array();
let firstNames =
[
"Andrew", "Nancy", "Shelley", "Regina", "Yoshi", "Antoni", "Mayumi", "Ian", "Peter", "Lars", "Petra", "Martin", "Sven", "Elio", "Beate", "Cheryl", "Michael", "Guylene"
];
let lastNames =
[
"Fuller", "Davolio", "Burke", "Murphy", "Nagase", "Saavedra", "Ohno", "Devling", "Wilson", "Peterson", "Winkler", "Bein", "Petersen", "Rossi", "Vileid", "Saylor", "Bjorn", "Nodier"
];
let productNames =
[
"Black Tea", "Green Tea", "Caffe Espresso"
];
let 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++) {
let row: any = {};
let productindex = Math.floor(Math.random() * productNames.length);
let price = parseFloat(priceValues[productindex]);
let 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[i] = row;
}
this.sampleData = data;
// create a data source and data adapter
let source =
{
localdata: data,
datatype: "array",
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' }
]
};
let dataAdapter = new jqx.dataAdapter(source);
dataAdapter.dataBind();
// create a pivot data source from the dataAdapter
let pivotDataSource = new jqx.pivot(
dataAdapter,
{
pivotValuesOnRows: false,
totals: {rows: {subtotals: true, grandtotals: true}, columns: {subtotals: false, grandtotals: true}},
rows: [{ dataField: 'firstname' }, { dataField: 'lastname'}],
columns: [{ dataField: 'productname' }],
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;
}
drillThrough(pivotRow: any, pivotColumn: any): void
{
let pivotGridInstance = this.pivotGrid1().getInstance();
let rows = pivotGridInstance.getPivotCells().drillThroughCell(pivotRow, pivotColumn);
let drillThroughRows = [];
for (let i = 0; i < rows.length; i++)
drillThroughRows[i] = this.sampleData![rows[i]]
let drillThroughSrc =
{
localData: drillThroughRows,
dataType: "array",
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' }
]
};
let drillThroughDataAdapter = new jqx.dataAdapter(drillThroughSrc);
this.tableSrcRecords().source(drillThroughDataAdapter);
this.drillThroughWindow().open();
}
onPivotCellDblClick(event: any): void {
const args = event.args;
this.drillThrough(args.pivotRow, args.pivotColumn);
}
}
<jqxPivotGrid #pivotGrid1
[width]="getWidth()"
[height]="400"
[source]="pivotDataSource"
[treeStyleRows] = "true"
[autoResize] = "false"
[multipleSelectionEnabled] = "true"
(onPivotcelldblclick)="onPivotCellDblClick($event)"
>
</jqxPivotGrid>
<jqxWindow
#drillThroughWindow
[autoOpen]="false"
[width]="370"
[height]="300"
[maxHeight]="400"
[minHeight]="200"
[maxWidth]="700"
[minWidth]="200"
[showCollapseButton]="true">
<div #windowHeader>
Drillthrough records
</div>
<div style="overflow: hidden;" #windowContent>
<jqxDataTable
#tableSrcRecords
[width]="'100%'"
[height]="'100%'"
[pageable]="true"
[pagerButtonsCount]="10"
[columns]="tableSrcRecordsColumns"
[columnsResize]="true"
>
</jqxDataTable>
</div>
</jqxWindow>