• Grid宫格
    • 规则
  • 代码演示
  • API

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

    Grid 宫格 - 图1

    API

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

    注: Grid 支持ng-content.