Skip to content
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

fix(table): schematic generates code that throws an exception #15800

Merged
merged 1 commit into from
May 17, 2019

Conversation

Splaktar
Copy link
Member

@Splaktar Splaktar commented Apr 11, 2019

fix exception when instantiating MatPaginator before dataSource defined
fix ExpressionChangedAfterItHasBeenCheckedError caused by above

  • first initialize the template w/o dataSource & w/ a sort and paginator
  • then get the references to the template's MatSort and MatPaginator
  • then set the data source's sort and paginator
  • then set the table's data source from the component

Fixes #15788

Not the same issue, but this is also related to #15791.

@Splaktar Splaktar requested a review from jelbourn as a code owner April 11, 2019 22:04
@Splaktar Splaktar self-assigned this Apr 11, 2019
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Apr 11, 2019
@jelbourn
Copy link
Member

@andrewseguin PTAL

@jelbourn jelbourn added P2 The issue is important to a large percentage of users, with a workaround target: patch This PR is targeted for the next patch release labels Apr 12, 2019
@mmalerba mmalerba added aaa and removed aaa labels Apr 25, 2019
@eugene-milostivenko
Copy link

Any fixes? The problem is still relevant.

@jelbourn
Copy link
Member

@Splaktar can you rebase?

@andrewseguin can you review?

fix exception when instantiating MatPaginator before dataSource defined
fix ExpressionChangedAfterItHasBeenCheckedError caused by above
- first initialize the template w/o dataSource & w/ a sort and paginator
- then get the references to the template's MatSort and MatPaginator
- then set the data source's sort and paginator
- then set the table's data source from the component

Fixes #15788
@Splaktar Splaktar force-pushed the table-fixSchematicExceptions branch from 1596160 to dd7f4ea Compare May 13, 2019 17:48
@Splaktar
Copy link
Member Author

Rebased ✅

@andrewseguin
Copy link
Contributor

It seems it would be easier to just add [length]="dataSource?.data.length" as the fix here and we can have another PR that changes the data source structure if we want to improve how it flows.

I'm not a fan of providing the data source as an input to the table in the code rather than the template, and the data source code in general is confusing because it tries to be a mix of client-side and server-side data.

Also, this is particular is confusing to read and doesn't seem to be the best way to show users how to use the datasource:

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

@Splaktar
Copy link
Member Author

@andrewseguin I tried to explain it a bit in the commit, but it may be too brief. I had discussed this a good deal with Paul while working on it.

Just making the [length]="dataSource?.data.length" change causes an ExpressionChangedAfterItHasBeenCheckedError to be thrown. That can be worked around by using ChangeDetectorRef.detectChanges() but that didn't seem like a good practice to be using in a schematic.

@jelbourn
Copy link
Member

jelbourn commented May 13, 2019

Yeah, people should extremely rarely call detectChanges in real code

@Splaktar
Copy link
Member Author

@andrewseguin I agree that the snippet of RxJS that you posted is not an easily readable piece of code as it uses a lot of concepts that may not be well known by every developer. However, that wasn't introduced or changed in this PR. I had considered doing so, but I didn't want to widen the scope of the PR, so I tried my best to keep that intact.

@Splaktar
Copy link
Member Author

Splaktar commented May 13, 2019

@andrewseguin on the topic of client vs server side paging, I agree that this is confusing to developers who just want to try out our schematic and may only be considering one of those options. I commented in #15791 (comment) that we should probably have an option to generate the table with client or server side paging. I also mentioned that we should probably have an option to generate a table w/o paging or sorting. Should I open issues to track that?

@Splaktar
Copy link
Member Author

@crisbeto is also working on a related change to "Rename MatTableDataSource to ClientArrayTableDataSource" (#14378) since as Jeremy noted in the issue, it causes a lot of confusion. The PR is #15432.

@andrewseguin
Copy link
Contributor

What about just removing [length]="dataSource.data.length" and changing the lifecycle hook from ngAfterViewInit to ngOnInit?

@Splaktar
Copy link
Member Author

Splaktar commented May 13, 2019

@andrewseguin if I make only those changes, then the original error from #15788 remains: TypeError: Cannot read property 'page' of undefined due to

When the MatPaginator's length is not defined, it's readonly page: EventEmitter<PageEvent> is also undefined.

@andrewseguin
Copy link
Contributor

Hm, well hopefully we could figure out some better syntax to the whole schematics. For now this looks fine if the other solutions aren't panning out

@andrewseguin andrewseguin added pr: lgtm action: merge The PR is ready for merge by the caretaker labels May 16, 2019
@jelbourn jelbourn merged commit 301371a into master May 17, 2019
@Splaktar Splaktar deleted the table-fixSchematicExceptions branch May 17, 2019 23:52
RudolfFrederiksen pushed a commit to RudolfFrederiksen/material2 that referenced this pull request Jun 21, 2019
…r#15800)

fix exception when instantiating MatPaginator before dataSource defined
fix ExpressionChangedAfterItHasBeenCheckedError caused by above
- first initialize the template w/o dataSource & w/ a sort and paginator
- then get the references to the template's MatSort and MatPaginator
- then set the data source's sort and paginator
- then set the table's data source from the component

Fixes angular#15788
@felikf
Copy link

felikf commented Jun 26, 2019

How to find out which version contains this fix?
I have this one: "@angular/material": "7.3.7"

@Splaktar
Copy link
Member Author

@felikf it's in the commit linked above: 301371a.
8.0.0-rc.2 is the first version with this fix. 8.0.0 is the first stable version with the fix.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement P2 The issue is important to a large percentage of users, with a workaround target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

schematic(table): Cannot read property 'data' of undefined
7 participants