DEV Community

Lorna Watson
Lorna Watson

Posted on

7 3

Rendering multiple icons in AG Grid cell

End result 🥳

Alt Text

Setup

I've tried to strip out any irrelevant bits. Also to note I am using the free Font Awesome icons and community edition of AG Grid.

Deployments List Component

TS

Include the new cell renderer component when setting up your col defs.

{
  field: 'DeployedBy', 
  cellRendererFramework: IconCellRendererComponent 
}
Enter fullscreen mode Exit fullscreen mode

Icon Cell Renderer Component

TS

import { Component } from "@angular/core";
import { ICellRendererAngularComp } from 'ag-grid-angular';
import { IconDefinition } from '@fortawesome/fontawesome-svg-core';
import { faUsers, faCity } from '@fortawesome/free-solid-svg-icons';

@Component({
  selector: 'app-icon-cell-renderer',
  templateUrl: './icon-cell-renderer.component.html'
})
export class IconCellRendererComponent implements ICellRendererAngularComp {
  faUsers = faUsers;
  faCity = faCity;

  params: any;
  icons: IconDefinition[]; // ✨ if wanting just the 1 icon change to `IconDefinition`

  agInit(params: any): void {
    this.params = params;
    if (this.params.value === 'Teamcity') {
      this.icons = [faUsers, faCity];
    }

    return this.params.value;
  }

  refresh(): boolean {
    return false;
  }
}
Enter fullscreen mode Exit fullscreen mode

HTML

I'm looking at tidying this up.

<span *ngIf="icons != null; else notSys">
  <span *ngFor="let icon of icons">
    <fa-icon [icon]="icon"></fa-icon>
  </span>
</span>
<ng-template #notSys>
  {{params.value}}
</ng-template>
Enter fullscreen mode Exit fullscreen mode

Styling

@almost-black: #262626;

ag-grid-angular.ag-theme-material {
  .ag-row .ag-cell fa-icon { 
    margin-left: 2px;
    margin-right: 2px;
    color: @almost-black; 
  }
}
Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay