How To Use Ag-Grid In Angular 8 With Sorting And Floating Filter

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
 
Create a new Angular project using the following NPM command:
  1. ng new ag-gridExample
Step 2
 
Now let's add the ag-Grid NPM packages. run the following command  
  1. 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 
  1. @import "~ag-grid-community/dist/styles/ag-grid.css";  
  2. @import "~ag-grid-community/dist/styles/ag-theme-balham.css";  
Step 4
 
Create a new ag-Grid Component using the following NPM command, 
  1. 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:
  1. import { AgGridModule } from 'ag-grid-angular';  
Step 5
 
Open the file app.module.ts and paste the below code,
  1. import { BrowserModule } from '@angular/platform-browser';  
  2. import { NgModule } from '@angular/core';  
  3. import { AppComponent } from './app.component';  
  4. import { BrowserAnimationsModule } from '@angular/platform-browser/animations';  
  5. import { AgGridComponent } from './ag-grid/ag-grid.component';  
  6. import { AgGridModule } from 'ag-grid-angular';  
  7.   
  8. @NgModule({  
  9.   declarations: [  
  10.     AppComponent,  
  11.     AgGridComponent  
  12.   ],  
  13.   imports: [  
  14.     BrowserModule,  
  15.     BrowserAnimationsModule,  
  16.     AgGridModule.withComponents([]),  
  17.     
  18.   ],  
  19.   providers: [],  
  20.   bootstrap: [AppComponent]  
  21. })  
  22. export class AppModule { }  
How To Use Ag-Grid In Angular 8 With Sorting And Floating Filter
 
Step 6
 
Now, open the app.component.ts file and add the following code in this file,
  1. import { Component, OnInit } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-root',  
  5.   templateUrl: './app.component.html',  
  6.   styleUrls: ['./app.component.css']  
  7. })  
  8. export class AppComponent implements OnInit {  
  9.   ColumnDefs;  
  10.   RowData: any;  
  11.   AgLoad: boolean;  
  12.   constructor() { }  
  13.   
  14.   ngOnInit() {  
  15.     this.GetAgColumns();  
  16.     this.GetGiftVoucherList();  
  17.   }  
  18.   GetAgColumns() {  
  19.     this.ColumnDefs = [  
  20.       { headerName: 'ArtNo', field: 'ArtNo', sortable: true, filter: true },  
  21.       { headerName: 'Provider', field: 'Provider', sortable: true, filter: true },  
  22.       { headerName: 'ProviderArtNo', field: 'ProviderArtNo', sortable: true, filter: true },  
  23.       { headerName: 'Brand', field: 'Brand', sortable: true, filter: true },  
  24.       { headerName: 'Price', field: 'Price', sortable: true, filter: true },  
  25.       { headerName: 'BuyAccount', field: 'BuyAccount', sortable: true, filter: true }  
  26.     ];  
  27.   }  
  28.   GetGiftVoucherList() {  
  29.     this.AgLoad = true;  
  30.     this.RowData = [  
  31.       {  
  32.         ArtNo: "100",  
  33.         Provider: "IPhone 11",  
  34.         ProviderArtNo: "1Yu",  
  35.         Brand: "Apple",  
  36.         Price: 7810.23,  
  37.         BuyAccount: "123",  
  38.       },  
  39.       {  
  40.         ArtNo: "101",  
  41.         Provider: "Samsung galaxy",  
  42.         ProviderArtNo: "1Yu",  
  43.         Brand: "Samsung",  
  44.         Price: 2310.23,  
  45.         BuyAccount: "123",  
  46.       },  
  47.       {  
  48.         ArtNo: "102",  
  49.         Provider: "Iphone 11 Pro",  
  50.         ProviderArtNo: "1Yu",  
  51.         Brand: "Apple",  
  52.         Price: 7810.23,  
  53.         BuyAccount: "123",  
  54.       },  
  55.       {  
  56.         ArtNo: "103",  
  57.         Provider: "Intex",  
  58.         ProviderArtNo: "1Yu",  
  59.         Brand: "Intex",  
  60.         Price: 5810.23,  
  61.         BuyAccount: "123",  
  62.       },  
  63.       {  
  64.         ArtNo: "100",  
  65.         Provider: "IPhone 11",  
  66.         ProviderArtNo: "1Yu",  
  67.         Brand: "Apple",  
  68.         Price: 7810.23,  
  69.         BuyAccount: "123",  
  70.       },  
  71.       {  
  72.         ArtNo: "101",  
  73.         Provider: "Samsung galaxy",  
  74.         ProviderArtNo: "1Yu",  
  75.         Brand: "Samsung",  
  76.         Price: 2310.23,  
  77.         BuyAccount: "123",  
  78.       },  
  79.       {  
  80.         ArtNo: "102",  
  81.         Provider: "Iphone 11 Pro",  
  82.         ProviderArtNo: "1Yu",  
  83.         Brand: "Apple",  
  84.         Price: 7810.23,  
  85.         BuyAccount: "123",  
  86.       },  
  87.       {  
  88.         ArtNo: "103",  
  89.         Provider: "Intex",  
  90.         ProviderArtNo: "1Yu",  
  91.         Brand: "Intex",  
  92.         Price: 5810.23,  
  93.         BuyAccount: "123",  
  94.       }  
  95.     ];  
  96.   }  
  97. }  
How To Use Ag-Grid In Angular 8 With Sorting And Floating Filter
 
Here column and row data are its properties where column contains the header, and field is the value which is we are storing inside Row.
 
Row data can be dynamically loaded 
 
On the below Image we are using sortable and filter to enable it in our application/ It will give the feature to sorting as well as filter
 
Step 7
 
The next step is to open the app.component.html file and paste the below code,
  1. <h1>Mobile Company Data</h1>    
  2. <div class="card">    
  3.   <div class="card-body pb-0">    
  4.     <div class="row">    
  5.       <div class="col-md-12">    
  6.         <div class="card">    
  7.           <div class="card-header">    
  8.             Mobile Company Data    
  9.           </div>    
  10.           <div class="card-body position-relative">    
  11.             <div class="ag-theme-balham" style="height:550px;">    
  12.               <app-ag-grid *ngIf="AgLoad" [ColumnDefs]="this.ColumnDefs" [RowData]="this.RowData"    
  13.                 [IsColumnsToFit]="true">    
  14.               </app-ag-grid>    
  15.             </div>    
  16.           </div>    
  17.         </div>    
  18.       </div>    
  19.     </div>    
  20.   </div>    
  21. </div>     
How To Use Ag-Grid In Angular 8 With Sorting And Floating Filter
 
Step 8
 
The next step is to open the ag-grid.component.html file and paste the below code,
  1. <div style="width:100%; height: 100%;">    
  2.   <table id="dynamic-table" class="table table-stripedtable-hover" style="width:100%; height: 100%;">    
  3.     <ag-grid-angular style="width:100%; height:85%;" class="ag-theme-balham" [columnDefs]="ColumnDefs"    
  4.       [enableSorting]="true" [animateRows]="true" [pagination]="true" [paginationPageSize]="10" [floatingFilter]="true"    
  5.       [animateRows]="true" (gridReady)="BindData($event)">    
  6.     </ag-grid-angular>    
  7.   </table>    
  8. </div>     
How To Use Ag-Grid In Angular 8 With Sorting And Floating Filter
Step 9
 
The next step is to open the ag-grid.component.ts file and paste the below code:
  1. import { Component, Input} from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-ag-grid',  
  5.   templateUrl: './ag-grid.component.html',  
  6.   styleUrls: ['./ag-grid.component.css']  
  7. })  
  8. export class AgGridComponent {  
  9.   @Input() ColumnDefs: any;  
  10.   @Input() RowData: any;  
  11.   @Input() IsColumnsToFit: boolean;  
  12.   
  13.   gridApi: any;  
  14.   gridColumnApi: any;  
  15.   
  16.   BindData(params) {  
  17.     this.gridApi = params.api;  
  18.     this.gridColumnApi = params.columnApi;  
  19.     params.api.setRowData(this.RowData);  
  20.     if (this.IsColumnsToFit) {  
  21.       this.gridApi.sizeColumnsToFit();  
  22.     }  
  23.   }  
  24. }  
How To Use Ag-Grid In Angular 8 With Sorting And Floating Filter
 
Now with this step, our coding part is done and it's time for the Output,
 
Type the below code to build and run the application:
  1. ng serve -o
You can see it shows the following image when the connection is there. Once we disconnect, the internet automatically detects it and the page will change.
 
How To Use Ag-Grid In Angular 8 With Sorting And Floating Filter
 
 The above image is showing data in ag-grid with ag-theme-balham
 
How To Use Ag-Grid In Angular 8 With Sorting And Floating Filter
 
The above image is showing ag-grid with a blue theme.There are many themes available in ag-grid, we have to just take its path and import it in style.css.
 
How To Use Ag-Grid In Angular 8 With Sorting And Floating Filter
 
The above image is showing ag-grid with floating filter . 
 
How To Use Ag-Grid In Angular 8 With Sorting And Floating Filter
 
Ag-grid also supports searching and sorting .
 
How To Use Ag-Grid In Angular 8 With Sorting And Floating Filter
 
Ag-gid also gives the feature to paginate our data as in the above image we can see how pagination is working . 
 
How To Use Ag-Grid In Angular 8 With Sorting And Floating Filter
 

Conclusion

 
In this article, we have seen how to use the  Ag-Grid feature  in Angular 8 with filter and sorting.
 
Please give your valuable feedback/comments/questions about this article.