如何动态地将数据传递到物料表数据源 [英] How to pass data to material table data source dynamically

查看:69
本文介绍了如何动态地将数据传递到物料表数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将动态数据传递到物料表.我显示我的代码:

I want to pass the dynamic data to material table. I show my code:

parent.component.html

parent.component.html

<div class="overview">
  <div class="vehicle-img">
    <img src={{vehicleImg}} />
  </div>
  <div class="board">
    <div class="board-column company-overview">
      <div class="board-column-header">{{ "COMPANY_OVERVIEW.TITLE" | translate}}</div>
      <div class="board-column-content">
        <app-company-overview [companyOverviewData]="companyOverviewData"></app-company-overview>
        <div *ngIf="!companyOverviewData" class="loading">
          <app-loading></app-loading>
        </div>
      </div>
    </div>
    <div class="board-column feeds">
      <div class="board-column-header">{{ "FEEDS.TITLE" | translate}}</div>
      <div class="board-column-content">
        <app-feeds [feedsOverviewData ]="feedsOverviewData "></app-feeds>
        <div *ngIf="!feedsData" class="loading">
            <app-loading></app-loading>
      </div>
    </div>
  </div>
</div>

在此父组件中,它将调用API以获取feedsOverviewData

in this parent component, it will call a API to get feedsOverviewData

 this.feedsService.getFeedsByVin(this.vin).subscribe((data) => {
        this.feedsOverviewData = data;
      });

,此feedsOverviewData将被传输到app-feeds组件,该组件还有一个子组件feeds-list组件

and this feedsOverviewData will be transport to app-feeds component, this component has also a child component feeds-list component,

<div class="feeds">
  <mat-card class="feed-card">
    <mat-card-title>
      <div class="title">
        <h3>{{ 'FEEDS.SUBTITLE' | translate}} </h3>
        <hr>
      </div>
    </mat-card-title>
    <app-feed-list [feedsOverviewData]="feedsOverviewData"></app-feed-list>
    <div class="comment">
      <textarea class="textarea" matInput placeholder="Enter your comment ..." [(ngModel)]="feedsComment"></textarea>
      <button mat-button [disabled]="!feedsComment">
        <mat-icon class="send-button" matSuffix>send</mat-icon>
      </button>
    </div>
  </mat-card>
</div>

此供稿列表组件是用角度材料表实现的.

this feed-list component is implemented with angluar material table.

<table mat-table [dataSource]="dataSource">
    <ng-container matColumnDef="feeds">
      <td mat-cell *matCellDef="let item">
        <mat-card-header>
          <div mat-card-avatar>
            <mat-icon class="avatar">insert_emoticon</mat-icon>
          </div>
          <mat-card-title class="feeds-header"><b>
              <!-- <div *ngIf="item.type === 'searchevent'">
                            <span>n-Level {{item.user}}</span>
                        </div> -->
              <div *ngIf="item.comment === 'searchevent'">
                <span>Event</span>
              </div>
            </b>
          </mat-card-title>
          <mat-card-subtitle class="feeds-date">{{item.timestamp | date: 'dd/MM/yyyy'}}</mat-card-subtitle>
        </mat-card-header>
        <mat-card-content>
          <div *ngIf="item.type !== 'searchevent'">
            <div class="feeds-info">{{item.comment}}</div>
          </div>
          <div *ngIf="item.type === 'searchevent'">
            <div class="feeds-info">FIN search executed by {{item.user}}.</div>
          </div>
        </mat-card-content>
      </td>
    </ng-container>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
  </table>

和feed-list.ts就像这样:

and feed-list.ts is just like this:

export class FeedListComponent implements OnInit {
  displayedColumns: string[] = ['feeds'];
  @Input() feedsOverviewData: Feeds[];
  @ViewChild(MatPaginator) paginator: MatPaginator;
  dataSource = new MatTableDataSource<any>();
  constructor() {
  }

  ngOnInit() {
    this.dataSource.paginator = this.paginator;
    this.dataSource.data = this.feedsOverviewData;
  }
  ngOnChange() {
    console.log(this.feedsOverviewData);
    this.dataSource.data = this.feedsOverviewData;
  }

}

我尝试了许多方法来将Dynamica数据传递到表,但是它没有用.

I have tried many ways to pass this dynamica data to table, but it was not working.

你有什么想法吗?

最好的问候,

狮子座

推荐答案

您可以像这样使用它:基于数据的副本创建新的数据源(出于不变性)

you can use it like this: create a new DataSource based on a copy of the data (for immutability)

this.dataSource = new MatTableDataSource([...<[whatever incoming data]>);

尝试一下(针对您的情况)

try this (for your case)

this.feedsService.getFeedsByVin(this.vin).subscribe((data) => {
        this.feedsOverviewData = [...data];
      });

(类似于ngOnInit的设置)

(similar setup for ngOnInit)

 ngOnChange() {
    console.log(this.feedsOverviewData);
    this.dataSource = new MatTableDataSource(this.feedsOverviewData);
  }

订阅在其自己的异步过程中工作,仅那里有可用数据. angular在幕后做了一些事情来使这项工作有效.

the subscribe works in its own async process with the data only available there. angular does some things behind the scenes to make this work.

 <app-feed-list [feedsOverviewData]="this.feedsOverviewData"></app-feed-list>

这篇关于如何动态地将数据传递到物料表数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆