Skip to content

fix(table): use static queries for examples #15483

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

Merged
merged 1 commit into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/material-examples/table-overview/table-overview-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ export interface UserData {
}

/** Constants used to fill up our data base. */
const COLORS: string[] = ['maroon', 'red', 'orange', 'yellow', 'olive', 'green', 'purple',
'fuchsia', 'lime', 'teal', 'aqua', 'blue', 'navy', 'black', 'gray'];
const NAMES: string[] = ['Maia', 'Asher', 'Olivia', 'Atticus', 'Amelia', 'Jack',
'Charlotte', 'Theodore', 'Isla', 'Oliver', 'Isabella', 'Jasper',
'Cora', 'Levi', 'Violet', 'Arthur', 'Mia', 'Thomas', 'Elizabeth'];
const COLORS: string[] = [
'maroon', 'red', 'orange', 'yellow', 'olive', 'green', 'purple', 'fuchsia', 'lime', 'teal',
'aqua', 'blue', 'navy', 'black', 'gray'
];
const NAMES: string[] = [
'Maia', 'Asher', 'Olivia', 'Atticus', 'Amelia', 'Jack', 'Charlotte', 'Theodore', 'Isla', 'Oliver',
'Isabella', 'Jasper', 'Cora', 'Levi', 'Violet', 'Arthur', 'Mia', 'Thomas', 'Elizabeth'
];

/**
* @title Data table with sorting, pagination, and filtering.
Expand All @@ -27,8 +30,8 @@ export class TableOverviewExample implements OnInit {
displayedColumns: string[] = ['id', 'name', 'progress', 'color'];
dataSource: MatTableDataSource<UserData>;

@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we move the usages to ngAfterViewInit, rather than marking them as static? Since it's an example it won't be a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue with the table is that it needs the paginator and sort to figure out the initially rendered rows before initialization. If we wait until after view init, the table will initially render all rows.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, although it's a little unfortunate that we have to require people to use static queries.

@ViewChild(MatSort, {static: true}) sort: MatSort;

constructor() {
// Create 100 users
Expand All @@ -54,8 +57,7 @@ export class TableOverviewExample implements OnInit {

/** Builds and returns a new User. */
function createNewUser(id: number): UserData {
const name =
NAMES[Math.round(Math.random() * (NAMES.length - 1))] + ' ' +
const name = NAMES[Math.round(Math.random() * (NAMES.length - 1))] + ' ' +
NAMES[Math.round(Math.random() * (NAMES.length - 1))].charAt(0) + '.';

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class TablePaginationExample implements OnInit {
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);

@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;

ngOnInit() {
this.dataSource.paginator = this.paginator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {Component, Input, OnDestroy, OnInit, Optional, ViewChild} from '@angular/core';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {Component, Input, OnDestroy, OnInit, Optional, ViewChild} from '@angular/core';
import {
MatColumnDef,
MatSort,
MatSortHeader,
MatTable,
MatTableDataSource
MatColumnDef,
MatSort,
MatSortHeader,
MatTable,
MatTableDataSource
} from '@angular/material';

export interface PeriodicElement {
Expand Down Expand Up @@ -41,7 +41,7 @@ export class TableSimpleColumnExample implements OnInit {
dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);
getWeight = (data: PeriodicElement) => '~' + data.weight;

@ViewChild('sort') sort: MatSort;
@ViewChild('sort', {static: true}) sort: MatSort;

ngOnInit() {
this.dataSource.sort = this.sort;
Expand Down Expand Up @@ -75,7 +75,9 @@ export class TableSimpleColumnExample implements OnInit {
export class SimpleColumn<T> implements OnDestroy, OnInit {
/** Column name that should be used to reference this column. */
@Input()
get name(): string { return this._name; }
get name(): string {
return this._name;
}
set name(name: string) {
this._name = name;
this.columnDef.name = name;
Expand All @@ -96,21 +98,23 @@ export class SimpleColumn<T> implements OnDestroy, OnInit {
@Input() dataAccessor: ((data: T, name: string) => string);

/** Alignment of the cell values. */
@Input() align: 'before' | 'after' = 'before';
@Input() align: 'before'|'after' = 'before';

/** Whether the column is sortable */
@Input()
get sortable(): boolean { return this._sortable; }
get sortable(): boolean {
return this._sortable;
}
set sortable(sortable: boolean) {
this._sortable = coerceBooleanProperty(sortable);
}
_sortable: boolean;

@ViewChild(MatColumnDef) columnDef: MatColumnDef;
@ViewChild(MatColumnDef, {static: true}) columnDef: MatColumnDef;

@ViewChild(MatSortHeader) sortHeader: MatSortHeader;

constructor(@Optional() public table: MatTable<any>) { }
constructor(@Optional() public table: MatTable<any>) {}

ngOnInit() {
if (this.table) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class TableSortingExample implements OnInit {
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource = new MatTableDataSource(ELEMENT_DATA);

@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatSort, {static: true}) sort: MatSort;

ngOnInit() {
this.dataSource.sort = this.sort;
Expand Down
8 changes: 4 additions & 4 deletions src/material-examples/table-wrapped/table-wrapped-example.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {DataSource} from '@angular/cdk/collections';
import {
AfterContentInit,
Component,
Expand All @@ -15,7 +16,6 @@ import {
MatTable,
MatTableDataSource
} from '@angular/material';
import {DataSource} from '@angular/cdk/collections';

export interface PeriodicElement {
name: string;
Expand Down Expand Up @@ -49,7 +49,7 @@ export class TableWrappedExample implements OnInit {
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);

@ViewChild('sort') sort: MatSort;
@ViewChild('sort', {static: true}) sort: MatSort;

ngOnInit() {
this.dataSource.sort = this.sort;
Expand All @@ -70,11 +70,11 @@ export class TableWrappedExample implements OnInit {
`]
})
export class WrapperTable<T> implements AfterContentInit {
@ContentChildren(MatHeaderRowDef) headerRowDefs: QueryList<MatHeaderRowDef>;
@ContentChildren(MatHeaderRowDef) headerRowDefs: QueryList<MatHeaderRowDef>;
@ContentChildren(MatRowDef) rowDefs: QueryList<MatRowDef<T>>;
@ContentChildren(MatColumnDef) columnDefs: QueryList<MatColumnDef>;

@ViewChild(MatTable) table: MatTable<T>;
@ViewChild(MatTable, {static: true}) table: MatTable<T>;

@Input() columns: string[];

Expand Down