question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

typeError: Cannot read property 'dtInstance' of undefined - angular2

See original GitHub issue

What versions you are using?

  • angular version:2.4.0
  • jquery version: 3.2.1
  • datatables version: 1.10.15
  • angular-datatables version: 2.3.0
  • angular-cli version: 1.0.0

What’s the problem?

when I try to get instance of Datatable it gives typeError: Cannot read property ‘dtInstance’ of undefined.

Can you share your code?

datatableElement: DataTableDirective;
 dtOptions: DataTables.Settings = {};
 constructor(
 private employeeservice:EmployeeService,
 private route: ActivatedRoute,
 private router: Router,
 ) { 
  this.getEmployee();    
 }
 ngOnInit(): void {
 this.dtOptions = {
  paginationType: 'full_numbers',
  displayLength: 5
  };
 }
displayToConsole(datatableElement: DataTableDirective): void 
{
datatableElement.dtInstance.then((dtInstance: DataTables.Api) => 
console.log(dtInstance));
} 
getEmployee(){
this.employeeservice.getEmployees()
.subscribe(
    employees =>{ 
        this.employees = employees; 
        console.log(this.employees);
    (error:Response) => console.log(error)
  },
 )     
}

 deleteEmployee(employee){
 if (confirm("Are you sure you want to delete Employee ?")) {
 console.log(employee.id);
  var index = this.employees.indexOf(employee);
  this.employees.splice(index, 1);

  this.employeeservice.deleteEmployee(employee.id)
    .subscribe(
      data =>{
        this.emp_data = employee;
      },
      err => {
        alert("Could not delete employee.");
        // Revert the view back to its original state
        this.employees.splice(index, 0, employee);
      },
      () => this.router.navigate(['/employees'])
      );
  }
 }
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:31 (7 by maintainers)

github_iconTop GitHub Comments

12reactions
Gaglia88commented, Apr 17, 2020

Hi, the problem is given by the datatable directive.

In src/angular-datatables.directive.ts in ngInit (line 42) it is possible to see that if dtTrigger is set, the function displayTable is not called until the next method is called on dtTrigger.

So dtInstance will result unavailable until dtTrigger.next(); is called for the first time.

A possible solution is to rewrite the rerender() method in this way:

rerender(): void {
	if("dtInstance" in this.dtElement){
		this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
		  // Destroy the table first
		  dtInstance.destroy();
		  // Call the dtTrigger to rerender again
		  this.dtTrigger.next();
		});
	}
	else{
		this.dtTrigger.next();
	}
}
2reactions
PI-Mathiscommented, Apr 10, 2018

I had the same problem, but for some strange reason, I made it work by using a setTimeout:

  @ViewChild(DataTableDirective)
  dtElement: DataTableDirective;

  dtOptions: DataTables.Settings = {};
  products: Product[];
  isLoading = true;
  dtTrigger: Subject<any> = new Subject;

  constructor(
    private route: ActivatedRoute,
    private productService: ProductService
  ) {
    this.route.params.subscribe(params => this.doSearch(params['query']));
  }

  ngOnInit() {
    this.dtOptions = {
      pagingType: 'full_numbers',
      pageLength: 5,
      searching: false
    };
  }

  ngAfterViewInit(): void {
    this.dtTrigger.next();
  }

  doSearch(query: string) {
    setTimeout(() => {
    this.productService.queryProducts(query)
      .pipe(finalize(() => { this.isLoading = false; }))
      .subscribe((products: Product[]) => {
        this.products = products;
        this.rerender();
      });
    });
  }

  rerender(): void {
    this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
      // Destroy the table first
      dtInstance.destroy();
      // Call the dtTrigger to rerender again
      this.dtTrigger.next();
    });
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read property 'dtInstance' of undefined - Stack Overflow
Cannot read property 'dtInstance' of undefined. This is how I implement it: ts import { Component, OnInit, ViewChild } from '@angular/core'; ...
Read more >
cannot read properties of undefined (reading 'dtinstance')
I am trying to add a new button to datatable DOM in angular using renderer2. But its triggering an error in runtime saying....
Read more >
Angular Dtinstance Doesnt Exist On Dtelement (forked)
example for dtInstance doesn't exist on dtElement in angular-datatables.
Read more >
Cannot read property 'dtInstance' of undefined - angular2
typeError : Cannot read property 'dtInstance' of undefined - angular2.
Read more >
cannot read property 'dtinstance' of undefined angular datatable
angular-datatables dtinstance not working ... ERROR in TypeError: Cannot read property 'flags' of undefined 9.1.5 @angular/compiler-cli throws `Cannot read ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found