首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Angular上更新mat-table

在Angular上更新mat-table
EN

Stack Overflow用户
提问于 2018-09-08 10:58:45
回答 3查看 431关注 0票数 2

我需要在database.My表单上插入数据后更新mat表,并且mat表在同一页中。从表单我插入数据到数据库。在数据库上插入成功。但是我的垫子表没有更新。如果我刷新页面,则mat-table将被更新。但我需要在将数据插入数据库后进行mat-table更新。

Spring服务

代码语言:javascript
复制
const: httpOptions = {
.......
}
export class SpringService {
  public getAllClubs(): any {
  return this.http.get(this.baseUrl+"/currentseason/get/clubs",httpOptions); 
  }
}

AppComponent.ts

代码语言:javascript
复制
expport class AppComponent implements OnInit {
clubdetails = new MatTableDataSource();
displayedColumns: string[] = [.....];
  ngOnInt() {
   this.getClubList();
  }
  getClubList() {
  this.springService.getAllClubs().pipe(
  ........
  ).subscribe(res => {this.clubdetails.data= res;});
  }

}

Appcomponent.html

代码语言:javascript
复制
<form (ngSubmit)="mymethod()">........</form>
<table mat-table [dataSource] = "clubdetails">
.............
</table>
EN

回答 3

Stack Overflow用户

发布于 2018-09-09 03:05:57

您可以使用BehaviorSubject<>对象,通过此对象,您可以调用下一个函数来更新订阅元素,例如:

代码语言:javascript
复制
@Component({selector:'my-component', templateUrl: './my-component.html', style:[]})
export class MyComponentComponent implements OnInit, OnDestroy {
  private dataObj = new BehaviorSubject<any>([]);
  private dataSubscription: Subscription;

  constructor(private http: HttpClient, springService: SpringService){

    // Subscribe with BehaviorSubject
    this.dataSunscription= this.dataObj
                               .asObservable()
                               .subscribe(res => this.clubdetails.data= res);

    this.refreshData(); // fill with first data
  }

  ngOnInit(): void {}

  ngOnDestroy(): void {
    this.dataSubscription.unsubscribe();
  } 

  // Refresh the data, and call the next function of BehaviorSubject
  refreshData(){
    this.springService.getAllClubs()
        .toPromise()
        .then( dt => this.dataObj.next(dt));        
  }

  mymethod(){
    // Call your code

    refreshData(); // Refresh after post the data
  }
}

您可以在this page上看到一个示例

票数 0
EN

Stack Overflow用户

发布于 2020-12-06 21:53:03

只需在getClubList()方法中进行以下更改:

代码语言:javascript
复制
  getClubList() {
    this.springService.getAllClubs().pipe(
      ........
    ).subscribe(res => {
        this.clubdetails.data = res;
        this.clubdetails.data = this.clubdetails.data;
      });
  }
票数 0
EN

Stack Overflow用户

发布于 2019-08-06 19:04:05

在您app.component.ts中尝试以下代码,

代码语言:javascript
复制
 getClubList() {
  this.springService.getAllClubs().pipe(
  ........
  ).subscribe(res => {this.clubdetails.data= res;
this.dataSource = new MatTableDataSource(this.Model_Obj_Name);});
  }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52231606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档