Angular Material表格根据点击下一页结果更改数据。

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

Angular material table to change data based on click of next results

问题

我正在使用Angular Material表格,分页的要求是,初始时只显示5条记录,当点击下一页按钮时,需要显示剩下的5条记录,当点击前5个结果时,需要返回到0到4索引值。

示例

TS:

  1. pageChange() {
  2. this.dataElement.slice(0, 5);
  3. }

HTML:

  1. <mat-table [dataSource]="dataSource" class="mat-elevation-z8">
  2. <!-- 位置列 -->
  3. <ng-container matColumnDef="position">
  4. <mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
  5. <mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
  6. </ng-container>
  7. <!-- 姓名列 -->
  8. <ng-container matColumnDef="name">
  9. <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
  10. <mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
  11. </ng-container>
  12. <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
  13. <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
  14. </mat-table>
  15. <h2 class="pagination-style" (click)="pageChange()">Next 5 results</h2>
英文:

i am using angular material table, and here pagination requirement is that, initial need to show only 5 records and when click of next button, need to show rest 5 records, when click of previous 5 results, it has to come back to 0to 4 index values.

Demo

TS:

  1. pageChange() {
  2. this.dataElement.slice(0, 5);
  3. }

HTML:

  1. &lt;mat-table [dataSource]=&quot;dataSource&quot; class=&quot;mat-elevation-z8&quot;&gt;
  2. &lt;!-- Position Column --&gt;
  3. &lt;ng-container matColumnDef=&quot;position&quot;&gt;
  4. &lt;mat-header-cell *matHeaderCellDef&gt; No. &lt;/mat-header-cell&gt;
  5. &lt;mat-cell *matCellDef=&quot;let element&quot;&gt; {{element.position}} &lt;/mat-cell&gt;
  6. &lt;/ng-container&gt;
  7. &lt;!-- Name Column --&gt;
  8. &lt;ng-container matColumnDef=&quot;name&quot;&gt;
  9. &lt;mat-header-cell *matHeaderCellDef&gt; Name &lt;/mat-header-cell&gt;
  10. &lt;mat-cell *matCellDef=&quot;let element&quot;&gt; {{element.name}} &lt;/mat-cell&gt;
  11. &lt;/ng-container&gt;
  12. &lt;mat-header-row *matHeaderRowDef=&quot;displayedColumns&quot;&gt;&lt;/mat-header-row&gt;
  13. &lt;mat-row *matRowDef=&quot;let row; columns: displayedColumns;&quot;&gt;&lt;/mat-row&gt;
  14. &lt;/mat-table&gt;
  15. &lt;h2 class=&quot;pagination-style&quot; (click)=&quot;pageChange()&quot;&gt;Next 5 results&lt;/h2&gt;

答案1

得分: 1

slice返回数组的一部分,但不会影响原始数组。splice通过删除、替换或添加值来更改原始数组,并返回受影响的值。

希望这有所帮助!

英文:

slice returns a piece of the array but it doesn't affect the original array. splice changes the original array by removing, replacing, or adding values and returns the affected values

Hope this helps !

https://stackblitz.com/edit/angular-5m6rnk-7nvbsm?file=src%2Fapp%2Ftable-flex-basic-example.html,src%2Fapp%2Ftable-flex-basic-example.ts,src%2Fapp%2Ftable-flex-basic-example.css

答案2

得分: 0

你可以使用<mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>

https://stackblitz.com/angular/dnbermjydavk?file=app%2Ftable-overview-example.html

英文:

You can use <mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>

https://stackblitz.com/angular/dnbermjydavk?file=app%2Ftable-overview-example.html

答案3

得分: 0

使用 slice pipe

  1. <table mat-table [dataSource]="dataSource | slice: 0:(page + 1) * pageSize">
  2. ..
  3. </table>

其中 page 的值从 0 到页面数,而 pageSize 是每次要显示的元素数量(在您的情况下为 5)。

英文:

Use slice pipe

  1. &lt;table mat-table [dataSource]=&quot;dataSource |slice:0:(page+1)*pageSize&quot;&gt;
  2. ..
  3. &lt;/table&gt;

where page goes from 0 to the number of pages and pageSize is how many element you want to show each time (in your case 5)

huangapple
  • 本文由 发表于 2023年4月4日 18:12:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75928143.html
匿名

发表评论

匿名网友

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

确定