Último exemplo, https://material.angular.io/components/table/examples. Mas mudei para buscar da base de dados. As informações vem do banco de dados.
Sempre dá este erro:
EstadoComponent_Host.ngfactory.js? [sm]:1 ERROR TypeError: Cannot set property 'paginator' of undefined
at EstadoComponent.ngAfterViewInit (estado.component.ts:44)
at callProviderLifecycles (core.js:12706)
at callElementProvidersLifecycles (core.js:12673)
at callLifecycleHooksChildrenFirst (core.js:12656)
at checkAndUpdateView (core.js:13811)
at callViewAction (core.js:14153)
at execEmbeddedViewsAction (core.js:14111)
at checkAndUpdateView (core.js:13803)
at callViewAction (core.js:14153)
at execComponentViewsAction (core.js:14085)
Meu código
import { Component, ViewChild, OnInit } from '@angular/core';
import { Http } from '@angular/http';
import { Router } from '@angular/router';
import {MatPaginator, MatSort, MatTableDataSource} from '@angular/material';
import { Estado } from '../model/estado.model';
import { EstadoService } from './estado.service';
@Component({
selector: 'app-estado',
templateUrl: './estado.component.html',
styleUrls: ['./estado.component.css']
})
export class EstadoComponent implements OnInit {
//displayedColumns = ['id', 'name', 'progress', 'color'];
displayedColumns = ['pais', 'descricao', 'sigla'];
dataSource: MatTableDataSource<Estado>;
//dataSource: MatTableDataSource<UserData>;
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
estados: Estado[];
constructor(private router: Router, private estadoService : EstadoService) { }
ngOnInit() {
this.estadoService.todos().forEach(
//subscribe(
(data :any) => {
this.estados = data.lista;
this.dataSource = new MatTableDataSource(this.estados);
});
}
/**
* Set the paginator and sort after the view init since this component will
* be able to query its view for the initialized paginator and sort.
*/
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
this.dataSource.filter = filterValue;
}
}
/** Builds and returns a new User. */
function createNewUser(id: number): UserData {
const name =
NAMES[Math.round(Math.random() * (NAMES.length - 1))] + ' ' +
NAMES[Math.round(Math.random() * (NAMES.length - 1))].charAt(0) + '.';
return {
id: id.toString(),
name: name,
progress: Math.round(Math.random() * 100).toString(),
color: COLORS[Math.round(Math.random() * (COLORS.length - 1))]
};
}
/** Constants used to fill up our data base. */
const COLORS = ['maroon', 'red', 'orange', 'yellow', 'olive', 'green', 'purple',
'fuchsia', 'lime', 'teal', 'aqua', 'blue', 'navy', 'black', 'gray'];
const NAMES = ['Maia', 'Asher', 'Olivia', 'Atticus', 'Amelia', 'Jack',
'Charlotte', 'Theodore', 'Isla', 'Oliver', 'Isabella', 'Jasper',
'Cora', 'Levi', 'Violet', 'Arthur', 'Mia', 'Thomas', 'Elizabeth'];
export interface UserData {
id: string;
name: string;
progress: string;
color: string;
}
function buscarEstado() {
var i : number;
i = 0;
this.estadoService.todos().subscribe(
(data :any) => {
this.estados = data.lista;
});
}