Loading data from API
I'm trying to load data from a API to show in the ngTable the thing is that, the data are loaded async. Now Im loading the data with a ngOnInit that has this.httpService.method().subscribe(data => {}) that returned the data that I want to load in the table. When its loaded the table is empty, but if you tried to filter something the data shows up. I dont know what i can do to solve this... I want that all the data shows up at the beging.
I run into the same problem. Have you solved this?
You can call this.onChangeTable(this.config) on callback or promise.
public loadDataToModel():void {
this.service.dataFromService$.subscribe(data => {
this.data = [];
this.data.push(data); // sort out data table
this.length = this.data.length; // this is for pagination
this.onChangeTable(this.config, true); //refresh table!
});
this.service.dataLoadingFunction();
};
Yes that helped! Thanks a lot
This does NOT work for me.
Is there an example of this anywhere ? I have a similar issue where it only seems to partially work after I attempt to sort the data.
I call this onIit - Do I need to call this in other places as well ?
getData() {
this.dataService.findAll()
.subscribe(
data => {
this.data = data
this.length = this.data.length; // this is for pagination
this.onChangeTable(this.config, true); //refresh tab error => { console.log("error") }
}
)
}
@mdaymond : Same issue here. Did you find any solution yet?
@nankathi - didn't find one...you ?
@mdaymond : I was able to load the data from the server as suggested above but i did that in the constructor and iam not using any pagination here. sample code for you
this._personService.getPersons()
.subscribe(persons => {
this.data = persons;
this.length = this.data.length;
this.onChangeTable(this.config, false);
});
P.S : There's is a lag in loading the records. In my case i have around 400 records don't know why its happening.
Any suggestions?
Also please note if your data(records) has null values please make sure to have a check on them if not you will come across this error :- initially the records will not be loaded but when you click on sort it will show the records.
It's working for me except for the initial set of results. I have paging and when I set the length to 2 it loads all 10 rows at first. After I page once it goes away and limits the results to 2.
It's not working for me, don't display anything from data :'(
Please help me!!
No solution yet..
Try this, the subscribe into onChangeTable. The filteredData, sortedData, this.rows and this.length will set after set this.data
public onChangeTable(config: any, page: any = {page: this.page, itemsPerPage: this.itemsPerPage}): any {
if (config.filtering) {
Object.assign(this.config.filtering, config.filtering);
}
if (config.sorting) {
Object.assign(this.config.sorting, config.sorting);
}
this._unidadService.buscarTodosConComplejo(this.token).subscribe(
resp => {
if (!resp.success) {
this.mensaje_error = 'No hay unidades cargadas';
} else {
// console.log(resp.datos);
this.data = resp.datos;
this.length = this.data.length;
const filteredData = this.changeFilter(this.data, this.config);
const sortedData = this.changeSort(filteredData, this.config);
this.rows = page && config.paging ? this.changePage(page, sortedData) : sortedData;
this.length = sortedData.length;
}
}
,
error => {
const errorMessage = <any> error;
if (errorMessage != null) {
console.log(error);
}
}
);
}