[英]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:我有两个要求可以单独解决,但不能一起解决:
[panelWidth] = "auto"
for this and it works perfectly.我为此使用[panelWidth] = "auto"
并且效果很好。 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)
};
I have reviewed you stackblitz example.我已经回顾了你的 stackblitz 示例。
It looks like is more a css issue.看起来更像是 css 问题。
Try this on autocomplete-simple-example.css
file在autocomplete-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>
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";
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.