Link to home
Start Free TrialLog in
Avatar of Ruchir Naphade
Ruchir NaphadeFlag for Germany

asked on

Angular Material Table

I want to use new angulat material table to show my produkt data. I was unsing old table perfectly but can not adopt to new angular material table.

I am getting my data in this.produkts array which i want to render in new angular material table. I used to render that in old table.

Can someone please tell me how to render my data in new angular table?

Component.ts

getPage(page: number) {
    if (this.produktCache.has(page)) {
      this.produkts = this.produktCache.get(page);
      this.currentPage = page;
    } else {
      this.produktService.getProduktSearch(page.toString(), this.produktsPerPage.toString(), this.filiale, this.artnr, this.produktbez,
        this.werbetext, this.mgeht, this.lfname, this.hinweise, this.bildnummer)
        .then(
          data => {
            this.produkts = data;
            console.log('array', this.produkts);
            this.currentPage = page;
            this.produktCache.set(this.currentPage, this.produkts);
          }).catch(
            (error: any) => {
              console.log('Produkt not found!');
            });
    }
  }

Open in new window


pervious table:

<mat-card>
  <table class="product-list" *ngIf="produkts">
    <thead>
      <tr>
        <th>{{ 'Artikle Nummer' }}</th>
        <th>{{ 'Filiale' }}</th>
        <th>{{ 'Bezeichnung' }}</th>
        <th>{{ 'Mehr Info' }} </th>
      </tr>
    </thead>
    <tbody>
      <tr [routerLink]="['/rullko/produkt/', product.Bildnummer]" *ngFor="let product of produkts | paginate: { id: 'server', itemsPerPage: produktsPerPage, currentPage: currentPage }">
        <td> {{ product.ARTNR }} </td>
        <td> {{ product.FILIALE }}</td>
        <td> {{ product.PRODUKTBEZ }} </td>
        <td>
          <button mat-icon-button color="primary" [routerLink]="['/rullko/produkt/', product.Bildnummer]">
            <mat-icon>more_vert</mat-icon>
          </button>
        </td>
      </tr>
    </tbody>
  </table>
  <app-pagination (pageChange)="getPage($event)"></app-pagination>
  <mat-card-actions align="end">
    <button mat-raised-button color="warn" [routerLink]="['/rullko/produkt']">
      <mat-icon>clear</mat-icon>CANCEL</button>
  </mat-card-actions>
</mat-card>

Open in new window


New table:
<table mat-table [dataSource]="this.produkts" class="mat-elevation-z8">
  <ng-container matColumnDef="artikle">
    <th mat-header-cell *matHeaderCellDef> ARTIKLE NO. </th>
    <td mat-cell *matCellDef="let user"> {{ this.produkts.ARTNR }}</td>
  </ng-container>
  <ng-container matColumnDef="filiale">
    <th mat-header-cell *matHeaderCellDef> FILIALE </th>
    <td mat-cell *matCellDef="let user"> </td>
  </ng-container>
  <ng-container matColumnDef="bezeichnung">
    <th mat-header-cell *matHeaderCellDef> BEZEICHNUNG </th>
    <td mat-cell *matCellDef="let user"> </td>
  </ng-container>
  <ng-container matColumnDef="details">
    <th mat-header-cell *matHeaderCellDef> DETAILS </th>
    <td mat-cell *matCellDef="let user"> </td>
  </ng-container>
  <tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
  <tr mat-row *matRowDef="let myRowData; columns: columnsToDisplay"></tr>
</table>

Open in new window

Avatar of Swatantra Bhargava
Swatantra Bhargava
Flag of India image

Please try below code and see the result.

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

  <!-- Position Column -->
  <ng-container matColumnDef="position">
    <th mat-header-cell *matHeaderCellDef> ARTIKLE NO. </th>
    <td mat-cell *matCellDef="let element"> {{produkts.ARTNR}} </td>
  </ng-container>

  <!-- FILIALE Column -->
  <ng-container matColumnDef="filiale">
    <th mat-header-cell *matHeaderCellDef> FILIALE </th>
    <td mat-cell *matCellDef="let element"> {{produkts.FILIALE}} </td>
  </ng-container>

  <!-- BEZEICHNUNG Column -->
  <ng-container matColumnDef="bezeichnung">
    <th mat-header-cell *matHeaderCellDef> BEZEICHNUNG </th>
    <td mat-cell *matCellDef="let element"> {{produkts.BEZEICHNUNG}} </td>
  </ng-container>

  <!-- Details Column -->
  <ng-container matColumnDef="details">
    <th mat-header-cell *matHeaderCellDef> DETAILS </th>
    <td mat-cell *matCellDef="let element"> {{produkts.DETAILS}} </td>
  </ng-container>

  <!-- Sticky Header -->
	<tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
	<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

Open in new window

Avatar of Ruchir Naphade

ASKER

Nopes Sir...it is not working...Can not get anything in my table...
Check your [dataSource] data is there or not? Is there any testing URL so that I can see?
I am getting my data in datasource. When i use normal HTML table, i can see that data but When i use angular table, i am not getting anything...Nopes Sir...there is no testing URL...
It's in which format JSON, XML? What's your datasource name?
Without any code, I am unable to suggest to you the changes. I gave you an example but that is not working as said by you. Please read below link to understand the Material Data Table.

https://blog.angular-university.io/angular-material-data-table/
Sir...my problem is I can use angular material table in other things but it is not working for me for this perticular array..i have been figuring out baout this since yesterday but no luck..so i went bak to normal one..

Thanks for the link...i will go through that...
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.