1

我正在使用 amat-table来显示文章列表。我可以更改不同文章的价格。更新文章价格后,我将取回更新的文章(更新的资源)。我想刷新我的数据源,而不必调用 getArticles 并加载所有文章,因为我已经有了更新的文章。

所以我可以更新数据源来替换旧的更新项目,或者这是正确的方法吗?

private updateArticle(value: any, id: string): void {
    this.dataService.patchArticle(id,price)
        .subscribe(
            article => {
               this.dataService.getArticles(); //that is unnecessary, I have the updated article in variable 'article'
               //this.replaceArticleDatasource(article) //update datasource
              .subscribe(
                  data => {
                    this.articles = data;
                    this.dataSource = new MatTableDataSource(this.articles);
              });
            }
          );
}
4

2 回答 2

2

你可以试试。假设您的补丁返回更新文章。

private updateArticle(value: any, id: string): void 
{
    this.dataService.patchArticle(id,price).subscribe(article => {
    this.atricles.find(x => x.id === article.id) = article;

    this.dataSource.data = this.articles;
});
于 2019-03-06T23:26:02.437 回答
0

// 你也可以试试 ..它可能会有所帮助

  datasource:any;

  @ViewChild(MatPaginator) paginator: MatPaginator;

  @ViewChild(MatSort) sort: MatSort;


  private updateArticle(value: any, id: string): void 
  {
    this.dataService.patchArticle(id,price).subscribe(article => 
    {
      this.datasource = new MatTableDataSource();

      let article_filtered =

      this.article.find(data => data.id === article.id);

      this.datasource.data = article_filtered;

      **// for sorting in mat table**

      this.datasource.sort = this.sort;

      **// for pagination in mat table** 

      this.datasource.paginator = this.paginator; 

   });
于 2019-03-07T05:26:12.793 回答