Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

html - Arrange <mat-list-item> objects in <mat-list> automatically

The sample code:

<div>
    <mat-list fxLayout="row" dense>
        <mat-list-item *ngFor="let label of labelsList"> <!-- just an array of strings -->
            <button mat-button>
                <mat-icon>cloud</mat-icon>
                {{label}}
            </button>
        </mat-list-item>
    </mat-list>
</div>

The result: Initial result


When I resize the browser window: Result of mat-list after resize


What I need: the buttons that don't "have room" to simply go on the next row.

How do I achieve this? I tried several combinations of CSS classes, properties, etc... nothing worked.

LATER EDIT: here's a complete reproducible example: https://angular-svt72k.stackblitz.io

question from:https://stackoverflow.com/questions/65626435/arrange-mat-list-item-objects-in-mat-list-automatically

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

mat-list-item is by default using the full width of its container and setting each item as display: block. To overrule this, you need to override the default Angular (Material) styling that comes with <mat-list>.

Setting .mat-list-test to display: flex and adding flex-flow: row wrap will make it go to the next line when there's not enough space available. Next to that, as said, the .mat-list-item styling is taking the full width. You can override it by setting display: initial and width: auto. Read more about flexbox at MDN.

CSS

.mat-list-test {
  display: flex;
  flex-flow: row wrap;
}

.mat-list-base .mat-list-item.mat-list-item-test {
  display: initial;
  width: auto;
}

Example of working mat-list

See this example on StackBlitz to show the outcome.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...