【问题标题】:Angular material pagination with different table library具有不同表库的角度材料分页
【发布时间】:2022-02-05 22:32:04
【问题描述】:

所以,我为我的表使用了这个不同的表库并创建了一个自定义分页器。我想在表格中使用这个 paginator-goto。我如何使用分页器来工作表。 该表是一个自定义表库,因此我不确定如何将数据源与分页一起使用。当我单击下一个或上一个时,我只需要分页来响应。 这是分页演示。 https://stackblitz.com/edit/mat-paginator-with-goto-5fxwvm?file=src/app/mat-paginator-goto/mat-paginator-goto.component.html

【问题讨论】:

  • 一般你可以使用slice pipe这样SO如果你有一个自定义分页器检查变量的名称
  • 如果您使用模板引用变量(链接中的#paginator),您可以访问MatPaginatorGotoComponent 的所有变量和函数。所以你可以使用<tru-core-table ... [rows]="tableExampleRows|slice:paginator.pageIndex*paginator.pageSize):((paginator.pageIndex+1)*paginator.pageSize)" >
  • 查看带有应用程序表的stackblitz(我找不到您的tru-core-table 组件。注意:小心,我在之前的评论中忘记了一个)跨度>
  • 我写了一个关于如何更改“”的答案。好吧,在示例中,我使用了 customPaginatorIntl。真的只需要更改 .css 即可获得它 - 而不是用作内容attr(aria-label),您可以“硬编码”内容

标签: html angular angular-material pagination


【解决方案1】:

要更改的“标签”,docs(参见国际化示例)指示如何更改“工具提示”

添加一个 MyCustomPaginatorIntl

export class MyCustomPaginatorIntl implements MatPaginatorIntl {
  changes = new Subject<void>();

  firstPageLabel = "First page"
  itemsPerPageLabel = "Items per page:"
  lastPageLabel = "Last page:"

  nextPageLabel = "Next";
  previousPageLabel = "Previous";
  getRangeLabel(page: number, pageSize: number, length: number): string {
    if (length === 0) {
      return "Page 1 of 1";
    }
    const amountPages = Math.ceil(length / pageSize);
    return `Page ${page + 1} of ${amountPages}`;
  }
}

并用作提供者 - 您可以在 app.module 或您的组件中执行此操作

providers:[{provide:MatPaginatorIntl,useClass:MyCustomPaginatorIntl}],

但是如果我们想改变“”,我们需要使用类

我们可以在 style.css 中编写 - 如果我们不为所有应用程序使用样式,则无法正常工作

.mat-paginator-navigation-next .mat-button-wrapper,
.mat-paginator-navigation-previous .mat-button-wrapper
{
  color:transparent!important;
}
.mat-paginator-navigation-previous
{
  width:5em!important;;
}
.mat-paginator-navigation-next::after{
  content:attr(aria-label);
  margin-left:-2em;
}
.mat-paginator-navigation-previous::after{
  content:attr(aria-label);
  margin-left:-2em;
}

stackblitz 中是一个使用 ashraful Islam 的自定义分页器的示例

更新当我们更改数据源时,我们需要“手动更新”分页器。好吧,它与另一个没有什么不同。我们可以使用 ViewChild 来获取 MatPaginatorGotoComponent

  @ViewChild(MatPaginatorGotoComponent) paginator

而且,当我们更改我们可以使用的数据时,例如

this.paginator.pageIndex=0;
this.paginator.goTo=1

或者

if (this.paginator.pageIndex*this.paginator.pageSize>this.data.length)
{
    const pageIndex=Math.ceil(this.data.length / this.paginator.pageSize)-1
    this.paginator.pageIndex=pageIndex
    this.paginator.goTo=pageIndex+1
}

【讨论】:

  • 太棒了,非常感谢@Eliseo
  • 我看到它在 stackblitz 中工作,但是当我在我的上实现时,它只更改了工具提示,仍然显示 。我用的正是这里的内容
  • 看到 .css 应该在“styles.css”(或styles.scss)中 - 您在 angular.json 中指定的文件-
  • 它的 mat-paginator-goto.component.scss 。如果我更改 style.scss,它将影响我使用 mat-paginator 的所有地方。它会破坏一些代码。所以,我只想改变这个分页器
  • 你可以在所有 .css 前面加上 mat-paginator-goto,例如mat-paginator-goto .mat-paginator-navigation-previous... 我更新了 stackblitz 看看
猜你喜欢
  • 2018-10-05
  • 1970-01-01
  • 2019-06-01
  • 1970-01-01
  • 2020-12-17
  • 2019-06-20
  • 2019-05-19
  • 2018-02-06
  • 1970-01-01
相关资源
最近更新 更多