【问题标题】:Mat-table event on row Added行上的 Mat-table 事件已添加
【发布时间】:2020-02-13 19:28:18
【问题描述】:

我想在 mat-table 中添加新行时执行一项操作,但我无法知道它何时发生。有没有发射器或其他方式?我已经阅读了所有 mat-table doc 并在 Internet 上搜索,但我没有找到任何东西。

我想要这个:

<tr mat-row *matRowDef="let row; columns: columns;" onAdd=(function())>

为了更新我刚刚输入的数据源: this.datasource.data = newData

【问题讨论】:

  • 新表格行是如何添加的?
  • @Rob,我更改了数据源,因为我不知道要添加的新行。
  • 抱歉,我不明白 - 你的意思是使用 http 请求加载新数据?
  • @Roby,我有一个覆盖数据源的订阅。我需要知道哪些行是新的,哪些不是。
  • 每一行是否都有某种类型的唯一 ID?

标签: angular angular-material mat-table


【解决方案1】:

您可以执行以下操作吗(我使用了代码“CaseDetailModel”中的现有模型,因为您没有提供有关您的数据类型的任何信息):

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { CaseDetailModel } from '../../models/casedetailmodel';
import { MatTableDataSource } from '@angular/material/table';

@Component({
  selector: 'app-stackoverflow',
  templateUrl: './stackoverflow.component.html',
  styleUrls: ['./stackoverflow.component.scss']
})

export class StackoverflowComponent implements OnInit {

  private currentCaseDetails: CaseDetailModel[] = [];
  private newCaseDetails: CaseDetailModel[] = [];

  public dataSource: MatTableDataSource<CaseDetailModel>;

  constructor(private http: HttpClient) {}

  ngOnInit(): void {
    this.http.get<CaseDetailModel[]>('/some/data/').subscribe((caseDetails: CaseDetailModel[]) => {

      //This if statement will stop the initial list items all being classed as new - you may want to remove it
      if (this.currentCaseDetails.length) {
        //Get any items that do not currently exist in the table
        this.newCaseDetails = caseDetails.filter(cd => {
          return !this.currentCaseDetails.some(ccd => ccd.caseId === cd.caseId);
        });
      }

      this.newCaseDetails.forEach(ncd => {
        //Do something with each new item
      });

      this.currentCaseDetails = caseDetails;

      this.dataSource = new MatTableDataSource(caseDetails);
    });
  }
}

【讨论】:

  • 谢谢!在 neCaseDetails 我只有值而不是行,所以我需要你之前所说的另一种方法来检查它是否是新行。然后它可以正常工作,但我的代码还有其他复杂性。
猜你喜欢
  • 2019-01-03
  • 2021-11-25
  • 2020-02-23
  • 1970-01-01
  • 2018-10-03
  • 1970-01-01
  • 2019-06-09
  • 2020-02-01
  • 2019-08-23
相关资源
最近更新 更多