Angular.js Migration Table
Angular Material data tables give a fast and effective way to create tables of data with common features like pagination. The pre requirements to use Angular Material Table are:
You should have some knowledge about TypeScript and Angular
You should have Node.js and npm installed in your system
Angular CLI 10 installed on your system
Angular 10 project
Some data to display in the data table.
First step is to set up Angular Material. Create a project using Angular CLI 10. Use ng add command to set up Angular 8 in your project.
$ cd your_angular_project $ ng add @angular/materialSecond step is to import the Angular Material Data Table module. Import the Material Table module in the app.module.ts file. Now open the src/app.module.ts file in which the main application module is present. Add the following code.
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import {MatTableModule} from '@angular/material/table'; import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, MatTableModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }First we import MatTableModule from @angular/material package and add this Module into the imports array of the application module. Now use Material table components to create Material tables in your application. We are going to create a Material Table with
tableColumns : string[] = ['policyNumber', 'creationDate', 'expireDate', 'policyAmount', 'clientId', 'employeeId'];Next step is to define the material table’s rows. To define it include
dataSource = [];You already have DataService that provides your application with data from the Rest API back-end and policy model that encapsulates an insurance policy type. In src/app/app.component.ts file add the following imports.
import { DataService } from "./data.service"; import { Policy } from "./policy"; Inject DataService as dataService instance through component’s constructor. constructor(private dataService: DataService) {}In the ngOnInit life-cycle event of Angular component,call .getPolicies method and subscribe to returned Observable:
ngOnInit() { this.dataService.getPolicies().subscribe((result)=>{ this.dataSource = result.body; }) }
Comments
0 comments
Please Sign in or Create an account to Post Comments