【问题标题】:How to refresh dataTable after closing a popup关闭弹出窗口后如何刷新数据表
【发布时间】:2021-08-17 08:07:15
【问题描述】:

在使用弹出窗口向表格添加新文章后,我尝试刷新我的表格,但我的代码没有做我需要的事情,它可以正常工作,但没有刷新我的表格 这是我的 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”的本地属性

这是我的模态组件.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();
  }
}

【问题讨论】:

    标签: angular typescript datatable modal-dialog refresh


    【解决方案1】:

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

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

    尝试用以下替换您的模式关闭回调函数:-

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

    【讨论】:

    • 我尝试了你写的,但仍然无法正常工作,直到我对表格进行了一些更改
    • 我希望它现在可以工作。如果是的话,你能写下你所做的更改吗?
    • 你能把它上传到 stackblitz 或其他地方,以便于查看和调试
    • 另一种解决方案可能是使 this.creationTourneeService.setTournee 可观察,以便您可以订阅它以使其执行并在订阅中发出事件并在列表页面和订阅中订阅该事件该事件,刷新表数据。所以一旦插入数据,它就会发出一个事件并刷新数据。
    • 很高兴我能帮上忙 :)
    猜你喜欢
    • 2012-07-09
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多