我可以删除我的标签页生成的角质材料仅在移动??
我正在研究角质6并使用最新版本的材料。
谢谢!
发布于 2018-11-09 07:43:30
您可以使用CDK布局检测屏幕大小,并将屏幕大小与角度材质断点内联起来,以便有条件地设置MatTableDataSource
分页器。本例假设您希望设置600 px的移动“断点”,但您可以根据需要的任何px宽度进行调整:
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator, MatTableDataSource } from '@angular/material';
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
import { tap } from 'rxjs/operators';
@Component({
selector: 'table-pagination-example',
styleUrls: ['table-pagination-example.css'],
templateUrl: 'table-pagination-example.html',
})
export class TablePaginationExample {
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);
@ViewChild(MatPaginator) paginator: MatPaginator;
isLargeScreen: boolean = false;
constructor(private breakpointObserver: BreakpointObserver) { }
ngOnInit() {
this.breakpointObserver.observe([
'(min-width: 600px)'
]).pipe(
tap(result => this.isLargeScreen = result.matches)
).subscribe(result => {
if (result.matches) {
this.dataSource.paginator = this.paginator;
} else {
this.dataSource.paginator = null;
}
});
}
}
然后使用条件式mat-paginator
使用ngStyle
。我们使用ngStyle
,而不是像*ngIf
这样的东西,因为这可能会影响ViewChild
查找MatPaginator
<mat-paginator [ngStyle]="{display: isLargeScreen ? 'block' : 'none'}" [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
下面是一个示例,演示仅当媒体为、Tablet、或Web时,才激活相应MatTableDataSource
的分页器。
希望这能帮上忙!
https://stackoverflow.com/questions/53228103
复制