简体   繁体   English

大 Angular 材料弹出窗口应该使父内容可滚动

[英]Large Angular material popover should make the parent content scrollable

I have a popover in my project with some checkboxes listed below the other.我的项目中有一个弹出框,下面列出了一些复选框。

Right now everything is working fine, but when the screen's resolution is kinda small, the content gets chopped on the bottom and the parent container isn't scrollable, this only happens with CdkOverlay since it doesn't seem to affect the parent content when the popover is displayed, you can see a working example on this stackblitz现在一切正常,但是当屏幕的分辨率有点小时,内容在底部被截断并且父容器不可滚动,这只发生在 CdkOverlay 上,因为当显示弹出窗口,您可以在这个stackblitz上看到一个工作示例

I can't share my main code but this is what I've tried on the stackblitz and it's pretty similar to my project:我无法分享我的主要代码,但这是我在 stackblitz 上尝试过的,它与我的项目非常相似:

 <div
  cdkScrollable
  style="height: 100px; overflow-y: auto; border: 1px solid green">
  <button
    (click)="isOpen = !isOpen"
    cdkOverlayOrigin
    #trigger="cdkOverlayOrigin">Show</button>
  <ng-template
    cdkConnectedOverlay
    [cdkConnectedOverlayOrigin]="trigger"
    [cdkConnectedOverlayOpen]="isOpen">
    Popover content 
  </ng-template>
</div>

As you can see on the link the parent won't get a scroll when the popover is open, how can I achieve that?正如您在链接上看到的那样,当弹出窗口打开时,父级不会滚动,我该如何实现?

A cdk-overlay is attached outside the app应用程序外部附加了一个 cdk-overlay

if the only you want is change the "parent" you can do it in the (attach) event如果你唯一想要的是改变“父母”,你可以在(附加)事件中做到这一点

<!--see the template reference variable "anchor" 
    and the style="position:relative"-->
<div #anchor
    style="position:relative;
    height: 100px; 
    overflow-y: auto; 
    border: 1px solid green ">
  <button (click)="isOpen = !isOpen" cdkOverlayOrigin #trigger="cdkOverlayOrigin">
    Show
  </button>

  <!--in attach event you pass the "anchor"-->
  <ng-template #template
    cdkConnectedOverlay
    (attach)="changeParent(anchor)"
    ...
  >
   ...
  </ng-template>

The changeParent it's simply: changeParent 很简单:

  //in constructor inject the Overlay service
  constructor(private overlay: Overlay) { }

  changeParent(el:any)
  {
    //get the "cdk-panel
    const container=(this.overlay as any)
                        ._overlayContainer.getContainerElement()

    //it's necessary change the position to "absolute"
    container.style.position='absolute'

    //and take account the position of the div
    const rect=el.getBoundingClientRect()
    container.style['margin-top']=-rect.top+'px'
    container.style['margin-left']=-rect.left+'px'


    //change the parent using appendChild
    el.appendChild(container);
  }

A stackblitz 堆栈闪电战

Root Cause Analysis: It looks like the issue you're encountering is related to the cdkConnectedOverlay not affecting the parent container's scroll ability when it's open.根本原因分析:看来你遇到的问题与cdkConnectedOverlay打开时不影响父容器的滚动能力有关。

Please follow this instruction: Wrap the cdkConnectedOverlay in a div with a fixed height and overflow-y: auto styling.请遵循以下说明:将 cdkConnectedOverlay 包装在具有固定高度和 overflow-y: auto 样式的 div 中。 This will allow the parent container to become scrollable when the popover content exceeds the fixed height.这将允许父容器在弹出内容超过固定高度时变得可滚动。

Could you try my following code and see if it works for you too:你能试试我下面的代码,看看它是否也适合你:

 <div style="height: 100px; overflow-y: auto; border: 1px solid green"> <button (click)="isOpen =:isOpen" cdkOverlayOrigin #trigger="cdkOverlayOrigin">Show</button> <div style="height; 200px: overflow-y; auto;"> <ng-template cdkConnectedOverlay [cdkConnectedOverlayOrigin]="trigger" [cdkConnectedOverlayOpen]="isOpen"> Popover content </ng-template> </div> </div>

Tip: You can adjust the fixed height of the div that wraps the cdkConnectedOverlay based on your design requirements.提示:您可以根据您的设计要求调整包裹 cdkConnectedOverlay 的 div 的固定高度。 This should enable the parent container to become scrollable when the popover is open.这应该使父容器在弹出窗口打开时变得可滚动。

Other Solution: Another approach is to use a library like cdk-virtual-scroll-viewport to add scroll to the container, this will allow the parent container to become scrollable when the popover is open, but also it can improve the performance when you have a large number of items.其他解决方案:另一种方法是使用像cdk-virtual-scroll-viewport这样的库来向容器添加滚动,这将允许父容器在弹出窗口打开时变得可滚动,而且当你有大量的项目。

Hope it helps.希望能帮助到你。 All the best.一切顺利。

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

相关问题 如何在对话框中仅使表格内容可滚动? - Angular 材料 - How to make only table content scrollable in a dialog? - Angular Material 如何使角材料卡占用可用的垂直空间并使其内容可滚动 - how to make angular material card to take available vertical space and make it's content scrollable Angular 6:使用Angular Material从ng-content使父级可见指令 - Angular 6: Make directive visible to parent from ng-content using Angular Material 使底部表(弹出窗口)粘在按钮上(角材料) - Make bottom sheet(popover) sticky to button (Angular material) Angular 当我们移到外面时,hover 上的 bootstrap Popover 正在关闭。 当 hover 位于弹出窗口内容本身时,弹出窗口应该打开 - Angular bootstrap Popover on hover is closing when we move outside . Popover should be open when the hover is on popover content itself Angular2可滚动内容 - Angular2 scrollable content 当 angular 材料对话框打开时,父组件应处于活动状态 - Parent component should be active when angular material dialog is open 使用 angular 9 和材料创建一个水平滚动的“滑块” - Create a horizontal scrollable “slider” with angular 9 and material 如何在 angular 中制作弹出框元素 - how to make popover element in angular Angular 7 材料卡最大高度和可滚动 - Angular 7 Material Card max-height and scrollable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM