通过 TypeScript 扩展 Angular Material 扩展 table 行

Expand Angular Material expansion table row via TypeScript

我正在使用我从文档 here 修改的 Angular Material 扩展数据 table。进行更改时,我会刷新 table 中的数据。这工作正常,但 table 在刷新后完全折叠到默认状态,造成不良的用户体验。

我想在刷新后强制展开先前打开的行。无论如何,我还没有找到从 TypeScript 中强制扩展行的方法。下面是我在发生 table 刷新时调用的方法:

  refresh() {
    this.refreshDatatable();
    // I want to forcefully open the previously expanded row HERE.
  }

如有任何帮助,我们将不胜感激!

这是一个允许您扩展默认 table 行的示例。 我希望这能帮到您。 您只需从之前在数据列表中打开的选项卡中检索索引。

TS:

export class TableExpandableRowsExample implements OnInit {
  dataSource = ELEMENT_DATA;
  columnsToDisplay = ['name', 'weight', 'symbol', 'position'];
  expandedElement: PeriodicElement | null;

  ngOnInit() {
    this.expandedElement = ELEMENT_DATA[2];
  }
}

HTML:

<ng-container matColumnDef="expandedDetail">
  <td mat-cell *matCellDef="let element" [attr.colspan]="columnsToDisplay.length">
    <div class="example-element-detail" [@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'">
      <div class="example-element-diagram">
        <div class="example-element-position"> {{element.position}} </div>
        <div class="example-element-symbol"> {{element.symbol}} </div>
        <div class="example-element-name"> {{element.name}} </div>
        <div class="example-element-weight"> {{element.weight}} </div>
      </div>
      <div class="example-element-description">
        {{element.description}}
        <span class="example-element-description-attribution"> -- Wikipedia </span>
      </div>
    </div>
  </td>
</ng-container>

StackBlitz HERE

演示: