如何将可滚动的Angular Material表格与分页器结合使用

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

How to combine scrollable Angular Material table with paginator

问题

在我们的项目中,我们希望使用Angular Material表格与分页组件。如果我们遵循示例中使用的布局,一切都运行正常。最接近我们所需布局的示例是这个:https://material.angular.io/components/table/examples#table-http

然而,与示例相比,在我们的布局中,我们不知道父组件的高度。我们无法将min-heightmax-height设置为固定数值。虽然分页器应该使用它需要的任何高度(-> auto),但表格应该根据需要增长/缩小,并通过提供滚动条来处理溢出。

解决方案可能接近以下内容:

<div style="height: 100%; display: flex; flex-direction: column">
  <table mat-table style="flex: 1 1 0px"> ... </table>
  <mat-paginator style="flex: none"> ... </mat-paginator>
</div>

虽然如果表格确实需要所有可用空间,这个方法可以正常工作,但如果表格中没有要显示的行,它也会占用可用高度。我不知道分页器的高度,因为它取决于引入了Angular 15的密度设置。因此,我无法将我的表格的max-height设置为类似calc(100% - $paginatorHeight)的东西。是否有一个flexbox属性可以告诉我的表格只增长到它的内容的高度?或者有适合我的用例的其他样式选项吗?

英文:

In our project we want to use the Angular Material table with the paginator component.
Everything works fine if we stick to the layouts which are used in the examples. The example closest to our desired layout is this one: https://material.angular.io/components/table/examples#table-http

However, compared to the example, in our layout we don't know the height of our parent component. We cannot set min-height and max-height to a fixed number. While the paginator should use whatever height it needs (-> auto), the table should grow/shrink and handle the overflow by providing a scrollbar.

The solution is probably close to the following:

&lt;div style=&quot;height: 100%; display: flex; flex-direction: column&quot;&gt;
  &lt;table mat-table style=&quot;flex: 1 1 0px&quot;&gt; ... &lt;/table&gt;
  &lt;mat-paginator style=&quot;flex: none&quot;&gt; ... &lt;/mat-paginator&gt;
&lt;/div&gt;

While this works fine if the table really needs all available space, it also consumes the available height if there are no rows to display in the table.
I don't know the height of the paginator as it depends upon the density setting introduced with Angular 15. Therefore, I cannot set max-height of my table to something like calc(100% - $paginatorHeight).
Is there a flexbox property which tells my table to only grow up to its content? Or is there any other styling option suitable for my use case?

答案1

得分: 1

我通过将分页器移到表格的页脚来解决了这个问题。

<ng-container matColumnDef="paginator">
    <td mat-footer-cell *matFooterCellDef [attr.colspan]="100">
        <mat-paginator #paginator [length]="totalCount" [pageIndex]="pageIndex" [pageSize]="pageSize" [pageSizeOptions]="[10, 20, 50]"
            (page)="onPageChange($event)">
        </mat-paginator>
    </td>
</ng-container>

<tr mat-footer-row *matFooterRowDef="['paginator']; sticky: true"></tr>

这会使分页器在滚动时保持在顶部,并且您可以将表格的高度设置为100%。

英文:

I solved this issue by moving the paginator to the footer of the table.

   &lt;ng-container matColumnDef=&quot;paginator&quot;&gt;
        &lt;td mat-footer-cell *matFooterCellDef [attr.colspan]=&quot;100&quot;&gt;
          &lt;mat-paginator #paginator [length]=&quot;totalCount [pageIndex]=&quot;pageIndex&quot; [pageSize]=&quot;pageSize&quot; [pageSizeOptions]=&quot;[10, 20, 50]&quot;
                         (page)=&quot;onPageChange($event)&quot;&gt;
          &lt;/mat-paginator&gt;
        &lt;/td&gt;
   &lt;/ng-container&gt;

   &lt;tr mat-footer-row *matFooterRowDef=&quot;[&#39;paginator&#39;]; sticky: true&quot;&gt;&lt;/tr&gt;

This makes the paginator sticky when scrolling and you can set the height of table to 100%

huangapple
  • 本文由 发表于 2023年3月7日 00:55:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653653.html
匿名

发表评论

匿名网友

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

确定