【问题标题】:Angular Material - MatTable expandable row animationAngular Material - MatTable 可扩展行动画
【发布时间】:2019-01-10 14:45:46
【问题描述】:

目前我已经构建了一个带有可扩展行的MatTable

<!-- Hidden cell -->
<ng-container matColumnDef="expandedDetail">
    <td mat-cell *matCellDef="let myModel" [attr.colspan]="displayedColumns.length">
        <div
            class="detail-cell"
            [@detailExpand]="myModel.isExpanded ? 'expanded' : 'collapsed'"
        >
            <my-inner-component
                ...
            ></my-inner-component>
        </div>
    </td>
</ng-container>

还有行:

<!-- Hidden row -->
<tr
    mat-row
    *matRowDef="let myModel; columns: ['expandedDetail']"
></tr>

可展开的行附加了一个动画:

animations: [
    trigger('detailExpand', [
        state('collapsed, void', style({ height: '0px', minHeight: '0', display: 'none' })),
        state('expanded', style({ height: '*' })),
        transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)'))
    ])
]

由于我显示了很多行,并且 my-inner-component 是一个沉重的组件,我希望仅在行扩展时创建它。
所以我补充说:

*ngIf="myModel.isExpanded"

到包含div

但是,动画显然会中断。
我怎么解决这个问题?如果可能的话,我想保持动画。

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    使用entry/leave 转换:

    ts 文件中的动画:

    animations: [
      trigger(
        'detailExpand', [
          transition(':enter', [
            style({ height: '0px', minHeight: '0', display: 'none' }),
            animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)', style({ height: '*' }))
          ]),
          transition(':leave', [
            style({ height: '*' }),
            animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)', style({ height: '0px', minHeight: '0', display: 'none' }))
          ])
        ]
      )
    ]
    

    在模板中使用它:

    <my-inner-component *ngIf="myModel.isExpanded" [@detailExpand]>
      ...
    </my-inner-component>
    

    【讨论】:

    • 关闭动画不起作用。它立即关闭,因为my-inner-component 不再存在
    • 顺便说一句,如果我将*ngIf[@detailExpand] 移动到div 上似乎效果更好。知道为什么吗?在内部组件上它是滞后的
    • 这很有趣但很奇怪,对不起,我不知道为什么会这样......
    猜你喜欢
    • 2022-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 2019-02-16
    相关资源
    最近更新 更多