Grid宫格

在水平和垂直方向,将布局切分成若干等大的区块。

规则

  • 区块中的内容应该是同类元素,eg:都是图片,或者都是图标+文字。

代码演示

基本用法

最简单的用法。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-grid-basic',
  4. template: `
  5. <div class="sub-title">Always square grid item</div>
  6. <Grid [activeStyle]="false" [data]="data" (onClick)="click($event)"></Grid>
  7. <br />
  8. <Grid [activeStyle]="false" [data]="dataList" (onClick)="click($event)"></Grid>
  9. <br />
  10. <div class="sub-title">Grid item adjust accroiding to img size</div>
  11. <Grid class="not-square-grid" [data]="data" [square]="false" (onClick)="click($event)"></Grid>
  12. <br />
  13. <div class="sub-title">ColumnNum=3</div>
  14. <Grid [data]="data" [columnNum]="3" (onClick)="click($event)"></Grid>
  15. <br />
  16. <div class="sub-title">No border</div>
  17. <Grid [data]="data" [hasLine]="false" (onClick)="click($event)"></Grid>
  18. <br />
  19. <div class="sub-title">Carousel</div>
  20. <Grid [data]="data" [isCarousel]="true" (onClick)="click($event)"></Grid>
  21. <br />
  22. <div class="sub-title">Custom content</div>
  23. <Grid
  24. [data]="data"
  25. [columnNum]="3"
  26. [itemStyle]="{ height: '150px', background: 'rgba(0,0,0,.05)' }"
  27. (onClick)="click($event)"
  28. ></Grid>
  29. <br />
  30. <div class="sub-title">ng-content</div>
  31. <Grid>
  32. <Flex *ngFor="let item of gridData; let i = index" [justify]="'center'" [align]="'stretch'">
  33. <FlexItem *ngFor="let subItem of item; let j = index">
  34. <div *ngIf="subItem !== null" class="am-grid-item-content" (click)="click(subItem, i * columnNum + j)">
  35. <div class="am-grid-item-inner-content column-num-{{ columnNum }}">
  36. <img src="{{ subItem.icon }}" class="am-grid-icon" />
  37. <div class="am-grid-text">{{ subItem.text }}</div>
  38. </div>
  39. </div>
  40. <div *ngIf="subItem === null" class="am-grid-null-item"></div>
  41. </FlexItem>
  42. </Flex>
  43. </Grid>
  44. `,
  45. styles: [
  46. `
  47. .sub-title {
  48. color: #888;
  49. font-size: 14px;
  50. padding: 15px 0 9px 15px;
  51. }
  52. /deep/.not-square-grid .am-grid-icon {
  53. width: 40px;
  54. height: 60px;
  55. }
  56. `
  57. ]
  58. })
  59. export class DemoGridBasicComponent {
  60. gridData = [];
  61. data = Array.from(new Array(9)).map((_val, i) => ({
  62. icon: '/assets/img/logo.svg',
  63. text: `name${i}`
  64. }));
  65. data1 = Array.from(new Array(9)).map(() => ({
  66. icon: 'https://gw.alipayobjects.com/zos/rmsportal/WXoqXTHrSnRcUwEaQgXJ.png'
  67. }));
  68. dataList = Array.from(new Array(9)).map((_val, i) => ({
  69. icon: `<img src="/assets/img/logo.svg" style="width:36px"/>`,
  70. text: `name${i}`
  71. }));
  72. click(event) {
  73. console.log(event);
  74. }
  75. constructor() {
  76. this.init();
  77. }
  78. init() {
  79. const dataLength = (this.data && this.data.length) || 0;
  80. let rowCount = Math.ceil(dataLength / 3);
  81. this.gridData = this.getRows(rowCount, dataLength);
  82. }
  83. getRows(rowCount: number, dataLength: number) {
  84. const columnNum = 3;
  85. const rowArr = new Array();
  86. for (let i = 0; i < rowCount; i++) {
  87. rowArr[i] = new Array();
  88. for (let j = 0; j < columnNum; j++) {
  89. const dataIndex = i * columnNum + j;
  90. if (dataIndex < dataLength) {
  91. rowArr[i][j] = this.data[dataIndex];
  92. } else {
  93. rowArr[i][j] = null;
  94. }
  95. }
  96. }
  97. return rowArr;
  98. }
  99. }

Grid宫格 - 图1

API

属性说明类型默认值
[data]传入的菜单数据Array<{icon: string, text: string}>[]
[columnNum]列数number4
[hasLine]是否有边框booleantrue
[isCarousel]是否跑马灯,booleanfalse
[carouselMaxRow]如果是跑马灯, 一页跑马灯需要展示的行数number2
[square]每个格子是否固定为正方形booleantrue
[itemStyle]每个格子自定义样式{}
[activeStyle]是否显示点击状态booleantrue
(onClick)点击每个菜单的回调函数EventEmitter<{data: object, index: number}>-

注: Grid 支持ng-content.