【问题标题】:angular 7: Server Side Pagination Using Angular Material角度 7:使用 Angular 材质的服务器端分页
【发布时间】:2019-08-19 08:42:00
【问题描述】:

我使用 Angular 7 和 Angular Material 作为服务器端分页。这件事我有问题。问题是分页变成了这样。

我想把分页改成这样

如何改变它。示例代码如下:-

html

<div class="example-container mat-elevation-z8">
  <div class="example-loading-shade"
       *ngIf="isLoadingResults || isRateLimitReached">
    <mat-spinner *ngIf="isLoadingResults"></mat-spinner>
    <div class="example-rate-limit-reached" *ngIf="isRateLimitReached">
      GitHub's API rate limit has been reached. It will be reset in one minute.
    </div>
  </div>

  <div class="example-table-container">

    <table mat-table [dataSource]="data" class="example-table"
           matSort matSortActive="created" matSortDisableClear matSortDirection="desc">
      <!-- Number Column -->
      <ng-container matColumnDef="number">
        <th mat-header-cell *matHeaderCellDef>#</th>
        <td mat-cell *matCellDef="let row">{{row.number}}</td>
      </ng-container>

      <!-- Title Column -->
      <ng-container matColumnDef="title">
        <th mat-header-cell *matHeaderCellDef>Title</th>
        <td mat-cell *matCellDef="let row">{{row.title}}</td>
      </ng-container>

      <!-- State Column -->
      <ng-container matColumnDef="state">
        <th mat-header-cell *matHeaderCellDef>State</th>
        <td mat-cell *matCellDef="let row">{{row.state}}</td>
      </ng-container>

      <!-- Created Column -->
      <ng-container matColumnDef="created">
        <th mat-header-cell *matHeaderCellDef mat-sort-header disableClear>
          Created
        </th>
        <td mat-cell *matCellDef="let row">{{row.created_at | date}}</td>
      </ng-container>

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

  <mat-paginator [length]="resultsLength" [pageSize]="30"></mat-paginator>
</div>

组件 ts

export class TableHttpExample implements AfterViewInit {
  displayedColumns: string[] = ['created', 'state', 'number', 'title'];
  exampleDatabase: ExampleHttpDatabase | null;
  // data: GithubIssue[] = [];

  resultsLength = 0;
  isLoadingResults = true;
  isRateLimitReached = false;

  @ViewChild(MatPaginator, {static: false}) paginator: MatPaginator;
  @ViewChild(MatSort, {static: false}) sort: MatSort;

  constructor(private _httpClient: HttpClient) {}

  ngAfterViewInit() {
    this.exampleDatabase = new ExampleHttpDatabase(this._httpClient);

    // If the user changes the sort order, reset back to the first page.
    this.sort.sortChange.subscribe(() => this.paginator.pageIndex = 0);

    merge(this.sort.sortChange, this.paginator.page)
      .pipe(
        startWith({}),
        switchMap(() => {
          this.isLoadingResults = true;
          return this.exampleDatabase!.getRepoIssues(
            this.sort.active, this.sort.direction, this.paginator.pageIndex);
        }),
        map(data => {
          // Flip flag to show that loading has finished.
          this.isLoadingResults = false;
          this.isRateLimitReached = false;
          this.resultsLength = data.total_count;

          return data.items;
        }),
        catchError(() => {
          this.isLoadingResults = false;
          // Catch if the GitHub API has reached its rate limit. Return empty data.
          this.isRateLimitReached = true;
          return observableOf([]);
        })
      ).subscribe(data => this.data = data);
  }
}

服务 ts

export interface GithubApi {
  items: GithubIssue[];
  total_count: number;
}

export interface GithubIssue {
  created_at: string;
  number: string;
  state: string;
  title: string;
}
export class ExampleHttpDatabase {
data: GithubIssue[] = [];



/** An example database that the data source uses to retrieve data for the table. */

  constructor(private _httpClient: HttpClient) {}

  getRepoIssues(sort: string, order: string, page: number): Observable<GithubApi> {
    const href = 'https://api.github.com/search/issues';
    const requestUrl =
        `${href}?q=repo:angular/components&sort=${sort}&order=${order}&page=${page + 1}`;

    return this._httpClient.get<GithubApi>(requestUrl);
  }
}

我尝试更改它,但它不起作用。我不知道如何改变它。希望大家帮忙解决。

这是我的示例demo 代码。

提前致谢。

【问题讨论】:

    标签: angular angular-ui-router angular-material


    【解决方案1】:

    这是我的解决方案https://stackblitz.com/edit/angular-material-pagination-server-side-pagination-awof75

    主要思想是您必须构建自己的分页器,而不是使用 Material 之一。这很简单,只需添加“固定”按钮,如“First”、“Prev”、“Next”、“Last”和数据信息(如lengthpageSize),您就可以创建页面列表。唯一不起作用的是pageSize,不知道为什么。我想您将能够在实际应用中找出原因。

    然后使用BehaviorSubject 处理页面更改。无论如何,您可以在示例中看到所有内容。

    样式,也许还有一些小事我留给你。我想你想以某种方式限制显示页面的数量,因为它们有很多等等。

    【讨论】:

    • @Pohon 用修复更新了答案中的 stackblitz 链接。它的解决方法与分页器类似,只需创建一个 BehaviorSubject 并在那里添加更改
    猜你喜欢
    • 2021-07-19
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 2023-02-15
    • 2019-07-29
    • 2017-01-12
    • 1970-01-01
    相关资源
    最近更新 更多