簡體   English   中英

訂閱 Angular 材料中 matTable 的 PageSize 和 PageIndex

[英]subscribe on PageSize and PageIndex of matTable in Angular material

我正在使用 angular 材料表和分頁器。 我想將 pageSize 和 pageIndex 發送到 BE,這樣我就可以從 BE 進行分頁,因為我們有大量數據,而這在 FE 中是不可能的。 如何訂閱 pageSize 更改和 PageIndex 更改並獲取用戶選擇的數字以發送給 BE。

您可以按如下方式訂閱尋呼機:

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  ],
})
export class ListComponent implements AfterViewInit {
  @ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
  data: MatTableDataSource<any>;

  constructor(private http: HttpClient) {}

  ngAfterViewInit(): void {
    this.data = new MatTableDataSource([]);
    this.data.paginator = this.paginator;
    this.paginator.pageIndex = 0;

    this.paginator.page.pipe(
      startWith({}),
      switchMap(() => {
        const page = this.paginator.pageIndex + 1;
        const itemsPerPage = this.paginator.pageSize;

        return this.http.get(`api_url?page=${page}&itemsPerPage=${itemsPerPage}`);
      }),
      map((apiResponseData) => { return apiResponseData;}),
    ).subscribe((data) => {
      this.data= new MatTableDataSource(data);
      this.data._updateChangeSubscription();
    });
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM