DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Introduction to Angular Grid

Introduction to Angular Grid

In this post, we look at how to at ag-Grid to Angular applications, going over all the code and commands you'll need.

Swathi Prasad user avatar by
Swathi Prasad
·
May. 07, 19 · Tutorial
Like (4)
Save
Tweet
Share
16.59K Views

Join the DZone community and get the full member experience.

Join For Free

Displaying and manipulating tabular data is often necessary in software applications. In this article, I will introduce ag-Grid for Angular applications. ag-Grid is implemented in TypeScript and can be used for displaying both simple and complex data. It also gives good user experience.

Adding ag-Grid to Angular Project

Create a project using Angular CLI and install ag-grid-community and ag-grid-angular through npm.

npm install --save ag-grid-community ag-grid-angular

Import the ag-Grid styles globally in styles.scss.

@import "~ag-grid-community/dist/styles/ag-grid.css"; 
@import "~ag-grid-community/dist/styles/ag-theme-blue.css";

The ag-grid.css imports Gird styles and ag-theme-blue.css is one of the available grid themes. There are many themes available within the module. You can either choose the one that matches your project design or customize styles through Sass variables.

Let's include the module app.module.ts.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import { AgGridModule } from 'ag-grid-angular';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    AgGridModule.withComponents([])
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Here, the withComponents call is required to use Angular components in the grid.

Add the grid definition in app.component.ts.

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'Angular ag-Grid';

  columnDefs = [
        { headerName: 'Employee Name', field: 'name' },
        { headerName: 'Title', field: 'title' },
        { headerName: 'Employee Number', field: 'number' },
        { headerName: 'Date of Joining', field: 'date’  }
    ];

    rowData = [
        { name: 'John', title: 'Software Engineer', number: 123456, date: 'March 02, 2016' },
        { name: 'Jane', title: 'Senior Software Engineer', number: 123451, date: 'April 01, 2014' },
        { name: 'Richard', title: 'Software Engineer', number: 123452, date: 'January 02, 2015' },
        { name: 'Janie', title: 'Software Engineer', number: 123453, date: 'March 23, 2016' },
        { name: 'Johnny', title: 'Senior Software Engineer', number: 123454, date: 'September 01, 2017' }
    ];
}

We just added two properties i.e., column definitions and row data. Note that the row data can also be dynamically loaded. More information about column definition can be found here.

Let's add the grid component on app.component.html. Here is the complete HTML file.

<div style="text-align:center">
  <h1>
    Welcome to {{ title }}!
  </h1>
</div>

<ag-grid-angular
    style="width: 800px; height: 155px; margin: auto"
    class="ag-theme-blue"
    [rowData]="rowData"
    [columnDefs]="columnDefs">
</ag-grid-angular>

<router-outlet></router-outlet>

It is possible to add, sort, and filter features on the grid. Just add the feature in column definition as follows.

columnDefs = [
        { headerName: 'Employee Name', field: 'name', sortable:true, filter:true },
        { headerName: 'Title', field: 'title', sortable:true, filter:true },
        { headerName: 'Employee Number', field: 'number', sortable:true, filter:true },
        { headerName: 'Date of Joining', field: 'date', sortable:true, filter:true }
    ];

We can also add checkbox selection and multiple row selections to the grid.

columnDefs = [
        { headerName: 'Employee Name', field: 'name', sortable:true, filter:true, checkboxSelection: true },
        { headerName: 'Title', field: 'title', sortable:true, filter:true },
        { headerName: 'Employee Number', field: 'number', sortable:true, filter:true },
        { headerName: 'Date of Joining', field: 'date', sortable:true, filter:true }
    ];

In the HTML, just add the rowselection property.

<ag-grid-angular
    style="width: 800px; height: 155px; margin: auto"
    class="ag-theme-blue"
    [rowData]="rowData"
    [columnDefs]="columnDefs"
    rowSelection="multiple">
</ag-grid-angular>

Let us run the  ng serve command to see our little app. Here is the final app that shows ag-Grid.


Angular Grid

Conclusion

ag-Grid has many more features to offer and it is easy to integrate into Angular projects. Checkout their complete documentation here. The complete example for this article can be found on my GitHub repository.

AngularJS

Published at DZone with permission of Swathi Prasad, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is Testing as a Service?
  • How Observability Is Redefining Developer Roles
  • Data Engineering Trends for 2023
  • The Role of Data Governance in Data Strategy: Part II

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: