Angular Mat-Chips连续显示5

发布于 2025-02-03 01:52:22 字数 1388 浏览 2 评论 0原文

我有来自角质材料的基本垫子。我正在尝试将它们连续将5放置,而不会增加它们的大小:
chip1 chip2 chip3 chip4 chip5 chip5 ....................................................................................................................................................................................................................................................................................................................... chip6 chip7 chip8 chip9 chip9 chip10 ...... chip11 chip12 ............................................................................................................................ ....(剩余空间)。

因此,基本上,我不希望在任何情况下芯片的大小增加。是否有可能使用Flexbox进行此操作?我尝试了一些事情: 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:

.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 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:
Chip1 Chip2 Chip3 Chip4 Chip5 ..................(space left)
Chip6 Chip7 Chip8 Chip9 Chip10 ...............(space left)
Chip11 Chip12 ....................................................(space left).

So basically, I dont want the size of the chips to increase in any circumstances. Is there any possibility to do this with flexbox? I tried something:
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:

.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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离不开的别离 2025-02-10 01:52:22

我认为目前无法使用Pure CSS完成此操作(尽管我可能错了)。如果您知道芯片的大小相同,我想您可以使用CSS网格布局,但是我不认为在这里是这样。我要做的是在每个n个项目(在您的情况下为5)添加一个额外的HTML元素,以迫使休息。

您将必须将*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>

我选择&lt; br/&gt;标签,因为它适合语义,但请选择您。

然后,只需样式.breaker正确:

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

这是工作stackblitz

请记住,此解决方案没有响应 - 例如,在5个芯片无法放入一行的情况下。但是话又说回来,您的要求(每排5芯片)不会留出太大的空间来使其响应能力。我想可以通过测量渲染芯片并使用脚本调整运行的布局来调整它,但这是您必须根据要求自行找出的东西。

I don't think this can be done using pure css at this point (though I might be wrong). 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. What I would do is to add an additional html element every N items (5 in your case) that would force the break.

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):

<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.

Then, just style the .breaker properly:

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

Here's a working stackblitz.

Have in mind that this solution is not responsive - e.g. in a case when 5 chips can't fit in a single row. But then again, your requirements (5 chips per row) doesn't leave much room for making it responsive. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文