0

I have a mat-list in an Angular component like below (food-list.component.html)

sample mat list UI

<mat-list id="site-list">
     <ng-container *cdkVirtualFor="let food of foods">
        <mat-list-item  id="food-{{food.id}}">
          <mat-icon *ngIf="showFoodIcon" color="warn">not_listed_location</mat-icon>
          <p matLine [ngStyle]="{width: foodLabelWidth} class="overflowText"><small>{{food.label}}</small></p>
          <p matLine [ngStyle]="{width: foodLabelWidth} class="overflowText"><small>{{food.type}}</small></p>
       </mat-list-item>
    </ng-container>
</mat-list>

And in CSS file (food-list.component.scss)

p.overflowText {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

As example in a desktop view mat list item width 100px and, MatIconWidth is 5px. and in mobile view mat list item width 50px and, MatIconWidth is 5px. I want to set text overflow width according to the space that left.

for example, in food-list.component.ts I want to do something like this.

ngOnInit(): void {
   this.foodLabelWidth= matListItemWidth - MatIconWidth;
}

to make it happen I want to get DOM element width of

    <mat-icon *ngIf="showFoodIcon" color="warn">not_listed_location</mat-icon>

Which lies inside a NgFor loop. Is there a way to achieve this?

bguiz
  • 27,371
  • 47
  • 154
  • 243
  • What is the desired end result? I am guessing to get `mat-list-item` labels to truncate `...` using ellipsis at some fixed width? I would recommend using mediaQuery instead. You can't width of `matLine` and then assign itself it's own width. – Joosep Parts Mar 28 '22 at 05:13
  • true that's exactly what I want to do. – Nimash Dilanka Mar 28 '22 at 05:18

1 Answers1

0

So I could use flex design to make it work !! https://css-tricks.com/snippets/css/a-guide-to-flexbox/

in html file

<mat-list id="site-list">
     <ng-container *cdkVirtualFor="let food of foods">
        <mat-list-item  id="food-{{food.id}}">
          <div class="flex-row w-100">
              <div class="flex-row">
                <mat-icon *ngIf="showFoodIcon" color="warn">not_listed_location</mat-icon>
              </div>
              <div class="food-title">
                  <p matLine class="overflowText"><small>{{food.label}}</small></p>
                  <p matLine class="overflowText"><small>{{food.type}}</small></p>
              </div>
          </div>
       </mat-list-item>
    </ng-container>
</mat-list>

scss file

::ng-deep .cdk-virtual-scroll-content-wrapper {
  max-width: 100%
}

.flex-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.overflow {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.food-title {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 29 '22 at 05:25