简体   繁体   English

如何修复 mat-paginator 大小

[英]How to fix mat-paginator size

In mat-table for pagination, there is no flexibility for sticky-table .在用于分页mat-table中, sticky-table没有灵活性。 In pagination after move there is no css is apply (as shown in images).在移动后的分页中,没有应用css (如图所示)。 I tried to give a background color but it is not working.我试图给背景颜色,但它不起作用。 refer this code: https://stackblitz.com/angular/bbmgqanjelq?file=app%2Ftable-sticky-columns-example.css参考此代码: https://stackblitz.com/angular/bbmgqanjelq?file=app%2Ftable-sticky-columns-example.css

在此处输入图像描述

![在此处输入图像描述

.mat-paginator{
    background-color: brown;
    color: gold;
    font-size: 1rem;
}

.mat-header-cell{
    font-size: 1rem;
    background-color: brown;
    color: gold;
  }
.example-container {
  height: 460px;
  width: 550px;
  overflow: auto;
}
table {
  width: 800px;
}
<div class="example-container mat-elevation-z8">
  <table mat-table [dataSource]="dataSource">

    <!-- Name Column -->
    <ng-container matColumnDef="name" sticky>
      <th mat-header-cell *matHeaderCellDef> Name </th>
      <td mat-cell *matCellDef="let element"> {{element.name}} </td>
    </ng-container>

    <!-- Position Column -->
    <ng-container matColumnDef="position">
      <th mat-header-cell *matHeaderCellDef> No. </th>
      <td mat-cell *matCellDef="let element"> {{element.position}} </td>
    </ng-container>

    <!-- Weight Column -->
    <ng-container matColumnDef="weight">
      <th mat-header-cell *matHeaderCellDef> Weight </th>
      <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
    </ng-container>

<!-- Star Column -->
    <ng-container matColumnDef="star" stickyEnd>
      <th mat-header-cell *matHeaderCellDef></th>
      <td mat-cell *matCellDef="let element">
        <mat-icon>more_vert</mat-icon>
      </td>
    </ng-container>

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

<mat-paginator [length]="50" [pageSizeOptions]="[10, 20, 30]" showFirstLastButtons>
</mat-paginator>
</div>

if we move the table paginator should look same as before.如果我们移动表格分页器应该看起来和以前一样。

Okay I'm adding another answer because this one is totally different from the previous one.好的,我正在添加另一个答案,因为这个答案与前一个完全不同。

You can use the Angular renderer along with view children to incread the size of the paginator to the size of the table.您可以使用 Angular 渲染器和视图子级来将分页器的大小增加到表格的大小。

Here is the stackblitz demo: https://stackblitz.com/edit/angular-mxdsdk-d2b7xa?file=app/table-sticky-columns-example.ts这是 stackblitz 演示: https://stackblitz.com/edit/angular-mxdsdk-d2b7xa?file=app/table-sticky-columns-example.ts

As you can see, the paginator gets the total size of the table and is displayed on the right.如您所见,分页器获取表格的总大小并显示在右侧。 Not sure you want it, but it gives you a new way of using it.不确定您是否想要它,但它为您提供了一种新的使用方式。

 <table mat-table [dataSource]="dataSource" #table>
 ...
 <mat-paginator sticky #paginator></mat-paginator>
@ViewChild('table', { static: true, read: ElementRef }) table: ElementRef<HTMLDivElement>;
@ViewChild('paginator', { static: true, read: ElementRef }) paginator: ElementRef<HTMLDivElement>;

constructor(
  private renderer: Renderer2
) {}

ngOnInit() {
  const width = this.table.nativeElement.getBoundingClientRect().width;
  this.renderer.setStyle(this.paginator.nativeElement, 'width', width + 'px');
}

在此处输入图像描述

I was facing the same issue, but got fixed.我面临同样的问题,但得到了解决。

Place the mat-paginator outside the container div of, then this pagination width issue won't even arise at first.将 mat-paginator 放在容器 div 之外,那么这个分页宽度问题甚至不会一开始就出现。 The scroll will apply only on table data itself, not on pagination.滚动仅适用于表格数据本身,而不适用于分页。

Maybe you need to use ::ng-deep property in your scss or css like this:也许您需要在scsscss中使用::ng-deep属性,如下所示:

::ng-deep .mat-paginator{
    background-color: brown;
    color: gold;
    font-size: 1rem;
}

::ng-deep .mat-header-cell{
    font-size: 1rem;
    background-color: brown;
    color: gold;
}

For more information about ng-deep, check this link .有关 ng-deep 的更多信息,请查看此链接

I don't think you can do anything at this point.我不认为你现在可以做任何事情。

The sticky is very tricky to use, that's why you should avoid using it (also it's not that well supported ).粘性使用起来非常棘手,这就是为什么你应该避免使用它(而且它的支持也不是很好)。

You could add background: brown;您可以添加background: brown; to your .example-container , which would result in this stackblitz: https://stackblitz.com/edit/angular-mxdsdk?file=app/table-sticky-columns-example.css到您的.example-container ,这将导致此堆栈闪电战: https://stackblitz.com/edit/angular-mxdsdk?file=app/table-sticky-columns-example.css

在此处输入图像描述

Maybe you could explain your end-goal, and we could come up with an alternative?也许你可以解释你的最终目标,我们可以想出一个替代方案?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM