【问题标题】:Angular mat-table expandable rows: how can i automatically expand first row? and how to control when i collapsed it?Angular mat-table 可扩展行:如何自动扩展第一行?以及如何控制我何时折叠它?
【发布时间】:2021-02-12 17:03:08
【问题描述】:

查看 stackblitz 上的示例: https://stackblitz.com/angular/ygdrrokyvkv?file=app%2Ftable-expandable-rows-example.html

1:如何在加载时自动展开第一行?

2:例如,如果我想做一些事情,比如计算我关闭一行的次数,我该怎么做?

在我的代码中,我有一个按钮可以打开和关闭它:

<td style="text-align: center;" mat-cell *matCellDef="let element">
    <mat-icon (click)="expandedElement = expandedElement === element ? null : element">
                    {{expandedElement === element ? 'expand_less' : 'expand_more'}}
    </mat-icon>
</td>

很多

【问题讨论】:

  • 我的数据源是来自数据库的 Json...

标签: angular angular-material


【解决方案1】:

要扩展第一个项目:

expandedElement: PeriodicElement = ELEMENT_DATA[0];

要算展开多少次,已经有click监听了:

(click)="expandedElement = element"

你可以用它来计算。简单例子:

(click)="toggleExpand(element)"


closedCountState = {};

toggleExpand(element) {
  if (this.closedCountState[this.expandedElement.name]) {
    this.closedCountState[this.expandedElement.name]++
  } else {
    this.closedCountState[this.expandedElement.name] = 1;
  }

  this.expandedElement = element;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 2020-05-30
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多