Introduction
In this article, we are going to see ag-Grid for Angular applications. ag-Grid provides a feature to display the data in proper grid format with filtering and sorting features already included in it and many more. Ag-grid is a fully-featured and highly customizable javascript data grid.
Prerequisites
- Basic knowledge of Angular
- Visual Studio Code must be installed
- Angular CLI must be installed
- Node JS must be installed
Step 1
- ng new ag-gridExample
Step 2
Now let's add the ag-Grid NPM packages. run the following command
- npm install --save ag-grid-community ag-grid-angular
Step 3
The next step is to add ag-Grid styles in style.css, just import the following below command in style.css
- @import "~ag-grid-community/dist/styles/ag-grid.css";
- @import "~ag-grid-community/dist/styles/ag-theme-balham.css";
Step 4
Create a new ag-Grid Component using the following NPM command,
- ng g c ag-grid
After installing the package, we just need to import it in our module, so open app.module.ts file and import the following code:
- import { AgGridModule } from 'ag-grid-angular';
Step 5
Open the file app.module.ts and paste the below code,
- import { BrowserModule } from '@angular/platform-browser';
- import { NgModule } from '@angular/core';
- import { AppComponent } from './app.component';
- import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
- import { AgGridComponent } from './ag-grid/ag-grid.component';
- import { AgGridModule } from 'ag-grid-angular';
- @NgModule({
- declarations: [
- AppComponent,
- AgGridComponent
- ],
- imports: [
- BrowserModule,
- BrowserAnimationsModule,
- AgGridModule.withComponents([]),
- ],
- providers: [],
- bootstrap: [AppComponent]
- })
- export class AppModule { }

Step 6
Now, open the app.component.ts file and add the following code in this file,
- import { Component, OnInit } from '@angular/core';
- @Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css']
- })
- export class AppComponent implements OnInit {
- ColumnDefs;
- RowData: any;
- AgLoad: boolean;
- constructor() { }
- ngOnInit() {
- this.GetAgColumns();
- this.GetGiftVoucherList();
- }
- GetAgColumns() {
- this.ColumnDefs = [
- { headerName: 'ArtNo', field: 'ArtNo', sortable: true, filter: true },
- { headerName: 'Provider', field: 'Provider', sortable: true, filter: true },
- { headerName: 'ProviderArtNo', field: 'ProviderArtNo', sortable: true, filter: true },
- { headerName: 'Brand', field: 'Brand', sortable: true, filter: true },
- { headerName: 'Price', field: 'Price', sortable: true, filter: true },
- { headerName: 'BuyAccount', field: 'BuyAccount', sortable: true, filter: true }
- ];
- }
- GetGiftVoucherList() {
- this.AgLoad = true;
- this.RowData = [
- {
- ArtNo: "100",
- Provider: "IPhone 11",
- ProviderArtNo: "1Yu",
- Brand: "Apple",
- Price: 7810.23,
- BuyAccount: "123",
- },
- {
- ArtNo: "101",
- Provider: "Samsung galaxy",
- ProviderArtNo: "1Yu",
- Brand: "Samsung",
- Price: 2310.23,
- BuyAccount: "123",
- },
- {
- ArtNo: "102",
- Provider: "Iphone 11 Pro",
- ProviderArtNo: "1Yu",
- Brand: "Apple",
- Price: 7810.23,
- BuyAccount: "123",
- },
- {
- ArtNo: "103",
- Provider: "Intex",
- ProviderArtNo: "1Yu",
- Brand: "Intex",
- Price: 5810.23,
- BuyAccount: "123",
- },
- {
- ArtNo: "100",
- Provider: "IPhone 11",
- ProviderArtNo: "1Yu",
- Brand: "Apple",
- Price: 7810.23,
- BuyAccount: "123",
- },
- {
- ArtNo: "101",
- Provider: "Samsung galaxy",
- ProviderArtNo: "1Yu",
- Brand: "Samsung",
- Price: 2310.23,
- BuyAccount: "123",
- },
- {
- ArtNo: "102",
- Provider: "Iphone 11 Pro",
- ProviderArtNo: "1Yu",
- Brand: "Apple",
- Price: 7810.23,
- BuyAccount: "123",
- },
- {
- ArtNo: "103",
- Provider: "Intex",
- ProviderArtNo: "1Yu",
- Brand: "Intex",
- Price: 5810.23,
- BuyAccount: "123",
- }
- ];
- }
- }











MatheusPosted Jul 27, 2023, 10:43 PM
The jspreadsheet library is also very good for working with grids. I liked it because it is very light.
Pramod ThakurPosted Mar 11, 2022, 2:09 AM
How to call on button grid. on page load, it's working fine. I just want to display on button click instead of page load.
vipin kumarPosted Jan 25, 2022, 6:07 PM
Thanks for explaining. How we can place button inside cell for a column