如何在关闭弹出窗口后刷新数据表

How to refresh dataTable after closing a popup

在使用弹出窗口向其中添加新文章后,我尝试刷新我的 table,但我的代码没有执行我需要的操作,它工作正常但没有刷新我的 table 这是我的 createModal 函数

createModal(){
    const modalRef = this.modalService.open(SelectNewArticleModalComponent,{ size: 'lg' });
    //modalRef.componentInstance.id = undefined;
    modalRef.result.then(() =>{
    this.tournee = this.creationTourneeService.getTournee();
    this.changeDetectorRef.detectChanges();
    }
    );
  }

在模式中,我尝试将我的数据添加到服务中名为“tournee”的本地属性

这是我的模态component.ts

export class SelectNewArticleModalComponent implements
  OnInit,
  OnDestroy {
  private subscriptions: Subscription[] = [];

  formGroup: FormGroup;
  codes: string[];
  detail: Observable<Article[]>
  articles: Article[] = [];
  constructor(public modal: NgbActiveModal,
    public articleService: ArticleService,
    private creationTourneeService: CreationTourneeService,
    private fb: FormBuilder,
  ) { }

  ngOnInit(): void {
    this.articleService.fetchNonSelectedArticle(this.creationTourneeService.getTournee().code);
    this.loadForm();
    const sb = this.articleService.isLoading$.subscribe(res => this.isLoading = res);
    this.subscriptions.push(sb);
  }

  ngOnDestroy() {
    this.subscriptions.forEach((sb) => sb.unsubscribe());
  }

  dismiss() {
    this.modal.dismiss();
  }

  loadForm() {
    this.formGroup = this.fb.group({
      articles: [this.articles],
    });
  }
  SaveArticles() {
    const tournee = this.creationTourneeService.getTournee();
    for(let code of this.grouping.getSelectedRows()){
      this.articleService.getItemByCode(code).subscribe(res=>{
        const detailTournee: DetailTournee = new DetailTournee();
        detailTournee.article = res;
        detailTournee.articleCode = res.code;
        tournee.detailTournees.push(detailTournee);
      });
    }
    this.creationTourneeService.setTournee(tournee);
    
    this.modal.close();
  }
}

根据您在问题中的共享代码,您在插入之前刷新 table 数据似乎存在问题。

所以尝试等待几秒钟让数据插入数据库,然后刷新。

尝试将您的模态关闭回调函数替换为以下内容:-

modalRef.result.then(() => {
  setTimeout(() =>{ 
    this.tournee = this.creationTourneeService.getTournee();
    this.changeDetectorRef.detectChanges();
    }, 1000);
 });