【问题标题】:Angular 2 *ngFor with algorithmAngular 2 *ngFor 与算法
【发布时间】:2017-03-13 08:53:28
【问题描述】:

我的网站上有一个类似“砌体”的网格,我使用 *ngFor 显示它。 网格块如下所示:

T T T
S A S
T S T

其中 T - 代表高块,S - 短块,A - 具有高块高度的特殊部分。

我正在尝试创建一个网格,该网格将使用 ngFor 但使用网格架构自动填充。

例如:

T T T
S T S
T S T

并继续遵循格式 ->

T T T
S T S
T S T
T T T
S T S
T S T
T T T
S T S
T S T

以此类推,以前三行为例,仅由类指定高或短块。所以我的问题是,检查所有这些条件以便以下列格式显示它们的最佳方法是什么?

【问题讨论】:

    标签: algorithm angular ngfor


    【解决方案1】:

    要获得您想要的效果,您需要利用 css 创建一个masonry layout

    最好的方法是使用 css column-count,但您需要转置数组,以便数据的每一行代表一列。

    此示例利用 ngClass 提供一个对象,其中键是 CSS 类,当值中给出的表达式计算为 true 时,这些类会被添加。

    ma​​sonry.component.ts

    export class MasonryComponent {
        wall = [['T', 'T', 'T'], ['S', 'A', 'S'], ['T', 'S', 'T']];
    
        constructor() { }
    
        transpose(arr: any[]){
            return arr.map((col, i) => arr.map((row)  => row[i] ));
        }
    }
    

    ma​​sonry.html

    <div class="wall" >
        <ng-container *ngFor="let blockCol of transpose(wall)">
            <div *ngFor="let block of blockCol" class="block" [ngClass]="{ 
                'tall' : block === 'T', 
                'short': block === 'S', 
                'special': block === 'A'  }">{{ block }}</div>
        </ng-container>
    </div>
    

    ma​​sonry.css

    .wall {
        width: 170px;
        column-count: 3;
    }
    
    .block {
        width: 50px;
        border: 1px solid black;
    }
    
    .tall {     height: 50px; }
    .special {  height: 50px; }
    .short {    height: 25px; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      • 2016-07-14
      • 2017-03-11
      • 2018-10-13
      • 2018-06-01
      相关资源
      最近更新 更多