简体   繁体   English

如何在 mat-autocomplete 中设置项目数

[英]How to set the number of items in mat-autocomplete

I'm now trying to change the number of items in a mat-autocomplete for a while without success.我现在正在尝试更改 mat-autocomplete 中的项目数,但没有成功。 I'm using material.angular.io我正在使用 material.angular.io

Here's my code这是我的代码

 import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-add-and-search', templateUrl: './add-and-search.component.html', styleUrls: ['./add-and-search.component.css'] }) export class AddAndSearchComponent implements OnInit { items: string[] = ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5']; constructor() { } ngOnInit() { } }
 <mat-form-field appearance="standard"> <mat-label>Search...</mat-label> <input matInput placeholder="Search..." [matAutocomplete]="auto"> <mat-icon matSuffix>search</mat-icon> </mat-form-field> <mat-autocomplete #auto="matAutocomplete"> <mat-optgroup label="Items"> <mat-option *ngFor="let item of items" [value]="item">{{item}}</mat-option> <mat-option><mat-icon>add</mat-icon>Add</mat-option> </mat-optgroup> </mat-autocomplete>

From the docs I learned that the autocomplete panel has a constant height of 256px ( https://material.angular.io/components/autocomplete/api#AUTOCOMPLETE_PANEL_HEIGHT ).从文档中我了解到自动完成面板的高度为 256 像素( https://material.angular.io/components/autocomplete/api#AUTOCOMPLETE_PANEL_HEIGHT )。

My question is: How can I change the autocomplete panel to display more items.我的问题是:如何更改自动完成面板以显示更多项目。 Let's say 10. If there is no intended way of doing this could someone give me a hint on a CSS trick to set the height eg to 512px instead of the default 256px?让我们说 10。如果没有打算这样做的方法,有人可以给我一个关于 CSS 技巧的提示,例如将高度设置为 512px 而不是默认的 256px?

Many thanks!非常感谢!

You can change the height of the autocomplete panel by overwriting the max-height like this:您可以通过像这样覆盖 max-height 来更改自动完成面板的高度:

::ng-deep .mat-autocomplete-panel {
 max-height: 512px !important; 
}

See it in action here . 在这里查看它的实际效果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将样式添加到 mat-autocomplete 的 mat-option - Adding Style to mat-option of mat-autocomplete 使用 Angular 材料将 mat-select 转换为 mat-autocomplete - Convert mat-select to mat-autocomplete using Angular Material 为 mat-autocomplete 的 mat-option 添加样式 - adding style to mat-option of mat-autocomplete 选择选项后,如何将 Angular Material mat-autocomplete text color 更改为白色? - How can I change Angular Material mat-autocomplete text color to white after an option is chosen? mat-autocomplete 下拉菜单显示在 window [Angular] 的左上方 - mat-autocomplete dropdown shows up on top left of the window [Angular] 进行选择而不是填写时清除 mat-autocomplete 搜索框? - Clear mat-autocomplete searchbox when making a selection instead of fill it out? Angular 8 mat-autocomplete 错误 - “matAutocomplete”,因为它不是“输入”的已知属性 - Angular 8 mat-autocomplete ERROR - 'matAutocomplete' since it isn't a known property of 'input' 如何使 mat 自动完成在 508 可访问性选项卡上不关闭? - How do I make the mat autocomplete not close on tab for 508 accessibility? 如何基于html表单输入设置数组中的项目数? - How to set the number of items in an array based on html form input? 在 angular 中,垫子自动完成功能无法正确过滤 - mat autocomplete not filtering properly in angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM