ng2-table icon indicating copy to clipboard operation
ng2-table copied to clipboard

Loading data from API

Open luismanuelmansilla opened this issue 9 years ago • 13 comments

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.

luismanuelmansilla avatar Dec 19 '16 05:12 luismanuelmansilla

I run into the same problem. Have you solved this?

hesyar avatar Jan 10 '17 08:01 hesyar

You can call this.onChangeTable(this.config) on callback or promise.

webcat12345 avatar Jan 10 '17 08:01 webcat12345

  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();
  };

webcat12345 avatar Jan 10 '17 08:01 webcat12345

Yes that helped! Thanks a lot

hesyar avatar Jan 10 '17 09:01 hesyar

This does NOT work for me.

tosehee75 avatar Feb 09 '17 18:02 tosehee75

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 avatar Mar 24 '17 14:03 mdaymond

@mdaymond : Same issue here. Did you find any solution yet?

rjankathi avatar Apr 03 '17 02:04 rjankathi

@nankathi - didn't find one...you ?

mdaymond avatar Apr 21 '17 13:04 mdaymond

@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.

rjankathi avatar Apr 26 '17 17:04 rjankathi

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.

mdaymond avatar Apr 30 '17 19:04 mdaymond

It's not working for me, don't display anything from data :'(

Please help me!!

hieuit7 avatar Aug 14 '17 16:08 hieuit7

No solution yet..

TeodorKolev avatar Aug 31 '17 07:08 TeodorKolev

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);
            }
        }
    );


}

munozdaniel avatar Dec 28 '17 12:12 munozdaniel