Overview of Grid in Essential JS 2: Part 1 | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (173).NET Core  (29).NET MAUI  (203)Angular  (107)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (211)BoldSign  (13)DocIO  (24)Essential JS 2  (106)Essential Studio  (200)File Formats  (65)Flutter  (132)JavaScript  (219)Microsoft  (118)PDF  (81)Python  (1)React  (98)Streamlit  (1)Succinctly series  (131)Syncfusion  (897)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (50)Windows Forms  (61)WinUI  (68)WPF  (157)Xamarin  (161)XlsIO  (35)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (146)Chart  (127)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (618)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (39)Extensions  (22)File Manager  (6)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (501)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (42)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (381)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (17)Web  (582)What's new  (323)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)

Overview of Grid in Essential JS 2: Part 1

The Essential JS 2 grid is a modern, lightweight, feature-rich, and open-source grid for Angular with blazing-fast performance. It has been built using TypeScript with modular architecture, which helps you load only the needed modules on demand.

In this article, we are going to walk you through how to create a grid and inject features such as paging, sorting, filtering, grouping, and aggregation in an Angular application. The final code can be found at our GitHub repository.

Set Up the Development Environment

You need to set up the development environment before creating a grid. Since the source is available in  GitHub and packages are available in npm, you can get started with grid in very few steps.

Creating an Angular Application

To create an Angular application, we need to install Angular CLI globally using the following command.

npm install -g @angular/cli

Then create a new Angular application using the following command.

ng new my-app

This will download all the files we need and initialize the npm install.

Grid Installation through Command Prompt

Once you have created the Angular application, you need to use the following command to install grid.

cd my-app
npm install @syncfusion/ej2-ng-grids --save

–save will save grid in the dependencies of package.json.

Configuring Grid

Now all the configuration related to environment is completed. Next you need to configure grid. Before configuration, we need a component where grid will render. To create an Angular component, you need to use the following Angular CLI command.

ng generate component grid

The new component has been created successfully.

Themes provide life to components. Grid has different themes. They are:

  •          Material
  •          Office 365
  •          Bootstrap
  •          High Contrast

In this demo application, the material theme will be used for grid. To add the theme, you need to import material.css in styles.css.

@import '../node_modules/@syncfusion/ej2-grids/styles/material.css';

After adding the theme, you need to import grid module in app.module.ts.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { GridComponent } from './grid/grid.component';
import { GridModule } from '@syncfusion/ej2-ng-grids';

@NgModule({
  declarations: [
    AppComponent,
    GridComponent
  ],
  imports: [
    BrowserModule,
    GridModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Creating the Grid Component

We have successfully completed configuration related to grid. Now you need to define your first grid in grid.component.html.

<ejs-grid></ejs-grid>

Then add the grid component in app.compnent.html.

<app-grid></app-grid>

Defining Row Data

After creating the grid component, you need to define an array of JavaScript objects in grid.component.ts.

  this.data = [
      { OrderID: 10248, CustomerID: 'VINET', Freight: 32.38 },
      { OrderID: 10249, CustomerID: 'TOMSP', Freight: 11.61 },
      { OrderID: 10250, CustomerID: 'HANAR', Freight: 65.83 },
      { OrderID: 10251, CustomerID: 'VICTE', Freight: 41.34 },
      { OrderID: 10252, CustomerID: 'SUPRD', Freight: 51.3 },
      { OrderID: 10253, CustomerID: 'HANAR', Freight: 58.17 },
      { OrderID: 10254, CustomerID: 'CHOPS', Freight: 22.98 },
      { OrderID: 10255, CustomerID: 'RICSU', Freight: 148.33 },
      { OrderID: 10256, CustomerID: 'WELLI', Freight: 13.97 },
    ];

Bind the data to the grid using the dataSource property, which accepts an array of JavaScript objects.

  <ejs-grid [dataSource]="data"></ejs-grid>

Defining Columns as Directives

Grid has an option to define columns as directives. In these column directives, we have properties to customize columns. Let’s check the properties used here:

  •          We have added field to map with a property name an array of JavaScript objects.
  •          We have added headerText to change the title of columns.
  •          We have used textAlign to change the alignment of columns. By default, columns will be left aligned. To change columns to right align, we need to define textAlign as Right.
  •          Also, we have used another useful property, format. Using this, we can format number and date values to standard or custom formats. Here, we have defined it for the conversion of numeric values to currency.

For this demo, we are going to define columns like the following.

 
 <ejs-grid [dataSource]='data'>
   <e-columns>
     <e-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-column>
     <e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
     <e-column field='Freight' textAlign='Right' format='c2' width=120></e-column>
    </e-columns>
 </ejs-grid>

Serve the Application

Go to the application directory and launch the server using the following commands.

ng serve –open

Once all the files are compiled successfully, it will serve the site.

Grid should look something like this.

Injecting Features

Now, you have the basic working grid with data populated. We are going to learn how to enable some more commonly used built-in features. Before adding these features, we need to define services in providers of app.module.ts. These services will provide a way to access their functionalities from grid.

import { GridModule, PageService, SortService, FilterService, GroupService, AggregateService } from '@syncfusion/ej2-ng-grids';

@NgModule({
  declarations: [
    AppComponent,
    GridComponent
  ],
  imports: [
    BrowserModule,
    GridModule
  ],
  providers: [PageService, SortService, FilterService, GroupService, AggregateService],
  bootstrap: [AppComponent]
})
export class AppModule { }

Paging

Once the PageService is injected in providers, we can gain access to paging functionalities. Also, we need to enable allowPaging in grid to enable the pager.

As of now, default paging is enabled. Now I am going to customize the default pager with pageSettings. Here we defined the pageSettings.pageSize as 3 to define the number of records to be displayed per page.

  <ejs-grid [dataSource]='data' [allowPaging]='true' [pageSettings]="pageSettings">
    <e-columns>
      <e-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-column>
      <e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
      <e-column field='Freight' textAlign='Right' format='c2' width=120></e-column>
    </e-columns>
  </ejs-grid>
@Component({
  selector: 'app-grid',
  templateUrl: './grid.component.html',
  styleUrls: ['./grid.component.css'],
  encapsulation: ViewEncapsulation.None
})
export class GridComponent implements OnInit {
  public data: Object[];
  public pageSettings: Object;
  constructor() { }
  ngOnInit() {
    this.data = [
      { OrderID: 10248, CustomerID: 'VINET', Freight: 32.38 },
      { OrderID: 10249, CustomerID: 'TOMSP', Freight: 11.61 },
      { OrderID: 10250, CustomerID: 'HANAR', Freight: 65.83 },
      { OrderID: 10251, CustomerID: 'VICTE', Freight: 41.34 },
      { OrderID: 10252, CustomerID: 'SUPRD', Freight: 51.3 },
      { OrderID: 10253, CustomerID: 'HANAR', Freight: 58.17 },
      { OrderID: 10254, CustomerID: 'CHOPS', Freight: 22.98 },
      { OrderID: 10255, CustomerID: 'RICSU', Freight: 148.33 },
      { OrderID: 10256, CustomerID: 'WELLI', Freight: 13.97 },
    ];
    this.pageSettings = { pageSize: 3 };
  }
}

After enabling paging, grid will be like this:

 

Sorting

After SortService is injected in providers, we need to enable allowSorting in grid to gain its functionalities.

  <ejs-grid [dataSource]='data' [allowPaging]='true' [allowSorting]='true' [pageSettings]="pageSettings">
    <e-columns>
      <e-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-column>
      <e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
      <e-column field='Freight' textAlign='Right' format='c2' width=120></e-column>
    </e-columns>
  </ejs-grid>

Interact with column headers to sort rows based on columns in either ascending or descending order.

Filtering

Grid has different filter UIs, such as filter bar, filter menu, check box, and Excel-like filters. In this demo application, we are going to use filter menu to filter records. For injecting this filtering feature, we need to define FilterService in providers. Also, to gain filter functionalities and define the filter UI, we need to enable allowFIltering and define filterSettings.type as Menu.

  <ejs-grid [dataSource]='data' [allowPaging]='true' [allowSorting]='true' [allowFiltering]='true'
   [filterSettings]='filterSettings' [pageSettings]="pageSettings">
    <e-columns>
      <e-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-column>
      <e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
      <e-column field='Freight' textAlign='Right' format='c2' width=120></e-column>
    </e-columns>
  </ejs-grid>
@Component({
  selector: 'app-grid',
  templateUrl: './grid.component.html',
  styleUrls: ['./grid.component.css'],
  encapsulation: ViewEncapsulation.None
})
export class GridComponent implements OnInit {
  public data: Object[];
  public pageSettings: Object;
  public filterSettings: Object;
  constructor() { }

  ngOnInit() {
    this.data = [
      { OrderID: 10248, CustomerID: 'VINET', Freight: 32.38 },
      { OrderID: 10249, CustomerID: 'TOMSP', Freight: 11.61 },
      { OrderID: 10250, CustomerID: 'HANAR', Freight: 65.83 },
      { OrderID: 10251, CustomerID: 'VICTE', Freight: 41.34 },
      { OrderID: 10252, CustomerID: 'SUPRD', Freight: 51.3 },
      { OrderID: 10253, CustomerID: 'HANAR', Freight: 58.17 },
      { OrderID: 10254, CustomerID: 'CHOPS', Freight: 22.98 },
      { OrderID: 10255, CustomerID: 'RICSU', Freight: 148.33 },
      { OrderID: 10256, CustomerID: 'WELLI', Freight: 13.97 },
    ];
    this.pageSettings = { pageSize: 3 };
    this.filterSettings = { type: 'Menu' };
  }
}

After defining the filter menu, the filter UI would be like this:

Grouping

If GroupService is injected in providers, then we need to enable allowGrouping in grid to access its functionalities.

  <ejs-grid [dataSource]='data' [allowPaging]='true' [allowSorting]='true' [allowFiltering]='true'
   [allowGrouping]='true' [filterSettings]='filterSettings' [pageSettings]="pageSettings">
    <e-columns>
      <e-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-column>
      <e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
      <e-column field='Freight' textAlign='Right' format='c2' width=120></e-column>
    </e-columns>
  </ejs-grid>

Aggregation

To use aggregation in grid, we need to inject AggregateService. After injecting the service, we need to define aggregations as directives to direct a child of grid. In this demo, we are going to use the Sum built-in aggregate for freight column.

  <ejs-grid [dataSource]='data' [allowPaging]='true' [allowSorting]='true' [allowFiltering]='true'
   [allowGrouping]='true' [filterSettings]='filterSettings' [pageSettings]="pageSettings">
    <e-columns>
      <e-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-column>
      <e-column field="CustomerID" headertext="Customer ID" width="120"></e-column>
      <e-column field="Freight" textalign="Right" format="c2" width="120"></e-column>
    </e-columns>
    <e-aggregates>
      <e-aggregate>
          <e-columns>
              <e-column type="Sum" field="Freight" format="C2">
                  <ng-template #footerTemplate let-data>Sum: {{data.Sum}}</ng-template>
              </e-column>
          </e-columns>
      </e-aggregate>
  </e-aggregates>
  </ejs-grid>

After defining aggregation, the total sum is rendered in the footer element of grid.

Summary

In this blog, we have learned how to create a simple grid and enhance its fundamental features using Angular CLI and grid from a node package manager. Grid has a lot of other advanced features, such as data binding with different adaptors, data manipulation (CRUD), column resizing, column choosing, column menu, state persistence, copying to clipboard, responsiveness, and virtualization. There is a lot more we can do with grid! In our next blogs, we can see how easy it is to configure these advanced features.

Feel free to visit the grid source in GitHub, sample browser and documentation to explore live samples of its features and  API. Also be sure to check out the grid sample in GitHub, which is readily runnable, and see just how easy it is to create and configure grid.

If you have any questions or require clarification, please let us know in the comments section. You can also contact us through our support forum or Direct-Trac. We are happy to assist you!

Tags:

Share this post:

Comments (4)

whether it is free!

The whole suite of Syncfusion controls is available for free (commercial applications also) through the community license program if you qualify (less than 1 million US Dollars in revenue). The community license is the full product with no limitations or watermarks.

For more information, please check our community license page
https://www.syncfusion.com/products/communitylicense

I need dynamically add columns in grid
I write this in my code, but it can not word :

How can I do?

Hi WILLS

To dynamically add columns in grid, we suggest you to add a new column by pushing it into the columns collection through external button and call the refreshColumns method to update Grid UI to show the newly added column in Grid.

Please refer the below code example and sample for more information.

[app.component.html]

// Put a new column name here

// It is perform adding new column to Grid’s Columns
Add columns

[app.component.ts]

AddColumns() {
var colName = (document.getElementsByClassName(‘form-control’)[0] as any).value;
// generate column in object format
var obj = {field: colName, headerText: colName, width:80};
//you can add the columns by using the Grid columns method
this.grid.columns.push(obj as any);
this.grid.refreshColumns();
}

Sample link: https://stackblitz.com/edit/angular-ws2fqv-pjqaaw?file=app.component.html

Please get back to us, if you need further assistance.

Regards,
Maithiliy K

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed