Skip to main content Skip to footer

How to Quickly Create an Angular Datagrid in Your Web Application

With automatic conveniences like sorting and autocomplete and advanced features like custom formulas, Microsoft Excel would be the natural choice for most users. Users choose excel because it is familiar.

Although Excel has become the most common example of software using a datagrid, the paradigm has increased throughout the software field. As web apps continue to replace the traditional native applications we're used to, it only makes sense that datagrids would make their way to web app UIs.

Plenty of JavaScript datagrids exist in the market: open-source, third-party, and homegrown. Which to choose?

In another one of our blogs, we discussed what makes datagrids so useful as UI elements and why FlexGrid is the best Angular datagrid. You can see Wijmo's Angular DataGrid in action and learn more about it through our Angular datagrid overview sample or by checking out the video below.

You can get your copy of Wijmo from NPM, from our CDN, or by downloading it from our site.

All this sounds amazing, right? Let's put FlexGrid to work by building a customizable Angular datagrid in minutes. We'll use the following application to demonstrate how to use FlexGrid with Angular, Google's web framework: Angular Sample App.

We use Stackblitz to create the sample, so it is easy to maintain and share.

Let's get started!

Ready to Create Custom Dashboards? Download Wijmo Today!

How to Create the Angular App

To create your copy of the app, follow these steps:

  1. Open Stackblitz
  2. Click the "Angular/TypeScript" button at the top of the screen
  3. Add Wijmo to the project by typing "@grapecity/wijmo.all" into the dependencies list

How to Quickly Create an Angular Datagrid in Your Web Application

How to Import the Required Modules

In the Angular version of the app, the logic is contained within the app.component.ts file, and the markup is contained within the app.component.html file.

We'll start by importing the required classes and styles from Angular and Wijmo:

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { WjGridModule } from '@grapecity/wijmo.angular2.grid';
import { WjGridFilterModule } from '@grapecity/wijmo.angular2.grid.filter';
import { AppComponent } from './app.component';
import { DataService } from './data.service';

@NgModule({
  imports: [BrowserModule, FormsModule, WjGridModule, WjGridFilterModule],
  providers: [DataService],
  declarations: [AppComponent],
  bootstrap: [AppComponent],
})
export class AppModule {}

app.component.ts

import { Component } from '@angular/core';
import * as WjCore from '@grapecity/wijmo';
import { DataService } from './data.service';

styles.css

@import "wijmo/styles/themes/wijmo.theme.material.css";

Notice how, in addition to the modules, the code imports some CSS. In this case, we chose Wijmo's material theme, one of several included with Wijmo.

Styling the App

To style the app, edit the styles.css file as follows:

body {
  font-family: Lato, Arial, Helvetica;
}
.wj-flexgrid { /* limit the grid's width and height */
  max-height: 300px;
  max-width: 32em;
}

Our data source will be stored within our app.component.ts file, which is defined as follows:

import { Component } from '@angular/core';
import * as WjCore from '@grapecity/wijmo';
import { DataService } from './data.service';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  // create our CollectionView
  data = new WjCore.CollectionView([], {
    groupDescriptions: ['make'], // group by make
  });

  // and populate it from the data.service.ts file
  constructor(private dataService: DataService) {
    this.data.sourceCollection = dataService.getSedanData();
  }
}

The app.component.ts file exposes the data through its "data" property, a CollectionView grouped by make. The data is loaded asynchronously after the component is constructed.

When the data is received, it is assigned to the CollectionView's sourceCollection property and becomes available to the application.

How to Visualize the Data in Angular

The final step is implementing the markup inside of the app.component.html file to render the Angular DataGrid:

<h1>2023 Sedans (Angular)</h1>
<p>
  Sort by model and price by clicking the column headers. Filter by value or
  condition by clicking the filter icons in the column headers. Collapse and
  expand makes to see all the models. Select one or more models with the mouse
  or keyboard and copy your selection to the clipboard.
</p>
<p>
  Showing
  <b>{{ data.items.length }}</b> models from
  <b>{{ data.groups.length }}</b> makes.
</p>
<wj-flex-grid
  [allowResizing]="'None'"
  [showAlternatingRows]="false"
  [isReadOnly]="true"
  [selectionMode]="'ListBox'"
  [headersVisibility]="'Column'"
  [itemsSource]="data"
>
  <wj-flex-grid-column
    [binding]="'make'"
    [header]="'Make'"
    [width]="'*'"
    [visible]="false"
  ></wj-flex-grid-column>
  <wj-flex-grid-column
    [binding]="'model'"
    [header]="'Model'"
    [width]="'*'"
  ></wj-flex-grid-column>
  <wj-flex-grid-column
    [binding]="'price'"
    [header]="'Price'"
    [format]="'c0'"
    [aggregate]="'Avg'"
    [width]="'.5*'"
  ></wj-flex-grid-column>
  <wj-flex-grid-filter></wj-flex-grid-filter>
</wj-flex-grid>

The app header contains a short app description and a summary of how many models and makes are displayed. The summary counts are updated automatically when the user filters the data. The header is followed by the "FlexGrid" element, which initializes the grid's properties, including the itemsSource, the columns to display, and their properties.

The column properties include the binding, header, format, and width for each column. Note that the aggregate property on the price column causes the grid to show the average price for each make in the group headers.

The "FlexGrid" element contains a "FlexGridFilter" element, which adds the column filter so that users may filter the grid data by model and price.

We've also included some data bindings connected to the CollectionView, which we use to show how many makes and models are being displayed in the FlexGrid, based on the filters the user has applied.

How to Run the Application

That's it! The Angular version of the "2023 Sedans" app is ready.

Happy coding! If you have questions or comments, be sure to enter them below.

Ready to Create Custom Dashboards? Download Wijmo Today!

Tags:

comments powered by Disqus