简体   繁体   English

Angular mat-chips 连续显示 5 个

[英]Angular mat-chips displays 5 on a row

I have the basic mat-chips from angular material.我有角材料的基本垫片。 What I'm trying is to put them 5 on a row, without their size to increase:我正在尝试将它们连续放置 5 个,而不增加它们的大小:
Chip1 Chip2 Chip3 Chip4 Chip5 ..................(space left)芯片 1 芯片 2 芯片 3 芯片 4 芯片 5 ........(左侧空间)
Chip6 Chip7 Chip8 Chip9 Chip10 ...............(space left)芯片 6 芯片 7 芯片 8 芯片 9 芯片 10 ......(左空间)
Chip11 Chip12 ....................................................(space left).芯片 11 芯片 12 ................................... ....(左侧空间)。

So basically, I dont want the size of the chips to increase in any circumstances.所以基本上,我不希望芯片的大小在任何情况下都增加。 Is there any possibility to do this with flexbox?有没有可能用 flexbox 做到这一点? I tried something: HTML:我尝试了一些东西:HTML:

<div class='list'>
   <mat-form-field class="example-chip-list" appearance="fill">
  <mat-label>Favorite Fruits</mat-label>
  <mat-chip-list #chipList aria-label="Fruit selection">
    <mat-chip *ngFor="let fruit of fruits" id='item' (removed)="remove(fruit)">
      {{fruit.name}}
      <button matChipRemove>
        <mat-icon>cancel</mat-icon>
      </button>
    </mat-chip>
  </mat-chip-list>
</mat-form-field>
</div>

CSS: CSS:

.list{
 width: 100%;
 flex-flow: row wrap;
}

#item{
flex: 1 0 20%; //this is not working because chip's size is increasing to take full width of the div
}

I don't think this can be done using pure css at this point (though I might be wrong).我认为此时不能使用纯 css 来完成(尽管我可能错了)。 If you knew your chips are all of the same size, I suppose you could go with css grid layout, but I don't believe that's the case here.如果您知道您的芯片大小都相同,我想您可以使用 css 网格布局,但我不相信这里是这种情况。 What I would do is to add an additional html element every N items (5 in your case) that would force the break.我要做的是每 N 个项目(在你的情况下为 5 个)添加一个额外的 html 元素,这将强制中断。

You would have to move the *ngFor to a higher level (use ng-container to not generate extra markup) and use it's index to calculate whether an extra item should be inserted or not (in your case after every 5th chip):您必须将*ngFor移动到更高级别(使用ng-container不生成额外标记)并使用它的索引来计算是否应插入额外项目(在您的情况下,在每 5 个芯片之后):

<div class="list">
  <mat-form-field class="example-chip-list" appearance="fill">
    <mat-label>Favorite Fruits</mat-label>
    <mat-chip-list #chipList aria-label="Fruit selection">
      <ng-container *ngFor="let fruit of fruits; let idx = index;">
        <mat-chip id="item" (removed)="remove(fruit)">
          {{fruit.name}}
          <button matChipRemove>
            <mat-icon>cancel</mat-icon>
          </button>
        </mat-chip>
        <br class="breaker" *ngIf="(idx+1)%5===0" />
      </ng-container>
    </mat-chip-list>
  </mat-form-field>
</div>

I went with <br/> tag since it fits semantically, but take your pick.我选择了<br/>标签,因为它符合语义,但请自行选择。

Then, just style the .breaker properly:然后,只需正确设置.breaker的样式:

.breaker {
  width:100%;
  content: '';
}

Here's a working stackblitz .这是一个有效的 stackblitz

Have in mind that this solution is not responsive - eg in a case when 5 chips can't fit in a single row.请记住,此解决方案没有响应 - 例如,当 5 个芯片不能放在一行中时。 But then again, your requirements (5 chips per row) doesn't leave much room for making it responsive.但是话又说回来,您的要求(每行 5 个芯片)并没有留下太多空间来使其响应。 I suppose it could be adapted by measuring the rendered chips and adjusting the layout on the run using script, but that's something you'd have to figure out on your own based on the requirements.我想它可以通过测量渲染的芯片并使用脚本调整运行时的布局来进行调整,但这是您必须根据要求自己弄清楚的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM