mat-table 如何将 mat-header 与 mat-cell 宽度对齐

mat-table how to align mat-header with the mat-cell width

我是 flex 的新手,我的 mat-table 做得很好。 不幸的是,我的 header 不符合我的单元格宽度。

这是我的结果图片。

如您所见,我的 header 与我的单元格不一致。

这是我的 CSS 代码,就像我说的我是新手,所以我可能漏掉了什么。

@import "./../../styles.scss";


.mat-header-row {
    width: 100%;
  min-height: 20px;
  padding: 0px 0px 0px 4px;
}

.mat-row {
    width: 100%;
  min-height: 12px;
  display: inline-flex;
 // min-width: 100%;
}

.mat-header-cell {
  justify-content: left;
  font-size: smaller;
  font-weight: bold;
  padding: 0px 0px 0px 4px;
}

.mat-cell {
  justify-content: left;
  border-top: 1px solid crimson;
  font-size: smaller;
  padding: 0px 0px 0px 4px;
  text-align: left;
  align-items: flex-start;
}

.mat-cell-points {
  flex: 0 0 20px !important;
  white-space: unset !important;
}

.mat-cell-concession{
    flex: 0 0 200px !important;
    white-space: unset !important;
  justify-content: left;
  border-top: 1px solid crimson;
  font-size: smaller;
  padding: 0px 0px 0px 4px;
  text-align: left;
}

.rcorner1 {
  border-radius: 25px;
  background: mat-color($accent, 300);
  padding: 20px;
}

.example-container {
  display: flex;
  flex-direction: column;

  // min-width: 200px;
  max-width: 380px;
}


label {
  min-width: 15%;
}

.mat-row:nth-child(1n + 1) {
    background-color: mat-color($accent, lighter);
  }

  .mat-row:not(:nth-child(2n + 1)) {
    background-color: mat-color($primary, 300);
  }

.example-element-row:hover {
  background: mat-color($accent, 800);
}
.example-element-row:active {
  background: #efefef;
}

还有我的 HTML 代码:

<div class="example-container mat-elevation-z8 alternate-theme mat-app-background" color="primary">
    <div class="mat-header-cell">
        {{ this.getTypeInfo(this.typeList.TypeBourse) }}
    </div>
    <mat-table [dataSource]="bourseDetail" class="mat-elevation-z8">
        <!--  -->
        <!-- Position Column -->
        <ng-container matColumnDef="Position">
            <mat-header-cell *matHeaderCellDef matTooltip="Position"> # </mat-header-cell>
            <mat-cell *matCellDef="let element">{{ element.Position }}</mat-cell>
        </ng-container>

        <!-- Concession Column -->
        <ng-container matColumnDef="nom">
            <mat-header-cell *matHeaderCellDef>Concession</mat-header-cell>
            <mat-cell class="mat-cell-concession" *matCellDef="let element">
                <ng-container *ngIf="element.Concession.Nom; else elseBlock">
                    {{ element.Concession.Nom }}
                    <ng-template #elseBlock>Content to render when condition is false.</ng-template>
                </ng-container>
            </mat-cell>
        </ng-container>

        <!-- Points Column -->
        <ng-container matColumnDef="Points">
            <mat-header-cell *matHeaderCellDef>points</mat-header-cell>
            <mat-cell *matCellDef="let element">{{ element.Points }}</mat-cell>

        </ng-container>

        <!-- Amount Column -->
        <ng-container matColumnDef="Montant">
            <mat-header-cell *matHeaderCellDef>Gain</mat-header-cell>
            <mat-cell *matCellDef="let element">{{ element.Montant | currency }}</mat-cell>

        </ng-container>

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

感谢您的帮助

这是我现在可以使用的代码:

<div class="example-container mat-elevation-z8 alternate-theme mat-app-background" color="primary"
style="width: 450px;">
<div class="div-title">
    {{ this.annee }} - {{ this.getTypeInfo(this.typeList.TypeBourse) }}
</div>
<mat-table [dataSource]="bourseDetail" class="mat-elevation-z8">
    <!--  -->
    <!-- Position Column -->
    <ng-container matColumnDef="Position">
        <mat-header-cell class="mat-cell" *matHeaderCellDef matTooltip="Position"> Pos </mat-header-cell>
        <mat-cell class="mat-cell-column" *matCellDef="let element">{{ element.Position }}</mat-cell>
    </ng-container>

    <!-- Concession Column -->
    <ng-container matColumnDef="nom">
        <mat-header-cell class="mat-cell-concession" *matHeaderCellDef>Concession</mat-header-cell>
        <mat-cell class="mat-cell-concession mat-cell-column" *matCellDef="let element">
            <ng-container *ngIf="element.Concession.Nom">
                {{ element.Concession.Nom }}
                                                                                                
            </ng-container>
            <ng-container *ngIf="!element.Concession.Nom">
                Info manquante
            </ng-container>
        </mat-cell>
    </ng-container>

    <!-- Points Column -->
    <ng-container matColumnDef="Points">
        <mat-header-cell class="mat-cell" *matHeaderCellDef>points</mat-header-cell>
        <mat-cell class="mat-cell-column" *matCellDef="let element">{{ element.Points }}</mat-cell>

    </ng-container>

    <!-- Amount Column -->
    <ng-container matColumnDef="Montant">
        <mat-header-cell class="mat-cell" *matHeaderCellDef>Gain</mat-header-cell>
        <mat-cell class="mat-cell-column" *matCellDef="let element">{{ element.Montant | currency }}</mat-cell>

    </ng-container>

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

和 CSS 文件: @import "./../../styles.scss";

    .mat-table {
      width: 100%;
    }

    .mat-row {
        width: 100%;
        min-height: 12px;
    }

    .div-title {
        text-align: center;
        font-size: medium;
        font-weight: bold;
        padding: 4px;
      }
      
    .mat-header-cell {
        min-height: 12px;
        font-size: smaller;
        font-weight: bold;
    }

    .mat-header-row {
      width: 100%;
      min-height: 10px;
      padding: 0px 0px 0px 4px;
    }
     
    .mat-cell-column {
        border-top: 1px solid crimson;
    }

    .mat-cell {
        flex: 0 0 50px !important;
        white-space: unset !important;
        justify-content: left;
        font-size: smaller;
        padding: 0px 0px 0px 4px;
        text-align: left;
    }

    .mat-cell-concession {
        flex: 0 0 250px !important;
        min-width: 250px !important;
        white-space: unset !important;
        justify-content: left;
        font-size: smaller;
        padding: 0px 0px 0px 4px;
        text-align: left;
        color: black;
    }

    .mat-row:nth-child(1n + 1) {
        background-color: mat-color($accent, lighter);
      }
      
      .mat-row:not(:nth-child(2n + 1)) {
        background-color: mat-color($primary, 300);
      }

    .example-element-row:hover {
      background: mat-color($accent, 800);
    }
    .example-element-row:active {
      background: #efefef;
    }