How to create a mat table in Angular

0 votes
Can someone explain to me with the code and example how to create a mat table in Angular?
Mar 6 in Angular by Nidhi
• 12,580 points
70 views

1 answer to this question.

0 votes

1. Install Angular Material

ng add @angular/material

2. Import MatTableModule

import { NgModule } from '@angular/core';

import { BrowserModule } from '@angular/platform-browser';

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { MatTableModule } from '@angular/material/table';

import { AppComponent } from './app.component';

@NgModule({

  declarations: [AppComponent],

  imports: [

    BrowserModule,

    BrowserAnimationsModule,

    MatTableModule,

  ],

  providers: [],

  bootstrap: [AppComponent],

})

export class AppModule {}

3. Define Table Data

import { Component } from '@angular/core';

export interface UserData {

  id: number;

  name: string;

  age: number;

}

const ELEMENT_DATA: UserData[] = [

  { id: 1, name: 'John', age: 30 },

  { id: 2, name: 'Jane', age: 25 },

  { id: 3, name: 'Doe', age: 40 },

];

@Component({

  selector: 'app-root',

  templateUrl: './app.component.html',

  styleUrls: ['./app.component.css'],

})

export class AppComponent {

  displayedColumns: string[] = ['id', 'name', 'age'];

  dataSource = ELEMENT_DATA;

}

4. Create the Table Template

<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

  <!-- ID Column -->

  <ng-container matColumnDef="id">

    <th mat-header-cell *matHeaderCellDef>ID</th>

    <td mat-cell *matCellDef="let element">{{ element.id }}</td>

  </ng-container>

  <!-- Name Column -->

  <ng-container matColumnDef="name">

    <th mat-header-cell *matHeaderCellDef>Name</th>

    <td mat-cell *matCellDef="let element">{{ element.name }}</td>

  </ng-container>

  <!-- Age Column -->

  <ng-container matColumnDef="age">

    <th mat-header-cell *matHeaderCellDef>Age</th>

    <td mat-cell *matCellDef="let element">{{ element.age }}</td>

  </ng-container>

  <!-- Header and Row Definitions -->

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>

  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>

</table>

Run the Application

ng serve

answered Mar 6 by Anvi

Related Questions In Angular

0 votes
1 answer

How to know tools and bundlers after create a new workspace or a project in angular?

Hello @sajal, When you create projects and workspaces ...READ MORE

answered Aug 6, 2020 in Angular by Niroj
• 82,840 points
1,174 views
0 votes
2 answers

How to detect a route change in Angular?

Hii Kartik For Angular 7 someone should write like: this.router.events.subscribe((event: Event) ...READ MORE

answered Apr 22, 2020 in Angular by Niroj
• 82,840 points
29,551 views
0 votes
1 answer

How to create a URL in the controller .NET MVC?

Hello @kartik, If you just want to get ...READ MORE

answered Jul 23, 2020 in Angular by Niroj
• 82,840 points
6,384 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Cannot find name webgl object TypeScript issue TS2304.

The error Cannot find name 'WebGLObject'. TS2304 ...READ MORE

answered Mar 6 in Angular by Anvi
96 views
0 votes
1 answer

How to get last visited page URL in React?

To get the last visited page URL ...READ MORE

answered Mar 6 in Angular by Anvi
56 views
0 votes
1 answer

How to pass data from a child component to a parent component in Angular 4?

In Angular 4, passing data from a ...READ MORE

answered Dec 4, 2024 in Angular by Navya
170 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP