Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material ngFor in mat-chip-list - how to prevent from line-break?

issue case in stackblitz

Problem: How to achieve that the mat-chips in angular-material are in one line (as they are as standard), but when looped via ngFor in a mat-chip-list (which is inside a mat-cell), they get placed all on a seperate line (see column "Name").

Goal: I would like to have it in line next to each until break due to width limit (see column "Weight").

chips of comma seprated string elements of name (e.g. A, Z) should be next to each other on line:

    const ELEMENT_DATA: PeriodicElement[] = [
      {position: 1, name: 'A, Z', weight: 1.0079, symbol: 'H'},
      {position: 2, name: 'this first, this second in line', weight: 4.0026, symbol: 'He'},
    ];

as in

    <ng-container matColumnDef="name">
        <th mat-header-cell *matHeaderCellDef> Name </th>
        <td mat-cell *matCellDef="let element">
          <mat-chip-list class="no-wrap" *ngFor="let n of element.name.split(',')">
            <span class="nobreak">
              <mat-chip class="no-wrap">{{n}}</mat-chip>
            </span>
          </mat-chip-list>
        </td>
    </ng-container>

visualisation issue case with angular-material

like image 504
hidatsa Avatar asked Mar 20 '19 21:03

hidatsa


People also ask

How do I disable mat chip list?

Selection. Chips can be selected via the selected property. Selection can be disabled by setting selectable to false on the <mat-chip-list> .

What is angular material contact chips?

The md-contact-chips, an Angular Directive, is an input control built on md-chips and uses the md-autocomplete element. The contact chip component accepts a query expression which returns a list of possible contacts. The user can select one of these and add it to the list of availble chips.


1 Answers

Your *ngFor is on the wrong tag, you have to place it on your <mat-chip>, see below :

    <!-- Name Column -->
    <ng-container matColumnDef="name">
      <th mat-header-cell *matHeaderCellDef> Name </th>
      <td mat-cell *matCellDef="let element">
        <mat-chip-list class="no-wrap" >
            <mat-chip class="no-wrap" *ngFor="let n of element.name.split(',')">{{n}}</mat-chip>
        </mat-chip-list>
      </td>
    </ng-container>

With your code you create a mat-chip-list for each element

like image 70
Sébastien Bousquet Avatar answered Nov 15 '22 04:11

Sébastien Bousquet