Getting error in Angular 5 Datatables: cannot reinitialise DataTable

前端 未结 4 2087
再見小時候
再見小時候 2021-01-05 10:00

I am working on the angular based project. I am using datatables to display data. When I try to do some modifications on the table, I get the following error.

DataT

4条回答
  •  太阳男子
    2021-01-05 10:13

    I have tried this in angular 7 and it worked.. runtime datatable changed

    .Ts file

        import { Component, AfterViewInit, OnDestroy, OnInit, ViewChild } from '@angular/core';
        import { DataTableDirective } from 'angular-datatables';
        
        @Component({
          selector: 'app-selector',
          templateUrl: 'app-selector.component.html'
        })
        export class CustomRangeSearchComponent implements AfterViewInit, OnDestroy, OnInit {
          @ViewChild(DataTableDirective, {static: false})
          datatableElement: DataTableDirective;
          dtOptions: DataTables.Settings = {};
          tableData = [];
        
          ngOnInit(): void {
            this.dtOptions = {
              destroy: true,
              ordering: true,
              pagelength: 10,
              pagingType: "full_numbers",
              columnDefs: [{ 
                targets: 0,
                checkboxes:{
                selectRow: true,
                selected: true
              }]
            };
          }
        
          ngOnDestroy(): void {
            $.fn['dataTable'].ext.search.pop();
          }
    
          ngAfterViewInit(): void {
            this.dtTrigger.next();
          }
        
          getdata(){
        
            //... get your response
            this.tableData = response; //this should be in array
    
            setTimeout(() => {
              this.datatableElement.dtInstance.then((dtInstance: DataTables.Api) => {
                dtInstance.draw();
              });
            });
          }
        }
    

    .HTML file

    
    

提交回复
热议问题