Mat-tab中的组件:如何滚动内部组件

时间:2019-06-02 22:25:41

标签: html css angular scroll angular-material

我正在做一个小型的出勤项目,并且在mat-tab组件中有一些表格组件。当表中有溢出时,它将滚动整个组件,而我只希望它在内部组件上滚动表。

我尝试在以下部分中添加“溢出:自动”:

  1. (应用程序出勤表)选择器上
  2. 表格内部
  3. :: ng-deep {.mat-tab-body {溢出:自动}}
  4. 在(mat-tab-group)选择器上
  5. 将(app-appendance-table)包装在(ng-container)或(div)中,并添加溢出:自动

这是带有标签的外部组件:

  <ng-container>
    <mat-form-field class="date">
      <input
        matInput
        [matDatepicker]="picker"
        placeholder="Select a Date"
        (dateInput)="addEvent($event)"
        [(ngModel)]="currentDate"
      />
      <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
      <mat-datepicker #picker></mat-datepicker>
    </mat-form-field>
  </ng-container>
  <mat-tab-group>
    <ng-container *ngFor="let grade of result; let i = index">
      <mat-tab *ngIf="grade.length > 0" [label]="grades[i]">
        <app-attendance-table [dataSource]="grade"></app-attendance-table>
      </mat-tab>
    </ng-container>
  </mat-tab-group>
  <div *ngIf="this.result.length < 1 && !this.loading">
    No Records Found for The Date: {{ currentDate.toDateString() }}
  </div>
  <mat-spinner *ngIf="this.loading"> </mat-spinner>
</div>

这是实际的表组件本身:

  <mat-table #table [dataSource]="dataSource">


    <ng-container matColumnDef="name">
      <mat-header-cell *matHeaderCellDef> Student Name </mat-header-cell>
      <mat-cell *matCellDef="let student"
        ><span
          [ngClass]="{
            absent: student.isAbsent() && !student.Reason,
            finished: student.isAbsent() && student.Reason && !student.editing
          }"
        >
          {{ student.Name }}
        </span>
      </mat-cell>
    </ng-container>

    <ng-container matColumnDef="grade">
      <mat-header-cell *matHeaderCellDef> Student Grade </mat-header-cell>
      <mat-cell
        [ngClass]="{
          absent: student.isAbsent() && !student.Reason,
          finished: student.isAbsent() && student.Reason && !student.editing
        }"
        *matCellDef="let student"
      >
        {{ student.Grade }}
      </mat-cell>
    </ng-container>

    <ng-container matColumnDef="status">
      <mat-header-cell *matHeaderCellDef> Status </mat-header-cell>
      <mat-cell
        [ngClass]="{
          absent: student.isAbsent() && !student.Reason,
          finished: student.isAbsent() && student.Reason && !student.editing
        }"
        *matCellDef="let student"
      >
        {{ student.Status }}
      </mat-cell>
    </ng-container>


    <ng-container matColumnDef="reason">
      <mat-header-cell *matHeaderCellDef> Reason </mat-header-cell>
      <mat-cell *matCellDef="let student">
        <mat-form-field
          class="reasons"
          *ngIf="!student.isPresent()"
          appearance="outline"
        >
          <mat-select
            [(ngModel)]="student.Reason"
            [disabled]="!student.editing"
            placeholder="Select Reason"
            (selectionChange)="makeChange(student)"
          >
            <mat-option
              *ngFor="let reason of reasons; let i = index"
              [value]="reason"
              [disabled]="student.Reason === reason"
            >
              {{ reason }}
            </mat-option>
          </mat-select>
        </mat-form-field>
      </mat-cell>
    </ng-container>

    <ng-container matColumnDef="comments">
      <mat-header-cell *matHeaderCellDef> Comments </mat-header-cell>
      <mat-cell *matCellDef="let student">
        <mat-form-field *ngIf="!student.isPresent()">
          <input
            matInput
            [(ngModel)]="student.Comments"
            [disabled]="!student.editing"
            (input)="makeChange(student)"
          />
        </mat-form-field>
      </mat-cell>
    </ng-container>

    <ng-container matColumnDef="edit">
      <mat-header-cell *matHeaderCellDef> </mat-header-cell>
      <mat-cell *matCellDef="let student">
        <button
          *ngIf="!student.isPresent() && !student.editing"
          mat-raised-button
          color="primary"
          (click)="startEditing(student)"
        >
          Edit
        </button>
        <button
          *ngIf="!student.isPresent() && student.editing"
          mat-raised-button
          color="warn"
          (click)="saveEdits(student)"
        >
          Finish
        </button>
      </mat-cell>
    </ng-container>

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

Picture of component

2 个答案:

答案 0 :(得分:0)

要确保表格可滚动,请以固定高度将表格包装在init内并使其div

您还可以检查下面的链接,这些链接既具有页面又具有可滚动的表格。

  1. https://angular-qfqpwr.stackblitz.io(正在工作的Stackblitz)
  2. https://material.angular.io/components/table/examples(带粘性表头的表)

答案 1 :(得分:0)

要扩展@Pankaj Prakash的答案,您应该将外部容器上的overflow属性设置为overflow: hidden

然后将div包裹在mat单元格周围,并将属性设置为overflow: scroll。如果只希望垂直滚动,则还可以设置以下属性:overflow-y: scroll