数据未使用 [datasource] 渲染到 mat-table,尽管存在,并使用 “json” 管道渲染。

huangapple go评论63阅读模式
英文:

Data is not rendered with mat-table [datasource] although it exists and is rendered with the "json" pipe

问题

I am implementing a table with mat-table. I want to bind the data source to an array whose data is fetched by an API. When I fetch the data using httpclient and subscribe to the result, the table doesn't render anything.

However, I used the "json" pipe to render the array outside the table, and it works.

Here is my code:

import { HttpClient } from '@angular/common/http';

constructor(private http: HttpClient) {}

displayedColumns: string[] = ['firstname', 'lastname'];
users: any[] = [];

ngOnInit(): void {
  this.http.get('/api/users').subscribe(
    (data) => {
      this.users = data
    },
    (error) => {
      console.log(error)
    }
  );
}

The HTML is as follows:

<table #table mat-table [dataSource]="users" class="mat-elevation-z8">
    <ng-container matColumnDef="firstname">
        <th mat-header-cell *matHeaderCellDef>First Name</th>
        <td mat-cell *matCellDef="let element"> {{element.firstname}} </td>
    </ng-container>
    <ng-container matColumnDef="lastname">
        <th mat-header-cell *matHeaderCellDef>Last Name</th>
        <td mat-cell *matCellDef="let element"> {{element.lastname}} </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
英文:

I am implementing a table with mat-table. I want to bind the data source to an array whose data is fetched by an API. When I fetch the data using httpclient and subscribe to the result, the table doesn't render anything.

However, I used the "json" pipe to render the array outside the table, and it works.

Here is my code:

import { HttpClient } from &#39;@angular/common/http&#39;;


constructor(private http: HttpClient) {}

displayedColumns: string[] = [&#39;firstname&#39;, &#39;lastname&#39;];
users: any[] = [];

ngOnInit(): void {
  this.http.get(&#39;/api/users&#39;).subscribe(
    (data) =&gt; {
      this.users = data
    },
    (error) =&gt; {
      console.log(error)
    }
  );
}

The HTML is as follows:

&lt;table #table mat-table [dataSource]=&quot;users&quot; class=&quot;mat-elevation-z8&quot;&gt;
    &lt;ng-container matColumnDef=&quot;firstname&quot;&gt;
        &lt;th mat-header-cell *matHeaderCellDef&gt;First Name&lt;/th&gt;
        &lt;td mat-cell *matCellDef=&quot;let element&quot;&gt; {{element.firstname}} &lt;/td&gt;
    &lt;/ng-container&gt;
    &lt;ng-container matColumnDef=&quot;lastname&quot;&gt;
        &lt;th mat-header-cell *matHeaderCellDef&gt;Last Name&lt;/th&gt;
        &lt;td mat-cell *matCellDef=&quot;let element&quot;&gt; {{element.lastname}} &lt;/td&gt;
    &lt;/ng-container&gt;

    &lt;tr mat-header-row *matHeaderRowDef=&quot;displayedColumns&quot;&gt;&lt;/tr&gt;
    &lt;tr mat-row *matRowDef=&quot;let row; columns: displayedColumns;&quot;&gt;&lt;/tr&gt;
&lt;/table&gt;

答案1

得分: 1

自从表格优化了性能,它将不会自动检查数据数组的更改。相反,当数据数组中添加、移除或移动对象时,您可以通过调用其 renderRows() 方法来触发对表格已渲染行的更新。

链接:https://material.angular.io/components/table/overview

在 Angular Material 表格上调用 renderRows()

英文:

Since the table optimizes for performance, it will not automatically check for changes to the data array. Instead, when objects are added, removed, or moved on the data array, you can trigger an update to the table's rendered rows by calling its renderRows() method.

https://material.angular.io/components/table/overview

Calling renderRows() on Angular Material Table

huangapple
  • 本文由 发表于 2023年6月26日 19:57:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定