动态更改MatPaginatorIntl itemsPerPageLabel

问题描述 投票:0回答:1

我正在使用Angular 7.3.9,并且在两个不同的组件中有两个列表,并带有一个MatPaginator。因此,我创建了一个带有french-paginator-intl.ts文件的文件夹“ paginator”,在其中翻译了paginator并定义了标签。我希望分页者的itemsPerPageLabel在第一个列表上显示“Barèmespar page”,在第二个列表上显示“Compétitionspar page”。

我将文件做成这样:

import { MatPaginatorIntl } from '@angular/material';

const frenchRangeLabel = (page: number, pageSize: number, length: number) => {
    if (length == 0 || pageSize == 0) { return `0 sur ${length}`; }

    length = Math.max(length, 0);
    const startIndex = page * pageSize;

    // If the start index exceeds the list length, do not try and fix the end index to the end.
    const endIndex = startIndex < length ?
        Math.min(startIndex + pageSize, length) :
        startIndex + pageSize;

    return `${startIndex + 1} - ${endIndex} sur ${length}`;
}

export function getFrenchPaginatorIntl(label: string) {
    const paginatorIntl = new MatPaginatorIntl();

    paginatorIntl.itemsPerPageLabel = label + ' par page : ';
    paginatorIntl.nextPageLabel = 'Page suivante';
    paginatorIntl.previousPageLabel = 'Page précédente';
    paginatorIntl.getRangeLabel = frenchRangeLabel;

    console.log(paginatorIntl.itemsPerPageLabel);
    return paginatorIntl;
  }

我的Competition.module.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CompetitionComponent } from './competition.component';
import { RouterModule } from '@angular/router';
import { MaterialModule } from '../material/material.module';

import { CompetitionListComponent } from '../competition-list/competition-list.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import {MatExpansionModule} from '@angular/material/expansion';
import { MatSortModule, MatTableModule, MatPaginatorModule, MatPaginatorIntl, MatTooltipModule } from '@angular/material';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { getFrenchPaginatorIntl } from '../paginator/french-paginator-intl';


@NgModule({
  declarations: [CompetitionComponent],
  imports: [
    CommonModule,
    MaterialModule,
    FormsModule,
    ReactiveFormsModule,
    MatExpansionModule,
    MatSortModule,
    MatTableModule,
    MatPaginatorModule,
    MatCheckboxModule,
    MatTooltipModule,
    RouterModule.forChild([
      {path: 'contests/:id', component: CompetitionComponent},
      //{path: 'contest', redirectTo:'/contests'},
      {path:'contests', component: CompetitionListComponent}
      ])
  ],
  providers: [
    { provide: MatPaginatorIntl, useValue: getFrenchPaginatorIntl('Barèmes') }
  ]
})
export class CompetitionModule { }

我的比赛清单.module.ts与'Compétitions'相同,而不是'Barèmes'

我的Competition.component.html:

<div>
    <div class="custom-card contest">
      <div class="custom-card-title">
        <h1>{{competition.label}}</h1><a *ngIf="user.authorities.includes('ROLE_ADMIN')" [routerLink]="['/addscales']" [state]="{contestId: competition.id}"><mat-icon class="mat-icon notranslate material-icons mat-icon-no-color add-icon" role="img" aria-hidden="true">add</mat-icon>Créer un barème</a>
      </div>
      <form class="custom-card-body" #competitionForm="ngForm">
        <div class="custom-card-row">
          <p><span>Du</span> {{competition.startDate | date:'shortDate'}} à {{ competition.startDate | date:'shortTime' }} <span>au</span> {{competition.endDate | date:'shortDate'}} à {{ competition.endDate | date:'shortTime' }}</p>
        </div>
        <div class="custom-card-row">
          <p class="error">{{error}}</p>
        </div>
        <div class="custom-card-row">
          <button class="material-btn-highlight" (click)="goToContestUpdate()">Modifier</button>
          <button class="material-btn-delete" (click)="openDialog()">Supprimer</button>
          <button class="material-btn-cancel" (click)="goToList()">Retour</button>
        </div>
      </form>
    </div>
    <mat-accordion *ngIf="user.authorities.includes('ROLE_ADMIN')">
      <mat-expansion-panel>
        <mat-expansion-panel-header>
          <mat-panel-title>
            Barèmes
          </mat-panel-title>
        </mat-expansion-panel-header>

        <mat-table #table [dataSource]="dataSource" matSort>
          <ng-container matColumnDef="label">
            <mat-header-cell *matHeaderCellDef mat-sort-header>Nom</mat-header-cell>
            <mat-cell *matCellDef="let element">
              {{element['label']}}
            </mat-cell>    
          </ng-container>
          <ng-container matColumnDef="{{column.id}}" *ngFor="let column of columnNames">
            <mat-header-cell *matHeaderCellDef> {{column.value}} </mat-header-cell>
            <mat-cell *matCellDef="let element"> {{element[column.id]}}</mat-cell>
          </ng-container>
          <ng-container matColumnDef="admin">
              <mat-header-cell *matHeaderCellDef></mat-header-cell>
              <mat-cell *matCellDef="let element">
                <mat-icon matTooltip="Modifier ce barème" (click)="goToScaleUpdate(element.id); $event.stopPropagation()" class="mat-icon notranslate material-icons mat-icon-no-color" role="img" aria-hidden="true">edit</mat-icon>
                <mat-icon matTooltip="Supprimer ce barème" (click)="openDialog(element.id); $event.stopPropagation()" class="mat-icon notranslate material-icons mat-icon-no-color" role="img" aria-hidden="true">cancel</mat-icon>
                <mat-checkbox matTooltip="Activer/Désactiver ce barème" [checked]="element.active" (click)="$event.stopPropagation()"></mat-checkbox>
              </mat-cell>
          </ng-container>

          <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
          <mat-row *matRowDef="let row; columns: displayedColumns;" [ngClass]="{'activeScale': row.active}" [matTooltip]="row.active ? 'Barème actif' : null"></mat-row>
        </mat-table>
        <mat-paginator [pageSizeOptions]="[10, 25, 100]"></mat-paginator>
      </mat-expansion-panel>
    </mat-accordion>
  </div>

[当我运行我的应用程序时,分页器在两个列表上显示'Barèmes'。console.log(paginatorIntl.itemsPerPageLabel)给了我这个:

Barèmes par page : 
Compétitions par page : 

有没有简单的方法可以实现这一目标?

angular angular7 mat-table paginator
1个回答
1
投票

您可以在此StackBlitz Link中找到完整的工作示例,>

export class AppComponent  {
   @ViewChild(MatPaginator, {static : false}) paginator: MatPaginator;
   pageEvent;

   ngAfterViewInit(){
      this.paginator._intl.itemsPerPageLabel = 'Barèmes par page ';
   }

   onPaginateChange(pageNumber){
      if (pageNumber.pageIndex === 1){
         this.paginator._intl.itemsPerPageLabel = 'Compétitions par page ';
      }
      else{
         this.paginator._intl.itemsPerPageLabel = 'rest of the page '
      }
   }
}

您的HTML文件是..

<mat-paginator [length]="100"
          [pageSize]="10"
          [pageSizeOptions]="[5, 10, 25, 100]"
          (page)="pageEvent = $event; onPaginateChange($event)">
</mat-paginator>
© www.soinside.com 2019 - 2024. All rights reserved.