When a Master Row is expanded, the grid uses the default Detail Cell Renderer to create and display the Detail Grid inside one row of the Master Grid. You can provide a custom Detail Cell Renderer to display something else if the default Detail Cell Renderer doesn't do what you want.
Configure the grid to use a custom Detail Cell Renderer using the grid property detailCellRenderer.
<ag-grid-angular
[detailCellRenderer]="detailCellRenderer"
[detailCellRendererParams]="detailCellRendererParams"
/* other grid options ... */ />
// normally left blank, the grid will use the default Detail Cell Renderer
this.detailCellRenderer = 'myCellRendererComp';
// params sent to the Detail Cell Renderer, in this case your MyCellRendererComp
this.detailCellRendererParams = {};The Detail Cell Renderer should be a Cell Renderer component. See Cell Renderer on how to build and register a Cell Renderer with the grid.
The following examples demonstrate minimalist custom Detail Cell Renderer. Note that where a Detail Grid would normally appear, only the message "My Custom Detail" is shown.
import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { AgGridAngular } from "ag-grid-angular";
import {
ClientSideRowModelModule,
ColDef,
ColGroupDef,
FirstDataRenderedEvent,
GridApi,
GridOptions,
GridReadyEvent,
ModuleRegistry,
RowApiModule,
enableDevValidations,
} from "ag-grid-community";
import {
ColumnMenuModule,
ColumnsToolPanelModule,
ContextMenuModule,
MasterDetailModule,
} from "ag-grid-enterprise";
if (process.env.NODE_ENV !== "production") {
// Enable extended validations only for development
enableDevValidations();
}
ModuleRegistry.registerModules([
RowApiModule,
ClientSideRowModelModule,
ColumnsToolPanelModule,
MasterDetailModule,
ColumnMenuModule,
ContextMenuModule,
]);
import { DetailCellRenderer } from "./detail-cell-renderer.component";
import { IAccount } from "./interfaces";
@Component({
selector: "my-app",
standalone: true,
imports: [AgGridAngular, DetailCellRenderer],
template: `<ag-grid-angular
style="width: 100%; height: 100%;"
[masterDetail]="true"
[detailCellRenderer]="detailCellRenderer"
[columnDefs]="columnDefs"
[defaultColDef]="defaultColDef"
[rowData]="rowData"
(firstDataRendered)="onFirstDataRendered($event)"
(gridReady)="onGridReady($event)"
/> `,
})
export class AppComponent {
detailCellRenderer: any = DetailCellRenderer;
columnDefs: ColDef[] = [
// group cell renderer needed for expand / collapse icons
{ field: "name", cellRenderer: "agGroupCellRenderer" },
{ field: "account" },
{ field: "calls" },
{ field: "minutes", valueFormatter: "x.toLocaleString() + 'm'" },
];
defaultColDef: ColDef = {
flex: 1,
};
rowData!: IAccount[];
constructor(private http: HttpClient) {}
onFirstDataRendered(params: FirstDataRenderedEvent) {
params.api.forEachNode(function (node) {
node.setExpanded(node.id === "1");
});
}
onGridReady(params: GridReadyEvent<IAccount>) {
this.http
.get<
IAccount[]
>("https://www.ag-grid.com/example-assets/master-detail-data.json")
.subscribe((data) => {
this.rowData = data;
});
}
}
import { ChangeDetectionStrategy, Component } from '@angular/core';
import type { ICellRendererAngularComp } from 'ag-grid-angular';
@Component({
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<div role="gridcell"><h1 style="padding: 20px;">My Custom Detail</h1></div>`,
})
export class DetailCellRenderer implements ICellRendererAngularComp {
agInit(params: any): void {}
refresh(params: any): boolean {
return false;
}
}
import '@angular/compiler';
import { provideHttpClient } from '@angular/common/http';
import { enableProdMode } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
if (new URLSearchParams(window.location.search).get('prod') !== 'false') {
enableProdMode();
}
const app = bootstrapApplication(AppComponent, {
providers: [provideHttpClient()],
});
export interface ICallRecord {
name: string;
callId: number;
duration: number;
switchCode: string;
direction: string;
number: string;
}
export interface IAccount {
name: string;
account: number;
calls: number;
minutes: number;
callRecords: ICallRecord[];
} Custom Detail With Form Copy Link
It is not mandatory to display a grid inside the detail section. As you are providing a custom component, there are no restrictions as to what can appear inside the custom component.
This example shows a custom Detail Cell Renderer that uses a form rather than a grid.
import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { AgGridAngular } from "ag-grid-angular";
import "./styles.css";
import {
ClientSideRowModelModule,
ColDef,
ColGroupDef,
FirstDataRenderedEvent,
GridApi,
GridOptions,
GridReadyEvent,
ModuleRegistry,
RowApiModule,
enableDevValidations,
} from "ag-grid-community";
import {
ColumnMenuModule,
ColumnsToolPanelModule,
ContextMenuModule,
MasterDetailModule,
} from "ag-grid-enterprise";
if (process.env.NODE_ENV !== "production") {
// Enable extended validations only for development
enableDevValidations();
}
ModuleRegistry.registerModules([
RowApiModule,
ClientSideRowModelModule,
ColumnsToolPanelModule,
MasterDetailModule,
ColumnMenuModule,
ContextMenuModule,
]);
import { DetailCellRenderer } from "./detail-cell-renderer.component";
import { IAccount } from "./interfaces";
@Component({
selector: "my-app",
standalone: true,
imports: [AgGridAngular, DetailCellRenderer],
template: `<ag-grid-angular
style="width: 100%; height: 100%;"
[columnDefs]="columnDefs"
[defaultColDef]="defaultColDef"
[masterDetail]="true"
[detailCellRenderer]="detailCellRenderer"
[detailRowHeight]="detailRowHeight"
[groupDefaultExpanded]="groupDefaultExpanded"
[rowData]="rowData"
(firstDataRendered)="onFirstDataRendered($event)"
(gridReady)="onGridReady($event)"
/> `,
})
export class AppComponent {
columnDefs: ColDef[] = [
// group cell renderer needed for expand / collapse icons
{ field: "name", cellRenderer: "agGroupCellRenderer" },
{ field: "account" },
{ field: "calls" },
{ field: "minutes", valueFormatter: "x.toLocaleString() + 'm'" },
];
defaultColDef: ColDef = {
flex: 1,
};
detailCellRenderer: any = DetailCellRenderer;
detailRowHeight = 80;
groupDefaultExpanded = 1;
rowData!: IAccount[];
constructor(private http: HttpClient) {}
onFirstDataRendered(params: FirstDataRenderedEvent) {
params.api.forEachNode(function (node) {
node.setExpanded(node.id === "1");
});
}
onGridReady(params: GridReadyEvent<IAccount>) {
this.http
.get<
IAccount[]
>("https://www.ag-grid.com/example-assets/master-detail-data.json")
.subscribe((data) => {
this.rowData = data;
});
}
}
p {
font-size: 1em;
margin: 0;
}
.cell-renderer-outer {
height: 80px;
}
.cell-renderer-outer form {
height: 100%;
}
.container {
max-width: 960px;
height: 100%;
margin: 10% auto;
padding: 2.5em;
}
form > div {
height: 100%;
display: flex;
background-color: #99999944;
}
form > div > div {
min-width: 33.3%;
}
label {
display: block;
margin: 0.75em 25%;
font-weight: bold;
}
import { Component, signal } from '@angular/core';
import type { ICellRendererAngularComp } from 'ag-grid-angular';
@Component({
standalone: true,
template: `
<div role="gridcell" class="cell-renderer-outer">
<form>
<div>
<div>
<label>
Call Id:<br />
<input type="text" value="{{ firstRecord()?.callId }}" />
</label>
</div>
<div>
<label>
Number:<br />
<input type="text" value="{{ firstRecord()?.number }}" />
</label>
</div>
<div>
<label>
Direction:<br />
<input type="text" value="{{ firstRecord()?.direction }}" />
</label>
</div>
</div>
</form>
</div>
`,
})
export class DetailCellRenderer implements ICellRendererAngularComp {
firstRecord = signal<any>(undefined);
// called on init
agInit(params: any): void {
this.firstRecord.set(params.data.callRecords[0]);
}
// called when the cell is refreshed
refresh(params: any): boolean {
return false;
}
}
import '@angular/compiler';
import { provideHttpClient } from '@angular/common/http';
import { enableProdMode } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
if (new URLSearchParams(window.location.search).get('prod') !== 'false') {
enableProdMode();
}
const app = bootstrapApplication(AppComponent, {
providers: [provideHttpClient()],
});
export interface ICallRecord {
name: string;
callId: number;
duration: number;
switchCode: string;
direction: string;
number: string;
}
export interface IAccount {
name: string;
account: number;
calls: number;
minutes: number;
callRecords: ICallRecord[];
} Custom Detail With Grid Copy Link
It is possible to provide a Custom Detail Grid that does a similar job to the default Detail Cell Renderer. This example demonstrates displaying a custom grid as the detail. Details are logged to the developer console.
import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { AgGridAngular } from "ag-grid-angular";
import "./styles.css";
import {
ClientSideRowModelApiModule,
ClientSideRowModelModule,
ColDef,
ColGroupDef,
FirstDataRenderedEvent,
GridApi,
GridOptions,
GridReadyEvent,
ModuleRegistry,
RowApiModule,
enableDevValidations,
} from "ag-grid-community";
import {
ColumnMenuModule,
ColumnsToolPanelModule,
ContextMenuModule,
MasterDetailModule,
} from "ag-grid-enterprise";
if (process.env.NODE_ENV !== "production") {
// Enable extended validations only for development
enableDevValidations();
}
ModuleRegistry.registerModules([
ClientSideRowModelApiModule,
RowApiModule,
ClientSideRowModelModule,
ColumnsToolPanelModule,
MasterDetailModule,
ColumnMenuModule,
ContextMenuModule,
]);
import { DetailCellRenderer } from "./detail-cell-renderer.component";
import { IAccount } from "./interfaces";
declare let window: any;
@Component({
selector: "my-app",
standalone: true,
imports: [AgGridAngular, DetailCellRenderer],
template: `<div class="example-wrapper">
<div style="margin-bottom: 5px">
<button (click)="printDetailGridInfo()">Print Detail Grid Info</button>
<button (click)="expandCollapseAll()">Toggle Expand / Collapse</button>
</div>
<ag-grid-angular
style="width: 100%; height: 100%;"
[columnDefs]="columnDefs"
[defaultColDef]="defaultColDef"
[masterDetail]="true"
[detailRowHeight]="detailRowHeight"
[detailCellRenderer]="detailCellRenderer"
[rowData]="rowData"
(firstDataRendered)="onFirstDataRendered($event)"
(gridReady)="onGridReady($event)"
/>
</div> `,
})
export class AppComponent {
private gridApi!: GridApi<IAccount>;
columnDefs: ColDef[] = [
// group cell renderer needed for expand / collapse icons
{ field: "name", cellRenderer: "agGroupCellRenderer" },
{ field: "account" },
{ field: "calls" },
{ field: "minutes", valueFormatter: "x.toLocaleString() + 'm'" },
];
defaultColDef: ColDef = {
flex: 1,
};
detailRowHeight = 310;
detailCellRenderer: any = DetailCellRenderer;
rowData!: IAccount[];
constructor(private http: HttpClient) {}
onFirstDataRendered(params: FirstDataRenderedEvent) {
// arbitrarily expand a row for presentational purposes
setTimeout(() => {
params.api.getDisplayedRowAtIndex(1)!.setExpanded(true);
}, 0);
}
expandCollapseAll() {
this.gridApi.forEachNode(function (node) {
node.expanded = !!window.collapsed;
});
window.collapsed = !window.collapsed;
this.gridApi.onGroupExpandedOrCollapsed();
}
printDetailGridInfo() {
console.log("Currently registered detail grid's: ");
this.gridApi.forEachDetailGridInfo(function (detailGridInfo) {
console.log(detailGridInfo);
});
}
onGridReady(params: GridReadyEvent<IAccount>) {
this.gridApi = params.api;
this.http
.get<
IAccount[]
>("https://www.ag-grid.com/example-assets/master-detail-data.json")
.subscribe((data) => {
this.rowData = data;
});
}
}
.example-wrapper {
display: flex;
flex-direction: column;
height: 100%;
}
#myGrid {
flex: 1 1 0px;
width: 100%;
}
.full-width-panel {
position: relative;
height: 100%;
width: 100%;
padding: 5px;
}
.call-record-cell {
text-align: right;
}
.full-width-detail {
padding-top: 4px;
}
.full-width-details {
float: left;
padding: 5px;
margin: 5px;
width: 150px;
}
.full-width-grid {
margin-left: 125px;
padding: 25px;
display: block;
height: calc(100% - 50px);
}
.full-width-grid-toolbar {
top: 4px;
left: 30px;
margin-left: 150px;
display: block;
position: absolute;
}
.full-width-phone-icon {
padding-right: 10px;
}
.full-width-search {
margin-left: 10px;
}
import { Component } from '@angular/core';
import type { ICellRendererAngularComp } from 'ag-grid-angular';
import { AgGridAngular } from 'ag-grid-angular';
import type { ColDef, GridApi, GridReadyEvent, ICellRendererParams } from 'ag-grid-community';
@Component({
standalone: true,
imports: [AgGridAngular],
template: ` <div role="gridcell" class="full-width-panel">
<div class="full-width-details">
<div class="full-width-detail"><b>Name: </b>{{ params.data.name }}</div>
<div class="full-width-detail"><b>Account: </b>{{ params.data.account }}</div>
</div>
<div class="full-width-grid">
<ag-grid-angular
#agGrid
style="height: 100%;"
[columnDefs]="colDefs"
[defaultColDef]="defaultColDef"
[rowData]="rowData"
(gridReady)="onGridReady($event)"
/>
</div>
</div>`,
})
export class DetailCellRenderer implements ICellRendererAngularComp {
params!: ICellRendererParams;
masterGridApi!: GridApi;
rowId!: string;
colDefs!: ColDef[];
defaultColDef!: ColDef;
rowData!: any[];
// called on init
agInit(params: ICellRendererParams): void {
this.params = params;
this.masterGridApi = params.api;
this.rowId = params.node.id!;
this.colDefs = [
{ field: 'callId' },
{ field: 'direction' },
{ field: 'number' },
{ field: 'duration', valueFormatter: "x.toLocaleString() + 's'" },
{ field: 'switchCode' },
];
this.defaultColDef = {
flex: 1,
minWidth: 120,
};
this.rowData = params.data.callRecords;
}
// called when the cell is refreshed
refresh(params: ICellRendererParams): boolean {
return false;
}
onGridReady(params: GridReadyEvent) {
const gridInfo = {
id: this.rowId,
api: params.api,
};
console.log('adding detail grid info with id: ', this.rowId);
this.masterGridApi.addDetailGridInfo(this.rowId, gridInfo);
}
ngOnDestroy(): void {
// detail grid is automatically destroyed as it is an Angular component
console.log('removing detail grid info with id: ', this.rowId);
this.masterGridApi.removeDetailGridInfo(this.rowId);
}
}
import '@angular/compiler';
import { provideHttpClient } from '@angular/common/http';
import { enableProdMode } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
if (new URLSearchParams(window.location.search).get('prod') !== 'false') {
enableProdMode();
}
const app = bootstrapApplication(AppComponent, {
providers: [provideHttpClient()],
});
export interface ICallRecord {
name: string;
callId: number;
duration: number;
switchCode: string;
direction: string;
number: string;
}
export interface IAccount {
name: string;
account: number;
calls: number;
minutes: number;
callRecords: ICallRecord[];
} Register Detail Grid Copy Link
In order for the Detail Grid's API to be available via the Master Grid as explained in Accessing Detail Grids, a Grid Info object needs to be registered with the Master Grid.
Register a detail grid with the master grid when it is created. |
Unregister a detail grid from the master grid when it is destroyed. |
When the Detail Grid is created, register it via masterGridApi.addDetailGridInfo(id, info) and when the Detail Grid is destroyed, unregister it via masterGridApi.removeDetailGridInfo(id). A Detail ID is required when calling these methods. Any unique ID can be used, however for consistency with how the default Detail Cell Renderer works it's recommended to use the ID of the detail Row Node.
//////////////////////////////
// Register with Master Grid
const detailId = params.node.id;
// Create Grid Info object
const detailGridInfo = {
id: detailId,
api: params.api,
};
this.masterGridApi.addDetailGridInfo(detailId, detailGridInfo);
//////////////////////////////
// Unregister with Master Grid
this.masterGridApi.removeDetailGridInfo(detailId); Custom Detail Height Copy Link
When using a custom Detail Cell Renderer the height of the detail section can be customised as explained in Detail Height.
Refreshing Copy Link
When data is updated in the grid using Transaction Updates, the grid will call refresh on all Detail Cell Renderers.
It is up to the Detail Cell Renderer whether it wants to act on the refresh or not. If the refresh() method returns true, the grid will assume the Detail Cell Renderer has refreshed successfully and nothing more will happen. However if false is returned, the grid will destroy the Detail Cell Renderer and re-create it again.
This pattern is similar to how refresh works for normal grid Cell Renderers.
The example below shows how components can refresh on updates. The example refreshes the first row every one second. The refresh() method gets called on the corresponding Detail Cell Renderer after the transaction is applied. The Detail Cell Renderer refresh method reads the latest call count from the params, and the last updated time is also changed.
import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { AgGridAngular } from "ag-grid-angular";
import "./styles.css";
import {
ClientSideRowModelApiModule,
ClientSideRowModelModule,
ColDef,
ColGroupDef,
FirstDataRenderedEvent,
GridApi,
GridOptions,
GridReadyEvent,
HighlightChangesModule,
ModuleRegistry,
enableDevValidations,
} from "ag-grid-community";
import {
ColumnMenuModule,
ColumnsToolPanelModule,
ContextMenuModule,
MasterDetailModule,
} from "ag-grid-enterprise";
if (process.env.NODE_ENV !== "production") {
// Enable extended validations only for development
enableDevValidations();
}
ModuleRegistry.registerModules([
ClientSideRowModelApiModule,
HighlightChangesModule,
ClientSideRowModelModule,
ColumnsToolPanelModule,
MasterDetailModule,
ColumnMenuModule,
ContextMenuModule,
]);
import { DetailCellRenderer } from "./detail-cell-renderer.component";
import { IAccount } from "./interfaces";
@Component({
selector: "my-app",
standalone: true,
imports: [AgGridAngular, DetailCellRenderer],
template: `<ag-grid-angular
style="width: 100%; height: 100%;"
[columnDefs]="columnDefs"
[defaultColDef]="defaultColDef"
[masterDetail]="true"
[detailCellRenderer]="detailCellRenderer"
[detailRowHeight]="detailRowHeight"
[groupDefaultExpanded]="groupDefaultExpanded"
[rowData]="rowData"
(firstDataRendered)="onFirstDataRendered($event)"
(gridReady)="onGridReady($event)"
/> `,
})
export class AppComponent {
columnDefs: ColDef[] = [
// group cell renderer needed for expand / collapse icons
{ field: "name", cellRenderer: "agGroupCellRenderer" },
{ field: "account" },
{ field: "calls" },
{ field: "minutes", valueFormatter: "x.toLocaleString() + 'm'" },
];
defaultColDef: ColDef = {
flex: 1,
enableCellChangeFlash: true,
};
detailCellRenderer: any = DetailCellRenderer;
detailRowHeight = 70;
groupDefaultExpanded = 1;
rowData!: IAccount[];
constructor(private http: HttpClient) {}
onFirstDataRendered(params: FirstDataRenderedEvent) {
setInterval(() => {
if (!allRowData) {
return;
}
const data = allRowData[0];
const newCallRecords: any[] = [];
data.callRecords.forEach((record: any, index: number) => {
newCallRecords.push({
name: record.name,
callId: record.callId,
duration: record.duration + (index % 2),
switchCode: record.switchCode,
direction: record.direction,
number: record.number,
});
});
data.callRecords = newCallRecords;
data.calls++;
const tran = {
update: [data],
};
params.api.applyTransaction(tran);
}, 2000);
}
onGridReady(params: GridReadyEvent<IAccount>) {
this.http
.get<
IAccount[]
>("https://www.ag-grid.com/example-assets/master-detail-data.json")
.subscribe((data) => {
allRowData = data;
params.api!.setGridOption("rowData", allRowData);
});
}
}
let allRowData: any[];
p {
font-size: 1em;
margin-top: 0;
}
.container {
max-width: 960px;
height: 100%;
margin: 10% auto;
padding: 2.5em;
}
[role='gridcell'] {
display: flex;
height: 100%;
width: 100%;
}
form {
flex: 1;
}
form > div {
display: flex;
flex: 1;
height: 100%;
background-color: #99999944;
}
form > div > p {
min-width: 33.33%;
}
label {
display: block;
margin: 0.75em 25%;
font-weight: bold;
}
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import type { ICellRendererAngularComp } from 'ag-grid-angular';
import type { ICellRendererParams } from 'ag-grid-community';
@Component({
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<div role="gridcell">
<form>
<div>
<p>
<label>
Calls:<br />
<input type="text" value="{{ callsCount() }}" />
</label>
</p>
<p>
<label>
Last Updated:
{{ now() }}
</label>
</p>
</div>
</form>
</div>
`,
})
export class DetailCellRenderer implements ICellRendererAngularComp {
callsCount = signal(0);
now = signal('');
// called on init
agInit(params: ICellRendererParams): void {
this.refresh(params);
}
// called when the cell is refreshed
refresh(params: ICellRendererParams): boolean {
this.callsCount.set(params.data.calls);
this.now.set(new Date().toLocaleTimeString());
// tell the grid not to destroy and recreate
return true;
}
}
import '@angular/compiler';
import { provideHttpClient } from '@angular/common/http';
import { enableProdMode } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
if (new URLSearchParams(window.location.search).get('prod') !== 'false') {
enableProdMode();
}
const app = bootstrapApplication(AppComponent, {
providers: [provideHttpClient()],
});
export interface ICallRecord {
name: string;
callId: number;
duration: number;
switchCode: string;
direction: string;
number: string;
}
export interface IAccount {
name: string;
account: number;
calls: number;
minutes: number;
callRecords: ICallRecord[];
} Keyboard Navigation Copy Link
To add keyboard navigation to custom detail panels, it must be implemented in the custom Detail Cell Renderer. There are several parts to this:
- Create a listener function for the
focusevent when the custom detail panel receives focus. Within this function, the event objecttargetvalue is the custom detail row element, and event objectrelatedTargetvalue is the previous element that was previously focused on. You will need to find the parent of therelatedTargetwithrole=rowattribute to get the previous row element. With the current row element and the previous row element, checking therow-indexattribute allows you to see if the user is entering the focus from the previous or current row (ie,row-indexincreases or is the same from previous to current) or the next row (ie,row-indexdecreases from previous to current). With this knowledge, you can set focus usingelement.focus()on the relevant element in your custom detail panel - Attach the above function to a
focuslistener on theeParentOfValueparam value in the component initialisation - Remove the above function from the
focuslistener in the component destroy or unmount method
The following example shows an implementation of keyboard navigation in a custom detail panel:
- Click a cell in the
Mila Smithmaster row and press ⇥ Tab key to move focus to the custom detail panel inputs of theMila Smithmaster row. - Click a cell in the
Evelyn Taylormaster row and press ⇧ Shift+⇥ Tab to focus the inputs in the custom detail panel of theMila Smithmaster row.
This example is illustrative of the main concepts, but the actual implementation of custom keyboard navigation will vary based on the specific custom detail panel.