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.

schematic(table): using generated DataSource with async data source

See original GitHub issue

What is the expected behavior?

Documentation for how to get async data DataSource connect() from (http.get.....().subscribe()....)

I don’t ask for example with static data nor old syntax, but how to use with actual schematic for Table and his DataSource

What is the current behavior?

DataSource extension is generated like this:

connect(): Observable<any[]> {
    // Combine everything that affects the rendered data into one update
    // stream for the data-table to consume.

    const dataMutations = [
      observableOf(this.data),
      this.paginator.page,
      this.sort.sortChange
    ];

    // Set the paginator's length
    this.paginator.length = this.data.length;

    return merge(...dataMutations).pipe(map(() => {
      return this.getPagedData(this.getSortedData([...this.data]));
    }));

  }

No documentation, where and how to call http.get… for getting async data for connect() method.

What are the steps to reproduce?

Providing a StackBlitz reproduction is the best way to share your issue.
StackBlitz starter: https://goo.gl/wwnhMV

Try to use material table schematic and fill connect() not with static data but async call like http.get

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Actual angular, actual material 7.3.7, needed typescript

Is there anything else we should know?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
Splaktarcommented, Apr 13, 2019

Ah OK, I see what you mean now.

On the MatTable examples page, the “Table retrieving data through HTTP” example could be helpful. Here’s a Stackblitz link to that example. Note that this example does paging and sorting on the server.

Looking at your getIOPList(id) call, it looks like you may be wanting an example that does paging and sorting on the client. Here’s an example that does that:

  connect(): Observable<MyItem[]> {
    this.subscription = observableOf(EXAMPLE_DATA).subscribe((result: MyItem[]) => {
      this.data = result;
      this.paginator.length = this.data.length;
    });

    // Combine everything that affects the rendered data into one update
    // stream for the data-table to consume.
    const dataMutations = [
      observableOf(this.data),
      this.paginator.page,
      this.sort.sortChange
    ];

    return merge(...dataMutations)
    .pipe(
      map(() => this.getPagedData(this.getSortedData([...this.data])))
    );
  }

Converting this for your code should look something like this:

  connect(): Observable<MyItem[]> {
    this.subscription = this.service.getIOPList(10052).subscribe(r => {
      this.data = r.table;
      this.paginator.length = this.data.length;
    });

    // Combine everything that affects the rendered data into one update
    // stream for the data-table to consume.
    const dataMutations = [
      observableOf(this.data),
      this.paginator.page,
      this.sort.sortChange
    ];

    return merge(...dataMutations)
    .pipe(
      map(() => this.getPagedData(this.getSortedData([...this.data])))
    );
  }

Then you will probably want to change your template from

  <mat-paginator #paginator
      [length]="dataSource.data.length"
      [pageIndex]="0"
      [pageSize]="50"
      [pageSizeOptions]="[25, 50, 100, 250]">
  </mat-paginator>

to

  <mat-paginator #paginator
      [pageIndex]="0"
      [pageSize]="50"
      [pageSizeOptions]="[25, 50, 100, 250]">
  </mat-paginator>
1reaction
b-micommented, Apr 12, 2019

Splaktar, I don’t have problem with usage of HttpClient - it works perfectly. I have problem how to connect async data with new/actual version of table schematic.

  1. angular table schematic should not use as a sample static data, but async data.

  2. As an answer to my question should be that I don’t need change connect() method, but I should get async data first and then create DataSource. It is better, but still with some strange errors.

I need fully functional sample with async data, not static. (No, I do not need link for HttpClient!!)

“Old syntax”? Google and look at various versions of usage material table schematic.

Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular Material Data Table: A Complete Example
A complete example of an Angular Material Data Table with server-side pagination, sorting, filtering, as well as a loading indicator.
Read more >
Angular Mat Table with remote source for data - Stack Overflow
I am trying to update the data source, and while the data shows up it doesn't apply any sorting or pagination that has...
Read more >
Asynchronous Paging Data Source - Infragistics WPF™ Help
This topic provides instructions on how to create data sources that allow retrieving data from high-latency remote data stores using an asynchronous and ......
Read more >
Writing query resolvers | Full-Stack Quickstart - Apollo GraphQL
We've designed our schema and configured our data sources, but our server doesn't know how to use its data sources to populate schema...
Read more >
Data Sources Methods - Tableau Help
4, there was no way to append data to an extract with multiple tables using the REST API. Data Sources stored locally. When...
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