-
Notifications
You must be signed in to change notification settings - Fork 487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loading dtOptions later because of using a Promise/Observable #980
Labels
Comments
You can use the ngIf directive to display the table only when the options are loaded: <div *ngIf="displayTable">
<table datatable [dtOptions]="dtOptions" class="dataTable row-border hover"></table>
</div> import { Http, OnInit } from '@angular/http';
export class Table implements OnInit {
dOptions: any = {};
displayTable: boolean = false;
constructor(private http:Http) { }
ngOnInit(): void {
this.http.get('app/table/table.config.json')
.subscribe(res => {
this.dtOptions = res.json();
this.displayTable = true;
});
}
} |
Or you can use a promise instead: <table datatable [dtOptions]="dtOptions" class="dataTable row-border hover"></table> import { Component, Inject, OnInit } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
@Component({
selector: 'load-dt-options-with-promise',
templateUrl: 'load-dt-options-with-promise.component.html'
})
export class LoadDtOptionsWithPromiseComponent implements OnInit {
dtOptions: any = {};
constructor(@Inject(Http) private http: Http) {}
ngOnInit(): void {
this.dtOptions = this.http.get('app/table/table.config.json')
.toPromise()
.then(response => response.json())
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error); // for demo purposes only
return Promise.reject(error.message || error);
}
} |
l-lin
added a commit
that referenced
this issue
Feb 26, 2017
Indeed, using the promise works great. Thanks for the fast reply! |
l-lin
added a commit
that referenced
this issue
Mar 3, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am using an Observable to load data that is used to load information about which columns to display. So this information needs to go into dtOptions. How do I make the directive wait for the dtOptions to be set?
What versions you are using?
What's the problem?
I get an error, probably because the datatable tries to initialize before the dtOptions are set. If I place the data directly in dtOptions without using the Http class, it works.
EXCEPTION: Uncaught (in promise): TypeError: Cannot read property 'aDataSort' of undefined
TypeError: Cannot read property 'aDataSort' of undefined
Can you share your code?
table.template.html:
table.component.ts (shortened):
The text was updated successfully, but these errors were encountered: