- ag stands for Agnostic which means independent of any framework.
- 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.
- It is mainly used to display table data with rows and columns with datafilters.
-
Support for multiple frameworks with the same API.
-
GUI layer tailored for each framework for the best developer experience and performance.
-
Community Edition is completely free, even for commercial use.
-
Our code is open source so you can review the code as part of your evaluation.
-
Dedicated support team for Enterprise customers.
-
Features no other Data Grid provides like pivoting, grouping, integrated charts.
To Create any angular project , first create a folder and go inside the folder
Type the below command in terminaal to install angular cli
npm install -g @angular/cli OR
npm i -g @angular/cli OR
npm install @angular/cli@latest (To leatest version) OR
To generate new project we must type the below command to get all library files from angular
ng new app-name
- Output screenshot:
To run the angular project in your local host : 4200 port type below command
ng serve (A link will be generated , need to be open the page by clicking the link) OR
ng s OR
ng serve -o (To open directly in browser) OR
ng s -o
- Sample ScreenShots:
Instll the ag-grid community by following command
npm install ag-grid-community
Instll the ag-grid-angular by following command
npm install ag-grid-angular
Import the AgGridModule in the module.ts file and also update the imported module in below declarations as follows:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { AgGridModule } from 'ag-grid-angular';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AgGridModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
To display the ag-grid in dispay we shoul import the styles in style.css as below:
@import "ag-grid-community/dist/styles/ag-grid.css";
@import "ag-grid-community/dist/styles/ag-theme-alpine.css";
Bind the ag-rid module in html as follows
<ag-grid-angular
class="ag-theme-alpine"
style="height:100vh;width:100vw"
[rowData]="rowData"
[columnDefs]="colDefs"
>
<ag-grid-angular>
NOTE : here , class and mentioning width and height is maditory to show the grid data in browser
To get the values for the html attribute , we must pass those inapp.component.ts file according to the variables as below:
import { Component } from '@angular/core';
import { ColDef } from 'ag-grid-community';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
rowData : any[] =[
{make : 'Toyota' , model : 'celino' , price :100000},
{make : 'Toyota' , model : 'celino' , price :560000},
{make : 'Toyota' , model : 'celino' , price :450000},
];
colDefs : ColDef[] =[
{field : 'make'},
{field : 'model'},
{field : 'price'},
];
}
First import the HttpClientModule in app.module.ts file
...
import { HttpClientModule } from '@angular/common/http';
...
Attach the imported module in the imports in same app.module.ts as below:
...
imports: [
BrowserModule,
AgGridModule,
HttpClientModule
],...
In app.component.ts , make the rowdata variable to rowdata$ with the type observable
rowData$!: Observable<any[]>;
constructor(private http: HttpClient){ }
Initialize the ngOnInit metho in the export class itself as below:
export class AppComponent implements OnInit {
...
}
Define the ngOnit method like below using the get method with prviding the URL of the row data
ngOnInit(){
this.rowData$ = this.http.get<any[]>('https://www.ag-grid.com/example-assets/row-data.json')
}
use the async method to bind the observable array with URL
[rowData]="rowData$ | async"
- It is very easy to achive by adding sortable and filter values set to be TRUE in app.component.ts file within the columDefs as below:
colDefs : ColDef[] =[
{field : 'make' , sortable:true , filter:true },
{field : 'model', sortable:true , filter:true },
{field : 'price', sortable:true , filter:true },
];
- It can be achieved by other method as below : adding defaultColDef in htlm and ive the vlues for them in ts file:
...
[defaultColDef]="defaultColDef"
...
colDefs : ColDef[] =[
{field : 'make' , },
{field : 'model',},
{field : 'price', },
];
defaultColDef : ColDef = {
sortable:true , filter:true
}
- Output ScreenShot:
- We can select the multiple rows and animate them with sorting by adding attribute in htlm as below:
...
[rowSelection]="'multiple'"
[animateRows]="true"
...
-
There are lot of events available in grid . Here we just try a simple event to console something by click is heppend by a user in any cell.
-
We must bind the event in the html as follows :
(cellClicked)="onCellClicked($event)"
- After that , we must define the event in .ts file . Here we just dispay the events in console.
onCellClicked(event : CellClickedEvent){
console.log(event);
}
- Output Screenshot:
- There are lot of methods to use. Here i have shared a simple @viewchld method in ts as follows:
@ViewChild(AgGridAngular) agGrid!: AgGridAngular;
- To make use of this , i have created a clearall button to deselect all the selected rows from api as with below method:
clearselection(){
this.agGrid.api.deselectAll();
}
- Finaly add a button in html and bind the clearselection method in it.
<button (click) = "clearselection()"> clear all </button>
...
- Output:
- domLayout is an attribute which can be useg in ag-grid componenet with the any of following three values as follows:
- domlayout = "normal" --> It displays the normal grid.
- domlayout = "autoHeight" --> It adjustes the rows and provide scroll bar.
- domlayout = "print" --> No scroll bars provided.
-
In simple words , it helps to define data types in seperate folder.
-
models in angular is used to store the data.
-
Mostly it deponds on two different types as 1 --> interface 2 --> classes
-
The reason to use models in application is to validate the data type of an typical data strctures like objects or table data.
-
We can export the model interface files and can be obtained whereever we need in our application by importing it.
- We must use the below command in required directory .
ng g interface modelFileName --type=model
- Example :
ng g interface learn-model --type=model
- Output :
- We may create class also that belongs to the user's requirement.
Example :
ng g class learn-model --type=model
- NOTE : We can do this mannaly without generating angular cli .
-
Mostly , we will use this model for row data attribute in ag-grid .
-
So , let's take an simple component to display the row data.
- Assign [rowData] and [columnDefs] in ag-grid.
- First of all , assign the variable with public keyword and assign it to the interface name with [] and with the ow data .
- Kindly import the inteface in the .ts to utilize the interface for validatio of similar datatype.
- Just need to assign the row data values to correct datatype reqired like number,boolean and etc.
-
The purpose of model interface is to omit unwanted errors in application as it is very large program.
-
It will show error if the data is mismatches with the intrface datatypes.