0

Angular Datatable: Have search fields. will do select and will get datatable population. First time working fine, once we click with some other data and click, API response coming but data is not updated in the table. Could you please suggest me how to destroy/reinitialize table for next click. Here is the sample code I written

    //html table
    
         <table
            datatable
            [dtOptions]="dtOptions"
            [dtTrigger]="dtTrigger"
            class="row-border hover">
            </table> 

    //datatable properties

    this.dtOptions = {
          data: jsonData,
          pagingType: "full_numbers",
          pageLength: 10,
          scrollCollapse: true,
          processing: true,
          fixedHeader: true,
          destroy: true,
          columns: [
            {data: 'test1'},
            {data: 'test2'},
            {data: 'test3'},
      ]
    }

 //http.get for response

    this.http
        .get(url, {params})
        .subscribe((res) => {
          this.results = res;
          this.dtTrigger.next();
   }
    //Table destroy

     ngOnDestroy(): void {
        this.dtTrigger.unsubscribe();
      }
2
  • does your this.http.get is in ngOnInit ? Commented Dec 16, 2020 at 7:58
  • No, while click on button I'm calling this.http.get Commented Dec 16, 2020 at 8:02

1 Answer 1

0

I think this question is duplicates with

Getting error in Angular 5 Datatables: cannot reinitialise DataTable

In order to reinitialize table for next click you can define this

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

and call rerender() function after server response

Hope that answers your question

Sign up to request clarification or add additional context in comments.

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.