Skip to content

Commit 301371a

Browse files
Splaktarjelbourn
authored andcommitted
fix(table): schematic generates code that throws an exception (#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 #15788
1 parent dc859fe commit 301371a

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

src/material/schematics/ng-generate/table/files/__path__/__name@dasherize@if-flat__/__name@dasherize__-datasource.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ const EXAMPLE_DATA: <%= classify(name) %>Item[] = [
4040
*/
4141
export class <%= classify(name) %>DataSource extends DataSource<<%= classify(name) %>Item> {
4242
data: <%= classify(name) %>Item[] = EXAMPLE_DATA;
43+
paginator: MatPaginator;
44+
sort: MatSort;
4345

44-
constructor(private paginator: MatPaginator, private sort: MatSort) {
46+
constructor() {
4547
super();
4648
}
4749

@@ -59,9 +61,6 @@ export class <%= classify(name) %>DataSource extends DataSource<<%= classify(nam
5961
this.sort.sortChange
6062
];
6163

62-
// Set the paginator's length
63-
this.paginator.length = this.data.length;
64-
6564
return merge(...dataMutations).pipe(map(() => {
6665
return this.getPagedData(this.getSortedData([...this.data]));
6766
}));

src/material/schematics/ng-generate/table/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="mat-elevation-z8">
2-
<table mat-table class="full-width-table" [dataSource]="dataSource" matSort aria-label="Elements">
2+
<table mat-table class="full-width-table" matSort aria-label="Elements">
33
<!-- Id Column -->
44
<ng-container matColumnDef="id">
55
<th mat-header-cell *matHeaderCellDef mat-sort-header>Id</th>
@@ -17,7 +17,7 @@
1717
</table>
1818

1919
<mat-paginator #paginator
20-
[length]="dataSource.data.length"
20+
[length]="dataSource?.data.length"
2121
[pageIndex]="0"
2222
[pageSize]="50"
2323
[pageSizeOptions]="[25, 50, 100, 250]">

src/material/schematics/ng-generate/table/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { AfterViewInit, Component, ViewChild<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%><% if(changeDetection !== 'Default') { %>, ChangeDetectionStrategy<% }%> } from '@angular/core';
1+
import { AfterViewInit, Component, OnInit, ViewChild<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%><% if(changeDetection !== 'Default') { %>, ChangeDetectionStrategy<% }%> } from '@angular/core';
22
import { MatPaginator, MatSort } from '@angular/material';
3-
import { <%= classify(name) %>DataSource } from './<%= dasherize(name) %>-datasource';
3+
import { <%= classify(name) %>DataSource, <%= classify(name) %>Item } from './<%= dasherize(name) %>-datasource';
44

55
@Component({
66
selector: '<%= selector %>',<% if(inlineTemplate) { %>
@@ -15,15 +15,22 @@ import { <%= classify(name) %>DataSource } from './<%= dasherize(name) %>-dataso
1515
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
1616
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>
1717
})
18-
export class <%= classify(name) %>Component implements AfterViewInit {
18+
export class <%= classify(name) %>Component implements AfterViewInit, OnInit {
1919
@ViewChild(MatPaginator) paginator: MatPaginator;
2020
@ViewChild(MatSort) sort: MatSort;
21+
@ViewChild(MatTable) table: MatTable<<%= classify(name) %>Item>;
2122
dataSource: <%= classify(name) %>DataSource;
2223

2324
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
2425
displayedColumns = ['id', 'name'];
2526

27+
ngOnInit() {
28+
this.dataSource = new <%= classify(name) %>DataSource();
29+
}
30+
2631
ngAfterViewInit() {
27-
this.dataSource = new <%= classify(name) %>DataSource(this.paginator, this.sort);
32+
this.dataSource.sort = this.sort;
33+
this.dataSource.paginator = this.paginator;
34+
this.table.dataSource = this.dataSource;
2835
}
2936
}

0 commit comments

Comments
 (0)