简体   繁体   English

Mat-autocomplete 面板应与主机输入的宽度相同,或者根据内容更宽

[英]Mat-autocomplete panel should be the same width as host input, or wider depending on content

I have an angular autocomplete-tree component and I have an issue with the autocomplete panel width.我有一个 angular 自动完成树组件,我对自动完成面板宽度有疑问。

I have two requirements that I can solve individually but not together:我有两个要求可以单独解决,但不能一起解决:

  • The panel should grow once the content is wider than the panel.一旦内容比面板宽,面板应该会增长。 I used [panelWidth] = "auto" for this and it works perfectly.我为此使用[panelWidth] = "auto"并且效果很好。
  • The panel should have a minimum width of the host input.面板应具有主机输入的最小宽度。 Can also use panelWidth for this but then I lose the functionality in the first point.也可以为此使用 panelWidth ,但随后我失去了第一点的功能。

From Change the mat-autocomplete width?更改垫子自动完成宽度? ) I saw I can use [panelWidth] = "auto" . ) 我看到我可以使用[panelWidth] = "auto"

I know I can use the css styling .mat-autocomplete-panel{min-width: "300px"} but don't know how to get the width of the specific input.我知道我可以使用 css 样式.mat-autocomplete-panel{min-width: "300px"}但不知道如何获取特定输入的宽度。

So I resorted to javascript.所以我求助于javascript。 I saw in the documentation that AutocompletePanel exposes the panel ElementRef, but this value seems to be undefined no matter what I do?我在文档中看到 AutocompletePanel 公开了面板 ElementRef,但是无论我做什么,这个值似乎都是未定义的? Is the right way to acquire this value with @ViewChild ?使用@ViewChild获取此值的正确方法是什么? I have the following code that runs on the opened event.我有以下代码在opened的事件上运行。

<mat-autocomplete #autocomplete (opened)="opened()" [panelWidth]="auto" #auto="matAutocomplete">
@ViewChild("auto") autocomplete: MatAutocomplete;
@ViewChild("autoTreeInput") autoTreeInput: ElementRef;

  opened = () => {
    setTimeout(()=>{

    let p = this.autocomplete.panel?.nativeElement; //always null because panel is undefi
    console.log("opened", p, this.autocomplete);
    if (!p ) return
    p.style.minWidth =
      this.autoTreeInput.nativeElement.getBoundingClientRect().width + "px";
    },1000)
  };

Stackblitz 堆栈闪电战

I have reviewed you stackblitz example.我已经回顾了你的 stackblitz 示例。

It looks like is more a css issue.看起来更像是 css 问题。

Try this on autocomplete-simple-example.css fileautocomplete-simple-example.css文件上试试这个

.example-form {
  min-width: 150px;
  // max-width <-- remove it to use 100% of width
  width: 100%;
}

.example-full-width {
  width: 100%; // add this to use all width.
  transition: width 0.5s ease;
}
.mat-focused {
  width: 270px;
}

A working example is here: stackblitz一个工作示例在这里: stackblitz

Edit编辑

You can add any valid css to width css .您可以将任何有效的 css 添加到width css In this way a have added calc(100% - 60%) to make it fixed same as input以这种方式添加了calc(100% - 60%)以使其与输入相同

.example-form {
  min-width: 150px;
  width: 100%;
}

.example-full-width {
  width: 100%;
  transition: width 0.5s ease;
}
.mat-focused {
  width: 100%;
}
<mat-autocomplete (opened)="opened()" panelWidth="calc(100% - 60px)" </mat-autocomplete>

Working example 工作示例

The panel elementRef shows up after (not on) opened event is fired.面板 elementRef 在(未打开) opened事件被触发后显示。 More specifically, after the panel is added to the DOM.更具体地说,在面板被添加到 DOM 之后。 So simply adding a setTimeout in which I get the element works.因此,只需添加一个setTimeout即可让元素正常工作。 Unfortunately this means that the element shows up with the wrong width and then jumps to the width of the input.不幸的是,这意味着元素以错误的宽度显示,然后跳转到输入的宽度。

There might be a better solution, but this is what I'm going with now.可能有更好的解决方案,但这就是我现在要做的。

 opened = ()=> {
    let inputWidth = this.autoInput.nativeElement.getBoundingClientRect().width
    setTimeout(()=>{
      let panel = this.autocomplete.panel?.nativeElement;

      if (!panel ) return
      panel.style.minWidth = inputWidth + "px";
    })
  }

Working stackblitz 工作堆栈闪电战

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

相关问题 如何通过@input 属性应用样式,我想在 angular 中增加 mat-autocomplete 的宽度 - How to apply style through @input property, I want to increase width of mat-autocomplete in angular 如何在其他mat-autocomplete更改时绑定mat-autocomplete? - How to bind mat-autocomplete on change of other mat-autocomplete? 垫子自动完成不允许我 select - mat-autocomplete not allowing me to select mat-autocomplete 选项无法找到属性 - mat-autocomplete options not able to find property 垫子自动完成中的无限滚动 angular 11 - Infinite scroll in mat-autocomplete angular 11 Mat-autocomplete 在 ngOnInit() 中不起作用,Map() 不起作用 - Mat-autocomplete is not working in ngOnInit(), Map() is not working 如何在 formBuilder 上禁用 mat-autocomplete? - How to disable mat-autocomplete on formBuilder? 按下Tab键时如何选择垫子选项?它应该像垫子自动完成角度6中的输入按钮一样工作 - How can we select mat option when press on tab key?, it should work like enter button in mat-autocomplete angular 6 mat-autocomplete:选择后需要重置选项列表 - mat-autocomplete: Need to Reset the List of Options After a Selection is Made Angular5 - 如何检测垫子自动完成中的空字段或已删除字段? - Angular5 - How to detect empty or deleted field in mat-autocomplete?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM