在 angular material 中显示多个网格
Displaying multiple grids in angular material
我正在尝试在一个页面中显示 8 个小网格。每个网格有 2 列和
4 行,每个单元格大约 2 个单词。
<mat-grid-list cols="2">
<mat-grid-tile>
hello world
</mat-grid-tile>
<mat-grid-tile>
hello world
</mat-grid-tile>
<mat-grid-tile>
hello world
</mat-grid-tile>
<mat-grid-tile>
hello world
</mat-grid-tile>
<mat-grid-tile>
hello world
</mat-grid-tile>
<mat-grid-tile>
hello world
</mat-grid-tile>
<mat-grid-tile>
hello world
</mat-grid-tile>
<mat-grid-tile>
hello world
</mat-grid-tile>
</mat-grid-list>
我对如何以令人愉悦的方式排列它们的其他建议持开放态度,但我的想法是:
- 如果有足够的space一行放4个,分2行显示:4+4
- 否则如果有足够的space在一行中显示 3 个,则将它们显示在 3 行上:3+3+2
- 否则如果有足够的space在一行中显示 2 个,则将它们显示在 4 行上:2+2+2+2
- 否则显示在 8 行上
我真的不知道该怎么做,这个设置和条件的正确方法是什么?
我什至无法在一行中显示 2 个网格,似乎默认情况下一个网格占据了所有可用的页面宽度。
您可以使用 FlexLayout 模块来监听媒体变化并根据屏幕尺寸设置列数。然后将 cols
值绑定到列数变量。 Here is a Stackblitz example.
HTML:
<mat-grid-list [cols]="columns" rowHeight="2:1">
<mat-grid-tile>hello world</mat-grid-tile>
<mat-grid-tile>hello world</mat-grid-tile>
<mat-grid-tile>hello world</mat-grid-tile>
<mat-grid-tile>hello world</mat-grid-tile>
<mat-grid-tile>hello world</mat-grid-tile>
<mat-grid-tile>hello world</mat-grid-tile>
<mat-grid-tile>hello world</mat-grid-tile>
<mat-grid-tile>hello world</mat-grid-tile>
</mat-grid-list>
TS:
import { Component, OnDestroy } from '@angular/core';
import { MediaChange, ObservableMedia } from '@angular/flex-layout';
import { Subscription } from 'rxjs';
/**
* @title Flexible grid-list
*/
@Component({
selector: 'grid-list-flexible-example',
styleUrls: ['grid-list-flexible-example.css'],
templateUrl: 'grid-list-flexible-example.html',
})
export class GridListFlexibleExample implements OnDestroy {
watcher: Subscription;
columns: number = 4;
constructor(media: ObservableMedia) {
this.watcher = media.subscribe((change: MediaChange) => {
if (change) {
if (change.mqAlias == 'xs') {
this.columns = 2;
} else if (change.mqAlias == 'sm') {
this.columns = 3;
} else {
this.columns = 4;
}
}
});
}
ngOnDestroy() {
this.watcher.unsubscribe();
}
}
我正在尝试在一个页面中显示 8 个小网格。每个网格有 2 列和
4 行,每个单元格大约 2 个单词。
<mat-grid-list cols="2">
<mat-grid-tile>
hello world
</mat-grid-tile>
<mat-grid-tile>
hello world
</mat-grid-tile>
<mat-grid-tile>
hello world
</mat-grid-tile>
<mat-grid-tile>
hello world
</mat-grid-tile>
<mat-grid-tile>
hello world
</mat-grid-tile>
<mat-grid-tile>
hello world
</mat-grid-tile>
<mat-grid-tile>
hello world
</mat-grid-tile>
<mat-grid-tile>
hello world
</mat-grid-tile>
</mat-grid-list>
我对如何以令人愉悦的方式排列它们的其他建议持开放态度,但我的想法是:
- 如果有足够的space一行放4个,分2行显示:4+4
- 否则如果有足够的space在一行中显示 3 个,则将它们显示在 3 行上:3+3+2
- 否则如果有足够的space在一行中显示 2 个,则将它们显示在 4 行上:2+2+2+2
- 否则显示在 8 行上
我真的不知道该怎么做,这个设置和条件的正确方法是什么?
我什至无法在一行中显示 2 个网格,似乎默认情况下一个网格占据了所有可用的页面宽度。
您可以使用 FlexLayout 模块来监听媒体变化并根据屏幕尺寸设置列数。然后将 cols
值绑定到列数变量。 Here is a Stackblitz example.
HTML:
<mat-grid-list [cols]="columns" rowHeight="2:1">
<mat-grid-tile>hello world</mat-grid-tile>
<mat-grid-tile>hello world</mat-grid-tile>
<mat-grid-tile>hello world</mat-grid-tile>
<mat-grid-tile>hello world</mat-grid-tile>
<mat-grid-tile>hello world</mat-grid-tile>
<mat-grid-tile>hello world</mat-grid-tile>
<mat-grid-tile>hello world</mat-grid-tile>
<mat-grid-tile>hello world</mat-grid-tile>
</mat-grid-list>
TS:
import { Component, OnDestroy } from '@angular/core';
import { MediaChange, ObservableMedia } from '@angular/flex-layout';
import { Subscription } from 'rxjs';
/**
* @title Flexible grid-list
*/
@Component({
selector: 'grid-list-flexible-example',
styleUrls: ['grid-list-flexible-example.css'],
templateUrl: 'grid-list-flexible-example.html',
})
export class GridListFlexibleExample implements OnDestroy {
watcher: Subscription;
columns: number = 4;
constructor(media: ObservableMedia) {
this.watcher = media.subscribe((change: MediaChange) => {
if (change) {
if (change.mqAlias == 'xs') {
this.columns = 2;
} else if (change.mqAlias == 'sm') {
this.columns = 3;
} else {
this.columns = 4;
}
}
});
}
ngOnDestroy() {
this.watcher.unsubscribe();
}
}