angular 7 - reload data in data table

不想你离开。 提交于 2019-12-30 22:50:37

问题


I'm using angular 7 with angular-datatables. I'm tring to define a "rerender" button in order to reload data like in this example.

I don't understand what should I place in the render function:

My API function:

fn_getFavoriteTables() {

  this._getFavoriteTablesApiCall =  this.getFavoriteTablesService.getFavoriteTables(Number(localStorage.getItem('UserID')), Number(localStorage.getItem('BranchID'))).pipe(takeUntil(this.destroySubject$)).subscribe(x => {
        this.getFavoriteTables = x;
        this.dtTrigger.next();
    });

};

rerender function:

rerender(): void {
    this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
        // Destroy the table first 

        dtInstance.destroy();


        // Call the dtTrigger to rerender again
        this.dtTrigger.next();
    });
}

ngOnDestroy(): void {
  console.log('ngDestroy');
  // Do not forget to unsubscribe the event
  this.dtTrigger.unsubscribe();
}

回答1:


This can also be done like this:

import { DataTableDirective } from 'angular-datatables';

dtElement: DataTableDirective;
dtInstance: Promise<DataTables.Api>;

rerender(): void  {
  this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
    dtInstance.ajax.reload()
  });
}



回答2:


Found an answer that worked for me: this is the source site

 rerender(): void {
    this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
        // Destroy the table first 
        //debugger;
        var table = $('#favoriteTable').DataTable();

        $('#tableDestroy').on('click', function () {
            table.destroy();
        });

        dtInstance.destroy();

        this.fn_getFavoriteTables();

    });
}


来源:https://stackoverflow.com/questions/55209840/angular-7-reload-data-in-data-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!