如何在div中制作一个flexbox,以获得2个带有mat-chips/angular material2的ngFor的列?

Rol*_*ndo 8 css flexbox angular-material2 angular

我有一个包含10个条目的水果列表.我想这样做,以便水果名称文本(在垫片旁边)显示在两列中,每列5个.我正在使用的芯片在这里:https://material.angular.io/components/chips/api

我的CSS:

.flexp {
    display: flex;
    flex-wrap:wrap;
}

.flexc {
    flex-grow: 1;
    display: inline-block;
}
Run Code Online (Sandbox Code Playgroud)

HTML

       <div class="flexp" *ngFor="let fruit of fruits">
              <div class="flexc">
                <mat-chip>
                  {{fruit.typeOfFruit}}
                </mat-chip>
                {{fruit.name}}
              </div>
          </div>
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?现在我的flexbox从上到下.

我想制作它看起来像一个表,但不必创建结构.如果有一种方法让ngFor足够聪明,以这种方式显示......这就是我要找的东西:

<table>
<tr>
<td>Fruit 1</td><td>Fruit 6</td>
<td>Fruit 2</td><td>Fruit 7</td>
<td>Fruit 3</td><td>Fruit 8</td>
<td>Fruit 4</td><td>Fruit 9</td>
<td>Fruit 5</td><td>Fruit 10</td></tr>
</table>
Run Code Online (Sandbox Code Playgroud)

如果不可能,请说明.

Dav*_*vid 5

我假设你作为例子给出的表代码是错误的,因为你提到你想要 2 列 5 行,这相当于这个。下面的答案是有一个类似于这里的表格的显示

<table>
<tr><td>Fruit 1</td><td>Fruit 6</td></tr>
<tr><td>Fruit 2</td><td>Fruit 7</td></tr>
<tr><td>Fruit 3</td><td>Fruit 8</td></tr>
<tr><td>Fruit 4</td><td>Fruit 9</td></tr>
<tr><td>Fruit 5</td><td>Fruit 10</td></tr>
</table>
Run Code Online (Sandbox Code Playgroud)

如果要使用flex,需要设置flow-directioncolumn并在容器上有一个max-height,等于一行高度的5倍

scss

$rowHeight: 40px;
.flexp {
    display: flex;
    flex-wrap:wrap;
    flex-direction: column;
    max-height: calc(5 * #{$rowHeight});
}

.flexc {
    height: $rowHeight;
    flex-grow: 1;
    display: inline-block;
}
Run Code Online (Sandbox Code Playgroud)

html

<mat-chip-list>
    <div class="flexp">
     <div class="flexc" *ngFor="let fruit of fruits">

        <mat-chip>
          {{fruit.typeOfFruit}}
        </mat-chip>
        {{fruit.name}}
      </div>
    </div>
</mat-chip-list>
Run Code Online (Sandbox Code Playgroud)

其他不使用 flex 的解决方案

您可以使用column-count属性

scss

$rowHeight: 40px;
.fruitContainer {
    column-count: 2;
    max-height: calc(5 * #{$rowHeight});
}

.fruit {
    height: $rowHeight;
}
Run Code Online (Sandbox Code Playgroud)

html

<mat-chip-list>
    <div class="fruitContainer">
     <div class="fruit" *ngFor="let fruit of fruits">
        <mat-chip>
            {{fruit.typeOfFruit}}
                    </mat-chip>
                    {{fruit.name}}
    </div>

</div>
</mat-chip-list>
Run Code Online (Sandbox Code Playgroud)

Stackblitz 演示

下面是结果

插图


G. *_*ter 3

MatGridList正是出于这个原因而提供的。

<mat-grid-list cols="2" rowHeight="48px">
    <mat-grid-tile *ngFor="let fruit of fruits">
        <div class="flexc">
            <mat-chip>
                {{fruit.typeOfFruit}}
            </mat-chip>
            {{fruit.name}}
        </div>
    </mat-grid-tile>
</mat-grid-list>
Run Code Online (Sandbox Code Playgroud)

请参阅stackblitz 示例