4

I am using Angular Datatables to show some tabular data. I have added an *ngIf condition to the table, so that it will not be visible until the data is loaded completely. The table code looks like the following :

<table class="table" id="releaseDatatable" datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger" *ngIf="!loading">

The loading variable I am setting from my component and then calling this.dtTrigger.next(). I have the following code to set loading.

set loading(loading: boolean) {
        this._loading = loading;
       setTimeout(()=>{
            this.dtTrigger.next();
       }, 100);
    }

Now for the first time, everything is working fine. But when I try to re-load the table (after edit or delete), it it is showing the following error :

ERROR ObjectUnsubscribedErrorImpl {message: "object unsubscribed", name: "ObjectUnsubscribedError"} 

and this.dtTrigger has null as observers. How this can be fixed ? I tried to re-render the table, but it is also not working.

3 Answers 3

4

You can use [ngClass] and hide rather than using ngIf

<table class="table" id="releaseDatatable" datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger" [ngClass]="loading">

in component make loading attribute as "hide" or "show";

in css hide it

.hide{
   display:none;
}

For render again I use destroy and and trigger next

import

  import { DataTableDirective } from 'angular-datatables';

  @ViewChild(DataTableDirective, {static: false})  
   datatableElement: DataTableDirective;

to render

rerender(){
        this.datatableElement.dtInstance.then((dtInstance: DataTables.Api) => {
            dtInstance.destroy();
            this.dtTrigger.next();
        });       
    }

call this rerender method

set loading(loading: boolean) {
       setTimeout(()=>{
            this.rerender();
       }, 100);
    }
Sign up to request clarification or add additional context in comments.

7 Comments

I think the issue is that ngIf removes the table from DOM and so for the second time, It is not able trigger the table reload. But no idea how to fix it.
@HappyCoder yes you are right. You can use class rather than ngIf as a way . I update answer with this approach
That also will not work. It is showing DataTables warning: table id=releaseDatatable - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3
This is rerender problem . I updated answer to rerender table . I firstly destroy then I trigger next @HappyCoder
Unfortunately, it is also not working It shows Cannot read property 'then' of undefined
|
2

This is a known problem with how Angular Datatables re-renders

You can find more details and workarounds from the GitHub issues list e.g. https://github.com/l-lin/angular-datatables/issues/1241

Some of the comments suggest using [hidden] instead of *ngIf

2 Comments

Any workaround ? I tried to use the hidden mentioned there, but it is showing error DataTables warning: table id=releaseDatatable - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3
Yes. But don't have any idea how to fix this.
0

It's better to add a wrapper div for your table like <div *ngIf="!loading"><table></table></div>

Angular v2 doesn't support more than one structural directive on the same element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.