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

Catching loading event

Open Squidy06 opened this issue 7 years ago • 9 comments

Hello, I want to make a spinner indicating data being refreshed. But how can I catch event for data being loaded?

Im getting data this way:

this.source = new ServerDataSource(http, { endPoint: 'https://jsonplaceholder.typicode.com/photos' });

I tried to user LocalDataSource, and manually getting data on "onSearch":

getData(modifiedData?) {
        this.loading = true;
        let sourceCopy = modifiedData ? modifiedData : this.source;
        let requestData = {
            filter: sourceCopy.getFilter(),
            paging: sourceCopy.getPaging(),
            sort: sourceCopy.getSort()
        }
        this.service.getData(requestData).then((data) => {
            this.source.load(data);
            this.loading = false;
        });
    }

This works for search input, but this way I'd have to write pagination component on my own, while the stock one is just fine.

Any ideas?

Squidy06 avatar May 14 '18 22:05 Squidy06

@Squidy06 Any Updates on this?

erpardeepjain avatar Jun 21 '18 07:06 erpardeepjain

@MrPardeep Yup Take and import this: https://github.com/akveo/ng2-smart-table/blob/master/src/app/pages/examples/server/serve.data-source.ts

In component:

constructor(http: Http) {
    this.http = http;
    this.source = new CustomServerDataSource(this.http, {
        endPoint: 'https://jsonplaceholder.typicode.com/posts?',
    });
    this.source.onUpdateStarted().subscribe(e => {
        setTimeout(() => {
            this.loading = true;
        });
    });
}
ngOnInit(): void {
    this.source.onChanged().subscribe(e => {
        setTimeout(() => {
            this.loading = false;
        });
    });
}

In your new data-source.ts

protected onUpdateStartedSource = new Subject<any>();
protected emitOnUpdateStarted(element: any) {
    this.onUpdateStartedSource.next(element);
}
onUpdateStarted(): Observable<any> {
    return this.onUpdateStartedSource.asObservable();
}

And add to your getElements() function in data-source.ts

this.emitOnUpdateStarted();

Squidy06 avatar Jul 02 '18 12:07 Squidy06

@Squidy06 , So you made changes in the existing package/Library? If so this is not what I want to do, because this caused a problem while updating to new version of anular/ng2-smart-table.

erpardeepjain avatar Jul 05 '18 06:07 erpardeepjain

Any update on this please?

mykeels avatar Aug 26 '19 16:08 mykeels

@mykeels @MrPardeep better move to Angular Material Table.

https://material.angular.io/components/table/overview

Squidy06 avatar Aug 26 '19 16:08 Squidy06

any update on this?

DanialV avatar Feb 15 '21 21:02 DanialV

Using suggestion by @Squidy06, I did this:

  1. I created a new file in my project (to avoid package dependencie):
import {ServerDataSource} from 'ng2-smart-table';
import {HttpClient} from '@angular/common/http';
import {ServerSourceConf} from 'ng2-smart-table/lib/lib/data-source/server/server-source.conf';
import {Observable, Subject} from 'rxjs';

export class CustomServerDataSource extends ServerDataSource {
  constructor(http: HttpClient, conf?: ServerSourceConf | {}) {
    super(http, conf);
  }

  protected onUpdateStartedSource = new Subject<any>();
  protected emitOnUpdateStarted(element: any) {
    this.onUpdateStartedSource.next(element);
  }
  onUpdateStarted(): Observable<any> {
    return this.onUpdateStartedSource.asObservable();
  }

  getElements(): Promise<any> {
    const element = super.getElements();
    this.emitOnUpdateStarted(element);
    return element;
  }
}
  1. In my component, I did this:
@Component({..})
export class MyComponent implements OnInit {
  public source: CustomServerDataSource;
  public isLoadingData = false;

  constructor() {
    this.source = new CustomServerDataSource(http, {
      endPoint: environment.apiMainUrl + 'settings'
    });
    this.source.onUpdateStarted().subscribe(() => {
      this.isLoadingData = true;
    });
  }

  ngOnInit(): void {
    this.source.onChanged().subscribe(() => {
      this.isLoadingData = false;
    });
  }
}

And It works like a charm!

Thanks @Squidy06

caallondono avatar Dec 06 '21 16:12 caallondono

Using suggestion by @Squidy06, I did this:

1. I created a new file in my project (to avoid package dependencie):
import {ServerDataSource} from 'ng2-smart-table';
import {HttpClient} from '@angular/common/http';
import {ServerSourceConf} from 'ng2-smart-table/lib/lib/data-source/server/server-source.conf';
import {Observable, Subject} from 'rxjs';

export class CustomServerDataSource extends ServerDataSource {
  constructor(http: HttpClient, conf?: ServerSourceConf | {}) {
    super(http, conf);
  }

  protected onUpdateStartedSource = new Subject<any>();
  protected emitOnUpdateStarted(element: any) {
    this.onUpdateStartedSource.next(element);
  }
  onUpdateStarted(): Observable<any> {
    return this.onUpdateStartedSource.asObservable();
  }

  getElements(): Promise<any> {
    const element = super.getElements();
    this.emitOnUpdateStarted(element);
    return element;
  }
}
2. In my component, I did this:
@Component({..})
export class MyComponent implements OnInit {
  public source: CustomServerDataSource;
  public isLoadingData = false;

  constructor() {
    this.source = new CustomServerDataSource(http, {
      endPoint: environment.apiMainUrl + 'settings'
    });
    this.source.onUpdateStarted().subscribe(() => {
      this.isLoadingData = true;
    });
  }

  ngOnInit(): void {
    this.source.onChanged().subscribe(() => {
      this.isLoadingData = false;
    });
  }
}

And It works like a charm!

Thanks @Squidy06

And for the html? There is no spinner to trigger inside the table and I don't want destroy the table while the data is loading.

marcmosco avatar Jan 20 '22 16:01 marcmosco

@MrPardeep Yup Take and import this: https://github.com/akveo/ng2-smart-table/blob/master/src/app/pages/examples/server/serve.data-source.ts

This link is broken. Does anybody have steps to create custom class from ServerDataSource? I managed to write something like this, but I guess there could be more elegant way:

export class MyDataSource extends ServerDataSource {
    constructor(http: HttpClient, conf?: ServerSourceConf | {}) {        
        super(http, conf);
        this.init();
      }     
      public init(): Promise<any> {
          let endPoint = `${environment.apiEndpoint}/${this.conf.endPoint}`;
          this.conf.endPoint = endPoint;
          this.conf.sortFieldKey = 'orderBy';
          this.conf.sortDirKey = 'sortDirection';
          this.conf.pagerPageKey = 'pageNumber';
          this.conf.pagerLimitKey = 'pageSize';
          this.conf.filterFieldKey = null;
          this.conf.totalKey = 'count';
          this.conf.dataKey = 'value';
          return super.getElements();
      }
}

call:

this.source = new MyDataSource(this.http, {
	endPoint: 'myMethod'
});

imbrod avatar Jan 21 '22 16:01 imbrod