<mat-select> 不够 space。如何添加多行<mat-option>?
not enough space in <mat-select>. How to add multiple line <mat-option>?
我想在选项文本下添加描述选项的行。
默认情况下 mat-select 限制字符数并在行尾添加“...”。我想要需要的多行选项。 stakckblitz 演示:https://stackblitz.com/edit/angular-wj9svw
我的代码:
export class SelectOverviewExample {
foods: Food[] = [
{value: 'steak-0', viewValue: 'Steak longDescription longDescription longDescription longDescription longDescription'},
{value: 'pizza-1', viewValue: 'Pizza longDescription longDescription longDescription longDescription longDescription longDescription v v longDescription'},
{value: 'tacos-2', viewValue: 'Tacos'}
];
}
html:
<mat-form-field>
<mat-select placeholder="Favorite food">
<mat-option *ngFor="let food of foods" [value]="food.value">
{{food.viewValue}}
<b> some additional text text text text texttext </b>
</mat-option>
</mat-select>
</mat-form-field>
使用以下 css :
.mat-select-panel mat-option.mat-option {
height: unset;
}
.mat-option-text.mat-option-text {
white-space: normal;
}
或
/deep/ .mat-select-panel mat-option.mat-option {
height: unset;
}
/deep/ .mat-option-text.mat-option-text {
white-space: normal;
}
示例演示在这里 - https://stackblitz.com/edit/angular-material2-issue-ndtstg
.mat-option {
margin: 1rem 0;
overflow: visible;
line-height: initial;
word-wrap: break-word;
white-space: pre-wrap;
}
.mat-option i {
display: block;
font-size: 1.5rem;
opacity: 0.6;
margin-left: 0.5rem;
}
这也很有帮助
我通过编写 MultiLineOptionDirective
来解决 <mat-option>
并调用 MatListItem
和 MatGridTile
从 [=16= 执行的相同 setLines()
函数]
<mat-form-field>
<mat-label>Favorite food</mat-label>
<mat-select [formControl]="mySelect">
<mat-select-trigger>{{ mySelect.value }}</mat-select-trigger>
<mat-option *ngFor="let food of foods" [value]="food.value" multiLineOption>
<h4 mat-line>{{ food.value }}</h4>
<p mat-line>{{ food.description }}</p>
</mat-option>
</mat-select>
</mat-form-field>
https://stackblitz.com/edit/angular-material-multi-line-option
编辑:“extends”可能会被误解,extends 作为指令扩展元素的功能而不是 class extends a base class.
我用了下面的CSS.
::ng-deep .mat-select-panel mat-option.mat-option {
height: unset;
line-height: 1.2em;
padding-top: 0.9em;
padding-bottom: 0.9em;
}
::ng-deep .mat-option-text.mat-option-text {
white-space: normal;
}
我最近遇到了这个问题,经过一些调试我发现你可以引用与 .
关联的 class
.mat-option {
white-space: normal;
line-height: normal;
}
这会将文本换行到下一行并相应地调整高度。如果您正在处理动态数据,也可以执行 line-height: auto
。
这显示了 2 列中的长文本。
在 .scss 中添加:(如果您希望每行具有相同的高度,您可以删除最后 3 行)
.multiline-mat-option.mat-option {
white-space: normal;
line-height: normal;
height: unset;
padding-top: 0.9em;
padding-bottom: 0.9em;
}
在Html中:
<mat-option class="multiline-mat-option"...
我想在选项文本下添加描述选项的行。 默认情况下 mat-select 限制字符数并在行尾添加“...”。我想要需要的多行选项。 stakckblitz 演示:https://stackblitz.com/edit/angular-wj9svw 我的代码:
export class SelectOverviewExample {
foods: Food[] = [
{value: 'steak-0', viewValue: 'Steak longDescription longDescription longDescription longDescription longDescription'},
{value: 'pizza-1', viewValue: 'Pizza longDescription longDescription longDescription longDescription longDescription longDescription v v longDescription'},
{value: 'tacos-2', viewValue: 'Tacos'}
];
}
html:
<mat-form-field>
<mat-select placeholder="Favorite food">
<mat-option *ngFor="let food of foods" [value]="food.value">
{{food.viewValue}}
<b> some additional text text text text texttext </b>
</mat-option>
</mat-select>
</mat-form-field>
使用以下 css :
.mat-select-panel mat-option.mat-option {
height: unset;
}
.mat-option-text.mat-option-text {
white-space: normal;
}
或
/deep/ .mat-select-panel mat-option.mat-option {
height: unset;
}
/deep/ .mat-option-text.mat-option-text {
white-space: normal;
}
示例演示在这里 - https://stackblitz.com/edit/angular-material2-issue-ndtstg
.mat-option {
margin: 1rem 0;
overflow: visible;
line-height: initial;
word-wrap: break-word;
white-space: pre-wrap;
}
.mat-option i {
display: block;
font-size: 1.5rem;
opacity: 0.6;
margin-left: 0.5rem;
}
这也很有帮助
我通过编写 MultiLineOptionDirective
来解决 <mat-option>
并调用 MatListItem
和 MatGridTile
从 [=16= 执行的相同 setLines()
函数]
<mat-form-field>
<mat-label>Favorite food</mat-label>
<mat-select [formControl]="mySelect">
<mat-select-trigger>{{ mySelect.value }}</mat-select-trigger>
<mat-option *ngFor="let food of foods" [value]="food.value" multiLineOption>
<h4 mat-line>{{ food.value }}</h4>
<p mat-line>{{ food.description }}</p>
</mat-option>
</mat-select>
</mat-form-field>
https://stackblitz.com/edit/angular-material-multi-line-option
编辑:“extends”可能会被误解,extends 作为指令扩展元素的功能而不是 class extends a base class.
我用了下面的CSS.
::ng-deep .mat-select-panel mat-option.mat-option {
height: unset;
line-height: 1.2em;
padding-top: 0.9em;
padding-bottom: 0.9em;
}
::ng-deep .mat-option-text.mat-option-text {
white-space: normal;
}
我最近遇到了这个问题,经过一些调试我发现你可以引用与 .
关联的 class.mat-option {
white-space: normal;
line-height: normal;
}
这会将文本换行到下一行并相应地调整高度。如果您正在处理动态数据,也可以执行 line-height: auto
。
这显示了 2 列中的长文本。
在 .scss 中添加:(如果您希望每行具有相同的高度,您可以删除最后 3 行)
.multiline-mat-option.mat-option {
white-space: normal;
line-height: normal;
height: unset;
padding-top: 0.9em;
padding-bottom: 0.9em;
}
在Html中:
<mat-option class="multiline-mat-option"...