【问题标题】:Angular Material Table with Sticky and Custom Width Column具有粘性和自定义宽度列的角材料表
【发布时间】:2019-07-07 12:26:34
【问题描述】:

我需要一个可以通过列的粘性和宽度自定义的表格。 到目前为止,如果我在 thtd 上放置一个具有 width: 150px 的自定义类,我发现了一些东西,即水平滚动时的粘性属性不知道如何正确计算 style.left

但例如,如果我在 CSS 中覆盖类 .mat-h​​eader-cell.mat-footer-cell.mat-cell width: 150px 的 sticky 属性知道如何管理它并正确放置 style.left。

CSS:

.table-responsive {
    height: 400px;
    width: 800px;
    overflow: auto;
}

table {
    width: 1000px;
}

.mat-header-cell, .mat-footer-cell, .mat-cell {
    box-sizing: border-box;
    text-align: center;
    width: 150px;
}

.mat-header-row {
    width: 100%;
}

HTML:

<div dir="ltr" class="table-responsive mat-elevation-z8">
    <table mat-table #table [dataSource]="dataSource" matSort >
      <ng-container *ngFor="let col of displayedColumns">

        <ng-container *ngIf="col.type === 'masterCheckbox'" [matColumnDef]="col.value"
                        [sticky]="col.sticky">
          <th mat-header-cell *matHeaderCellDef [style.width]="col.width" [fxFlex]="col.width">
            <mat-checkbox (change)="$event ? masterToggle() : null"
                            [checked]="selection.hasValue() && isAllSelected()"
                            [indeterminate]="selection.hasValue() && !isAllSelected()">
            </mat-checkbox>
          </th>
          <td mat-cell *matCellDef="let row" [style.width]="col.width" [fxFlex]="col.width">
            <span class="header-label"></span>
            <mat-checkbox (click)="$event.stopPropagation()"
                            (change)="$event? selection.toggle(row) : null"
                            [checked]="selection.isSelected(row)">
            </mat-checkbox>
          </td>
        </ng-container>


        <ng-container *ngIf="col.type === 'text'" [matColumnDef]="col.value" [sticky]="col.sticky">
          <th mat-header-cell *matHeaderCellDef mat-sort-header
                [fxFlex]="col.width">
              {{ 'TABLE.COLUMN.' + col.name | translate }}
          </th>
          <td mat-cell *matCellDef="let row" [fxFlex]="col.width">
            <div fxLayout="column" fxLayoutAlign="space-between start">
              <div>{{row[col.value]}}</div>
              <div fxLayout="row" fxLayoutAlign="space-between end">
                <app-column-actions (actionEvent)="doAction($event)" [row]="row"
                                      [actions]="col.actions"></app-column-actions>
              </div>
            </div>
          </td>
        </ng-container>

        <ng-container *ngIf="col.type === 'toggle'" [matColumnDef]="col.value" [sticky]="col.sticky">
          <th mat-header-cell *matHeaderCellDef mat-sort-header
                [fxFlex]="col.width">
              {{ 'TABLE.COLUMN.' + col.name | translate }}
          </th>
          <td mat-cell *matCellDef="let row" [fxFlex]="col.width">
            <div fxLayout="column" fxLayoutAlign="space-between start">
              <mat-slide-toggle [checked]="row[col.value]"></mat-slide-toggle>
              <div fxLayout="row" fxLayoutAlign="space-between end">
                <app-column-actions (actionEvent)="doAction($event)" [row]="row"
                                      [actions]="col.actions"></app-column-actions>
              </div>
            </div>
          </td>
        </ng-container>

        <ng-container *ngIf="col.type === 'checkbox'" [matColumnDef]="col.value">
          <th mat-header-cell *matHeaderCellDef mat-sort-header
                [fxFlex]="col.width">
              {{ 'TABLE.COLUMN.' + col.name | translate }}
          </th>
          <td mat-cell *matCellDef="let row" [fxFlex]="col.width">
            <div fxLayout="column" fxLayoutAlign="space-between start">
              <span class="header-label"></span>
              <mat-checkbox [checked]="row[col.value]"></mat-checkbox>
              <div fxLayout="row" fxLayoutAlign="space-between end">
                <app-column-actions (actionEvent)="doAction($event)" [row]="row"
                                      [actions]="col.actions"></app-column-actions>
              </div>
            </div>
          </td>
        </ng-container>

        <ng-container *ngIf="col.type === 'percentage'" [matColumnDef]="col.value">
          <th mat-header-cell *matHeaderCellDef mat-sort-header
                [fxFlex]="col.width">
              {{ 'TABLE.COLUMN.' + col.name | translate }}
          </th>
          <td mat-cell *matCellDef="let row" [fxFlex]="col.width">
            <div fxLayout="column" fxLayoutAlign="space-between start">
                {{row[col.value]}}%
              <div fxLayout="row" fxLayoutAlign="space-between end">
                <app-column-actions (actionEvent)="doAction($event)" [row]="row"
                                      [actions]="col.actions"></app-column-actions>
              </div>
            </div>
          </td>
        </ng-container>

        <ng-container *ngIf="col.type === 'link'" [matColumnDef]="col.value">
          <th mat-header-cell *matHeaderCellDef mat-sort-header
                [fxFlex]="col.width">
              {{ 'TABLE.COLUMN.' + col.name | translate }}
          </th>
          <td mat-cell *matCellDef="let row" [fxFlex]="col.width">
            <div fxLayout="column" fxLayoutAlign="space-between start">
              <button> CLICK </button>
              <div fxLayout="row" fxLayoutAlign="space-between end">
                <app-column-actions (actionEvent)="doAction($event)" [row]="row"
                                      [actions]="col.actions"></app-column-actions>
              </div>
            </div>
          </td>
        </ng-container>

        <ng-container *ngIf="col.type === 'actions'" [matColumnDef]="col.value" stickyEnd>
          <th mat-header-cell *matHeaderCellDef mat-sort-header
                [fxFlex]="col.width">
              {{ 'TABLE.COLUMN.' + col.name | translate }}
          </th>
          <td mat-cell *matCellDef="let row" [fxFlex]="col.width">
            <button mat-icon-button [matMenuTriggerFor]="menu">
              <mat-icon class="actionBtn">add_circle</mat-icon>
            </button>
            <mat-menu #menu="matMenu">
              <ng-container *ngFor="let action of singleSelectionActions">
                <app-single-selection-actions
                    (actionEvent)="doAction($event)"
                    [selectedRow]="row"
                    [singleSelectionAction]="action">
                </app-single-selection-actions>
              </ng-container>
            </mat-menu>
          </td>
        </ng-container>

      </ng-container>
      <mat-header-row *matHeaderRowDef="columnDefinitions"></mat-header-row>
      <mat-row *matRowDef="let row; columns: columnDefinitions"></mat-row>
    </table>
    <mat-paginator #paginator [pageSize]="10" [pageSizeOptions]="[5, 10, 20]"></mat-paginator>
  </div>

【问题讨论】:

    标签: angular angular-material material-design sticky customcolumn


    【解决方案1】:

    如果你把 fxFlex 和一个具有最小宽度的类放在一起,它就可以工作

    .min-width-100 {
      min-width: 100px !important;
    }
    <th mat-header-cell *matHeaderCellDef mat-sort-header [fxFlex]="col.width"
                        class="min-width-100">
     {{ 'TABLE.COLUMN.' + col.name | translate }}
    </th>
    <td mat-cell *matCellDef="let row" [fxFlex]="col.width" class="min-width-100 d-flex d-j-c-flex-end">
      <div fxLayout="column" fxLayoutAlign="space-between start">
        {{row[col.value]}}%
        <div fxLayout="row" fxLayoutAlign="space-between end">
           <app-column-actions 
             (actionEvent)="doAction($event)" 
             [row]="row"
             [actions]="col.actions">
           </app-column-actions>
        </div>
      </div>
    </td>

    【讨论】:

    • 不错! min-width 是我需要添加的所有内容,我没有使用 flex 选项-
    猜你喜欢
    • 2019-04-28
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 2020-06-10
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    相关资源
    最近更新 更多