AG Grid Display Formatter
May 15, 2024 · 3 min readLucian CiobanuAG Grid is a very powerful paid component library with numerous personalization options and settings. The fact that it is paid, makes it less likely to find information about it on forums such as stack overflow and the documentation is sometimes lacking as they have really tried to cover most of js existing libraries/frameworks.
I've recently had the challenge to use "multiple values" display - something like chips - and because this needed the flexibility to be used throughout the app, I included it in a component. Extra magic was to have various types of data coming - sometimes strings, sometimes objects - and also preventing empty strings to display an empty chip and this is how I did it:
1. including a component in the grid for display:
ag-grid.component.ts
...
columnDefs: ColDef<Type>[] = [
{
field: 'propertyWithData',
headerName: '$localize`Property`,
cellRenderer: MultipleValuesDisplayComponent,
valueGetter: (params) => params.data.map(({ toDisplay }: Type) => toDisplay),
},
...
]
What we see above is that we are passing on propertyWithData an object with property toDisplay and we are asigning the data to toDisplay which will be passed through AG Grid magic inside the MultipleValuesDisplay component.
The actual display and final check wether the toDisplay is an empty string or not, can be done as such:
multiple-values-display.omponent.html
@if (values.length) {
<div>
<span>{{ values[0] }}</span>
@if (values.length > 1) {
<span>+{{ values.length - 1 }}</span>
}
</div>
}
multiple-values-display.component.ts
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ICellRendererAngularComp } from '@ag-grid-community/angular';
@Component({
selector: 'app-mvd',
standalone: true,
imports: [CommonModule],
templateUrl: '...',
stylesUrl: '...',
})
export class mvdComponent implements ICellRendererAngularComp {
public params?: any;
values = [];
agInit(params: any): void {
this.params = params;
this.values = this.params?.value.filter(Boolean);
}
refresh(): boolean {
return false;
}
}
I'm really proud of this little bit of code. Basically, AG Grid magic makes the already formated values via valueGetter available within the component on params property which are used on agInit to be filtered if falsy and from there it's just basic Angular 18.
Hope this helped!
Cheers,
Lucian

Web Development & Software Delivery