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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Rediscovering Angular: Modern Advantages and Technical Stack
  • Angular Input/Output Signals: The New Component Communication
  • Azure Deployment Using FileZilla
  • Angular RxJS Unleashed: Supercharge Your App With Reactive Operators

Trending

  • How to Configure and Customize the Go SDK for Azure Cosmos DB
  • A Modern Stack for Building Scalable Systems
  • Performance Optimization Techniques for Snowflake on AWS
  • Contextual AI Integration for Agile Product Teams
  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.

By 
Swathi Prasad user avatar
Swathi Prasad
·
May. 07, 19 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
17.5K 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.

Related

  • Rediscovering Angular: Modern Advantages and Technical Stack
  • Angular Input/Output Signals: The New Component Communication
  • Azure Deployment Using FileZilla
  • Angular RxJS Unleashed: Supercharge Your App With Reactive Operators

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!