我一直试图在我的Material表上实现sort和paginator,但收效甚微。
下面给了我我的表但是当我点击标题时没有排序发生并且整个数据集都被列出,尽管paginator指示当我点击paginator按钮时它应该只有10个结果它自己的状态变化但是数据集未更新以跟随:
import {
AfterViewInit, Component, Input, OnChanges, OnInit, SimpleChanges,
ViewChild
} from '@angular/core';
import {MatSort, MatPaginator, MatTableDataSource} from '@angular/material';
import {SelectionModel} from '@angular/cdk/collections';
import {ApiConnectService} from '../../../../../assets/services/api.connect.service';
@Component({
selector: 'app-review-table',
templateUrl: './review.table.component.html',
styleUrls: ['./review.table.component.scss']
})
export class ReviewTable implements OnInit, AfterViewInit, OnChanges {
@Input() purchaseOrders: Array<object>;
@Input() searchKey: string;
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatPaginator) paginator: MatPaginator;
dataSource: MatTableDataSource<any>;
selection = new SelectionModel<any>(true, null);
displayedColumns = ['rowcount', 'siteId', 'importDate', 'poPoNumber', 'poPrice'];
constructor(private _api: ApiConnectService) { }
ngOnInit() {
this._api.getPurchaseOrders()
.subscribe(data => {
this.dataSource = new MatTableDataSource(data);
}, err => {
console.log(err);
});
}
ngOnChanges(changes: SimpleChanges) {
for (let property in changes) {
if (changes[property].previousValue !== changes[property].currentValue){
if (property === 'searchKey') {
this.applyFilter(this.searchKey);
}
}
}
}
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim();
filterValue = filterValue.toLowerCase();
this.dataSource.filter = filterValue;
}
}
在第二个实现中,我只放置了排序代码,并将我的订阅中的数据集的值设置为我的observable,我还在数据集声明前面第二次添加了@ViewChild(MatSort)
。这个奇怪的实验反对指南https://material2-docs-dev.firebaseapp.com/components/table/examples规定的导致分类按预期工作:
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { ApiConnectService } from '../../../../assets/services/api.connect.service';
import { MatSort, MatTableDataSource } from '@angular/material';
import { SelectionModel } from '@angular/cdk/collections';
@Component({
selector: 'app-menu3',
templateUrl: './purchase.orders.component.html',
styleUrls: ['./purchase.orders.component.scss']
})
export class PurchaseOrders implements OnInit, AfterViewInit {
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatSort) dataSource: MatTableDataSource<any>;
multipleSelect = true;
selection = new SelectionModel<any>(this.multipleSelect, null);
displayedColumns = ['siteId', 'importDate', 'poPoNumber', 'poPrice'];
constructor(private _api: ApiConnectService) {
}
ngOnInit() {
this._api.getPurchaseOrders()
.subscribe(data => {
this.dataSource = new MatTableDataSource(data);
}, err => {
console.log(err);
});
}
ngAfterViewInit() {
this.dataSource.sort = this.sort;
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim();
filterValue = filterValue.toLowerCase();
this.dataSource.filter = filterValue;
}
}
问题是我收到此浏览器控制台错误导致我的浏览器动画被发送到错误的元素 - &gt;我的数据集。该错误说明数据源是错误的类型(我猜?)但随后表格仍然显示和功能:
core.js:1448 ERROR Error: Provided data source did not match an array, Observable, or DataSource
at getTableUnknownDataSourceError (table.es5.js:379)
at MatTable.CdkTable._observeRenderChanges (table.es5.js:860)
at MatTable.CdkTable.ngAfterContentChecked (table.es5.js:577)
at callProviderLifecycles (core.js:12702)
at callElementProvidersLifecycles (core.js:12673)
at callLifecycleHooksChildrenFirst (core.js:12656)
at checkAndUpdateView (core.js:13806)
at callViewAction (core.js:14153)
at execComponentViewsAction (core.js:14085)
at checkAndUpdateView (core.js:13808)
简而言之,这不是解决方案。
我试图用@ViewChild(MatSort) @ViewChild(MatPaginator) dataSource: MatTableDataSource<any>;
加倍我的小屁股,但这种方法毫无结果。它只是忽略了MatPaginator部分。分页器仍然没有链接。
声明表的组件是否必须与API的数据调用相同,或者您是否允许在父级中进行数据调用并通过@Input
将其传递给表?
我在Column-Sort和Pagination中缺少什么才能在后一种情况下工作,我将表作为子项放在检索该数据的组件中?
为什么在强制排序工作时出现此错误?
答案 0 :(得分:3)
您在收到一些异步数据后创建MatTableDataSource。当你做的时候肯定是
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
this.dataSource
可能为null
你能改变吗?
constructor(private _api: ApiConnectService) { }
ngOnInit() {
this._api.getPurchaseOrders()
.subscribe(data => {
this.dataSource = new MatTableDataSource(data);
}, err => {
console.log(err);
});
}
到
constructor(private _api: ApiConnectService) {
this.dataSource = new MatTableDataSource(data); // create MatTableDataSource (synchronous) here to allow sort / filtering binding
}
ngOnInit() {
this._api.getPurchaseOrders()
.subscribe(data => {
this.dataSource.data = data; // populate data here
}, err => {
console.log(err);
});
}