【发布时间】:2020-08-26 22:57:19
【问题描述】:
我正在尝试创建一个带有排序和分页的表格。当我试图更改应该在一页上呈现的元素数量时,分页器的行为方式很奇怪。如果我单击下拉列表,它会在表格下方呈现下拉列表的内容。 (见截图)(如果我点击 5,10,25 或 100,它会选择正确的数字并设置分页器,所以功能是给定的)
代码:
github-repo:https://github.com/Pethaudi/testing-mat-table
app.module:
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
NoopAnimationsModule,
MatTableModule,
MatSortModule,
MatPaginatorModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component:
export interface PeriodicElement {
position: number;
}
const ELEMENT_DATA: PeriodicElement[] = [
{ position: 1 },
{ position: 2 },
{ position: 3 },
{ position: 4 },
{ position: 5},
{ position: 6 },
{ position: 7},
{ position: 8 },
{ position: 9 },
{ position: 10},
];
@Component({
selector: 'app-root',
template: `
<div id="body" class="mat-elevation-z8">
<mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>
<table mat-table [dataSource]="dataSource" matSort>
<!-- Position Column -->
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef mat-sort-header appChange> No. </th>
<td mat-cell *matCellDef="let element"> {{element.position}}
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
</div>`
})
export class AppComponent {
@ViewChild(MatPaginator, {static: true})
paginator: MatPaginator;
@ViewChild(MatSort, {static: true })
sort: MatSort;
displayedColumns: string[] = ['position'];
dataSource = new MatTableDataSource();
ngOnInit() {
this.dataSource = new MatTableDataSource(ELEMENT_DATA);
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
}
}
依赖:
{
"@angular/animations": "~9.1.1",
"@angular/cdk": "^9.2.3",
"@angular/common": "~9.1.1",
"@angular/compiler": "~9.1.1",
"@angular/core": "~9.1.1",
"@angular/forms": "~9.1.1",
"@angular/material": "^9.2.3",
"@angular/platform-browser": "~9.1.1",
"@angular/platform-browser-dynamic": "~9.1.1",
"@angular/router": "~9.1.1",
"rxjs": "~6.5.4",
"tslib": "^1.10.0",
"zone.js": "~0.10.2"
}
谢谢!
【问题讨论】:
-
请提供一份工作重现
-
我真的很想提供一个 stackblitz,但我无法让它运行。 (ngcc 错误)这是我走了多远:stackblitz.com/edit/angular-ivy-elcvpx
-
这里是 github 仓库:github.com/Pethaudi/testing-mat-table
-
我设法复制了它,但它在 stackblitz 上运行良好:stackblitz.com/edit/angular-5xtrnz?file=src/app/…
-
哦,哇,您的解决方案有效,我认为某处存在依赖问题。尽管如此,还是感谢您的帮助!
标签: angular angular-material angular9 angular-material-table angular-material-paginator