简体   繁体   English

更改 mat-autocomplete 宽度?

[英]Change the mat-autocomplete width?

It seems like the <mat-autocomplete> width is always the width of the input we're typing in. Is there no way to make it larger?似乎<mat-autocomplete>宽度始终是我们输入的输入的宽度。没有办法让它变大吗? I have a very small input and can't read the dropdown items.我有一个非常小的输入,无法阅读下拉项目。 I'm also limited in form space so can't make the input larger.我的表格空间也有限,所以不能让输入更大。

<mat-form-field style="width: 100px;">
  <input matInput [matAutocomplete]="auto">
  <mat-autocomplete style="width: 500px;" #auto="matAutocomplete">
    <mat-option *ngFor="let item of items" [value]="item">
    </mat-option>
  </mat-autocomplete>
</mat-form-field>

In the document for MatAutocomplete there is a property for exactly what you need:MatAutocomplete的文档中,有一个属性正是您所需要的:

@Input() panelWidth: string | @Input() 面板宽度:字符串 | number数字

Specify the width of the autocomplete panel.指定自动完成面板的宽度。 Can be any CSS sizing value, otherwise it will match the width of its host.可以是任何 CSS 大小值,否则它将匹配其主机的宽度。

By using this property you are not in the risk of meddling with anything else inherited from cdk-overlay-pane.通过使用此属性,您不会有干预从 cdk-overlay-pane 继承的任何其他内容的风险。

For further customization you might find this property useful as well:为了进一步定制,您可能会发现此属性也很有用:

@Input('class') classList: string @Input('class') 类列表:字符串

Takes classes set on the host mat-autocomplete element and applies them to the panel inside the overlay container to allow for easy styling.获取在主机 mat-autocomplete 元素上设置的类,并将它们应用于覆盖容器内的面板,以便轻松设置样式。

mat-autocomplete styles are inherited from cdk-overlay-pane. mat-autocomplete 样式继承自 cdk-overlay-pane。 In order to change width use below CSS.为了改变宽度使用下面的 CSS。

::ng-deep .cdk-overlay-pane {
    /* Do you changes here */
      min-width:450px;
}

You can refer here for full example: https://stackblitz.com/edit/angular-vzetqy您可以在此处参考完整示例: https ://stackblitz.com/edit/angular-vzetqy

Just add the panelWidth property to the mat-autocomplete只需将 panelWidth 属性添加到 mat-autocomplete

<mat-autocomplete [panelWidth]="300">

I solved it with (dynamic):我用(动态)解决了它:

 [panelWidth]="'auto'"

Following is also possible:以下也是可能的:

panelWidth="auto"

Apart from what Athan Soulis says, I've found out by sheer coincidence that panelWidth also accepts value "fit", which does exactly what most of us is aiming for (make it as wide as needed).除了 Athan Soulis 所说的,我偶然发现 panelWidth 也接受值“fit”,这正是我们大多数人的目标(使其尽可能宽)。 Not sure it's "any CSS sizing value", but it works.不确定它是“任何 CSS 大小调整值”,但它有效。

Angular provides configuration options for autocomplete named MAT_AUTOCOMPLETE_DEFAULT_OPTIONS Angular 为名为MAT_AUTOCOMPLETE_DEFAULT_OPTIONS的自动完成提供了配置选项

Documentation: https://material.angular.io/components/autocomplete/api#MatAutocompleteDefaultOptions文档: https ://material.angular.io/components/autocomplete/api#MatAutocompleteDefaultOptions

One of the field of that class is overlayPanelClass: string , that allow to set custom class to style cdk-overlay-pane for autocomplete.该类的字段之一是overlayPanelClass: string ,它允许将自定义类设置为样式cdk-overlay-pane以实现自动完成。

  1. Inject options in component that you need, through providers: [] in component declaration通过提供者在您需要的组件中注入选项:组件声明中的 []

 providers: [ { provide: MAT_AUTOCOMPLETE_DEFAULT_OPTIONS, useValue: { overlayPanelClass: 'autocomplete-overlay' }, }, ]

  1. Define your class in styles.scss在 styles.scss 中定义你的类

 .autocomplete-overlay { width: unset;important; }

  1. Set fit-content width for container, in which you display options of autocomplete, I use simplebar, so in my case it .simple-bar-content class为容器设置 fit-content 宽度,在其中显示自动完成选项,我使用 simplebar,所以在我的例子中它是.simple-bar-content

 .wrapper-for-simplebar { height: 100%; ::ng-deep.simplebar-content { width: fit-content; } }

If you need to set fixed width use [panelWidth] = "'150px'"如果您需要设置固定宽度,请使用 [panelWidth] = "'150px'"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM