我一直在尝试在Material表上实现sort和paginator,但收效甚微 .
以下给我我的表但是当我点击 Headers 时没有排序发生并且整个数据集都列出尽管paginator指示只有10个结果当我点击paginator按钮它自己的状态更改但数据集没有更新跟随:
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;
}
}
问题是我得到这个浏览器控制台错误导致我的浏览器动画被发送到错误的元素 - >我的数据集 . 该错误说明数据源是错误的类型(我猜?)但随后表格仍然显示和功能:
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的缺失用于将表作为子项放置到检索该数据的组件中?
当我强制Sort工作时,为什么会出现此错误?
1 回答
您在收到一些异步数据后创建MatTableDataSource . 当你做的时候肯定是这样的
this.dataSource
可能为null你能改变吗?
至