未加星标

Implementing ag-Grid in Angular 2

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
What is ag-Grid?

ag-Gridis a javascript Data Grid for Building Enterprise Web Applications. It is one of the most widely used grid. It provides features like sorting, filtering , selection , re-sizing and many more.

Here’s an inspiring article of how ag-grid was developed.

Prerequisite :

To understand how to implement ag-Grid in your Angular 2 application, you need to have a basic knowledge of Angular 2 and typescript .

Following are the documentations that you can refer :

Angular 2 Typescript So, LET’S BEGIN !!!

Followthese steps to set up ag-Grid in your Angular 2 application:

Step 1 :

First of all, you need to add dependencies to your project. In your package.json , add the following code :

"dependencies": { ... "ag-grid": "6.1.0", "ag-grid-ng2": "6.1.0" }

Make sure to have the latest version of ag-Grid.

Step 2 :

Update System.config.js to use ag-grid in your project.

System.config({ packages: { ... 'ag-grid-ng2': { defaultExtension: "js" }, 'ag-grid': { defaultExtension: "js" } }, map: { ... 'ag-grid-ng2': 'node_modules/ag-grid-ng2', 'ag-grid': 'node_modules/ag-grid' } }); Step 3 :

Import AgGridModule in the Module where you want to use it.

import {AgGridModule} from 'ag-grid-ng2/main'; @NgModule({ imports: [ BrowserModule, AgGridModule.forRoot(), ... })

forRootpart is to ensure that the providers of AgGrid are injected at the root level.

Step 4:

And, the last thing is to include the CSS for ag-Grid. You can do it directly in your index.html

<link href="node_modules/ag-grid/dist/styles/ag-grid.css" rel="stylesheet" /> <link href="node_modules/ag-grid/dist/styles/theme-material.css" rel="stylesheet" />

That’s it. The setup is complete.

Let’s move on to the implementation

Create a component where you want to use your grid.

@Component({ selector: 'ag-grid', template: ` <ag-grid-ng2 style="width: 80%; margin-left: 10%" #agGrid class="ag-material" [gridOptions]="myGridOptions"> </ag-grid-ng2>` }) export class MyGridComponent{ }

In the template, we have <ag-grid-ng2> tag. This is provided by theAgGridModule we imported before.The thing to notice is the “gridOptions” property, we have assigned it to a variable named “myGridOptions”, and therefore we need to initialize that variable in the component. Add the following code in the component :

private myGridOptions: GridOptions = {};

This initializes an empty grid. Now, obviously, we need some data in our grid.

To add date to an ag-Grid, we have two properties of GridOptions:

rowData columnDefs

we can initialize both of them on ngOninit .Import OnInit and implement it to the component, and add the following code :

import { OnInit } from [email protected]/core'; export class MyGridComponent implements OnInit{ ngOnInit() { this.myGridOptions.rowData = this.createRowData(); this.myGridOptions.columnDefs = this.createColumnDefs(); } private createColumnDefs() { return [ {headerName: "NAME", field: "name", width: 100}, {headerName: "AGE", field: "age", width: 100}, {headerName: "GENDER", field: "gender", width: 100} ] } //data gets mapped to the corresponding "field" key of the headers private createRowData() { return [ {name: "Geller", age: 30, gender: "Male"}, {name: "Geller", age: 28, gender: "Female"}, {name: "Tribbiani", age: 29, gender: "Male"}, {name: "Bing", age: 30, gender: "Male"}, {name: "Green", age: 28, gender: "Female"}, {name: "Buffay", age: 29, gender: "Female"} ]; } }

Here we have two methods that add the headers and row data into the grid.

This is how your grid should look like :


Implementing ag-Grid in Angular 2

We are using the material theme , but you can choose whichever one you like from here.

Now, in a general scenario, you’ll be getting this data from a service . Let’s create a mock service for that. But before doing that, we should create a type for our data.

Create a file “friends.ts”, and create a class Friends as follows :

export class Friends{ name: string; age: number; gender: string; }

Now, for the mock service , create a file friends.service.ts, and add the following code :

import {Friends} from "./friends"; export class FriendsService{ getDate() { let friends: Friends[] = [ {name: "Geller", age: 30, gender: "Male"}, {name: "Geller", age: 28, gender: "Female"}, {name: "Tribbiani", age: 29, gender: "Male"}, {name: "Bing", age: 30, gender: "Male"}, {name: "Green", age: 28, gender: "Female"}, {name: "Buffay", age: 29, gender: "Female"}, ]; return friends } }

To use the service, add it to the providers section of your ngModule .

Now, we need to update our methods in the component. Rows will directly get mapped, because rowData property needs an array of any type, so all we need to do is update the createColumnDefs method.

constructor(private friendsService: FriendsService){} ngOnInit() { let data: Friends[] = this.friendsService.getDate(); this.myGridOptions.rowData = data; this.myGridOptions.columnDefs = this.createColumnDefs(data[0]); } private createColumnDefs(friends: Friends) { let keyNames = Object.keys(friends); let headers = []; keyNames.map(key => { headers.push({ headerName: key.toUpperCase(), field: key, width: 100 }) }); return headers; }

We simply extract the keys from the data, add properties like width and field, and push it into an array.

Here’s the example project that you can refer to.

So that’s it. Now you can implement ag-Grid in your Angular 2 application . To use more features provided by ag-Grid, refer the documentation here.


Implementing ag-Grid in Angular 2

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: JavaScriptCSSJava
分页:12
转载请注明
本文标题:Implementing ag-Grid in Angular 2
本站链接:http://www.codesec.net/view/481768.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(34)