【问题标题】:clickable panel header overlaps header buttons可点击的面板标题与标题按钮重叠
【发布时间】:2022-01-31 20:39:19
【问题描述】:

我有一个面板组件,它的标题和按钮位于右上角。

目前向下箭头(V 形)切换面板内容的扩展/最小化。

当我尝试实现标题可单击以控制面板内容的扩展/最小化时,这也会影响“+”符号按钮可点击事件,从而导致面板扩展/最小化,这是不希望的。

(click)="toggleShowHide()" 不应影响“+”按钮

如何让“+”符号不受整个标题可点击事件的影响? 请记住,其他按钮将来也可能会加入“+”符号

dashboard.html

<div style="width: 600px">
  <custom-panel heading="TABLE" enableShowHide="true">
    <span class="panel-tools" >
      <div class="input-group">
        <span  title="New content" (click)="addContent()">
          <i class="tools-plus fas fa-plus" aria-hidden="true"></i>
        </span>
      </div>
    </span>
    //content
  </custom-panel>
</div>

panel.html

<div class="panel panel-custom">
  <div class="panel-heading" *ngIf="heading"  (click)="toggleShowHide()">
    {{heading}}

    <span *ngIf="enableShowHide" class="float-right panel-chevron" (click)="toggleShowHide()" style="cursor: pointer;">
      <i [hidden]="!showPanel" title="Hide" class="tools-plus fas fa-chevron-down" aria-hidden="true"></i>
      <i [hidden]="showPanel" title="Show" class="tools-plus fas fa-chevron-right" aria-hidden="true"></i>
    </span>

    <span class="float-right">
      <ng-content select=".panel-tools"></ng-content>
    </span>
  </div>

  <div class="panel-body" [ngClass]="{'panel-scrolling': panelScrolling }" [hidden]="shouldHidePanel()">
    <div class="mb-3">
    </div>

    <ng-content></ng-content>
  </div>
</div>

panel.ts

import { Component, Input } from '@angular/core';

@Component({
  selector: 'panel',
  templateUrl: './panel.component.html',
  styleUrls: ['./panel.component.scss']
})
export class PanelComponent {
  @Input() heading: string;
  @Input() size: string;
  @Input() panelScrolling = false;
  @Input() enableShowHide = false;
  @Input() hideFirst = false;
  showPanel = true;

  toggleShowHide(): void {
    this.showPanel = !this.showPanel;
  }

  shouldHidePanel(): boolean {
    if (this.enableShowHide) {
      if (this.hideFirst) {
        this.showPanel = false;
        this.hideFirst = false;
      }

      return !this.showPanel;
    }

    return false;
  }
}

panel.scss

.panel-body {
  padding: 12px;
}

.panel-custom {
  margin: 10px 15px 10px 0;
  border: none;
  border-radius: 0;
  box-shadow: 0 4px 4px 0 rgba(0, 0, 0, 0.15);
  background-color: white;

  .panel-heading {
    color: black;
    font-size: 16px;
    border-bottom: 1px solid black;
    padding: 8px 12px;
    @media (max-width: 1500px) {
      font-size: 14px;
    }
  }

【问题讨论】:

    标签: css angular


    【解决方案1】:

    我建议您了解“event bubbling”,因此您在父元素上添加了事件侦听器,它会自动为所有子元素设置它(具有自己的事件处理程序并停止传播父事件的元素除外(例如链接))。

    因此,您可以在事件处理程序方法中使用 event 对象中的方法“stopPropagation”作为参数

    写在cmets中,如果你需要什么,我会尽力帮助你。

    【讨论】:

    • 所以为了让它工作,我必须在dashboard.ts中我的addContent()函数的开头添加它。有没有办法只在面板中使用它?
    • 不,你必须将点击事件处理程序设置为'+'符号元素,并使用stopPropagation。 (顺便说一句,我没有在您的代码中隐含地看到它)。
    • plusIconClickHandler(e: Event): void { e.stopPropagation();像这样的东西。 stopPropagation 停止父事件。
    • 我添加了 (click)="$event.stopPropagation()" 到 dashboard.html,面板工具跨度。这解决了一切:D
    • 现在引入了一个错误,即雪佛龙向下箭头失去了它的功能,所以我添加了 的 panel.html 父跨度内容>
    【解决方案2】:

    阻止事件从原点进一步传播是个好主意。

    <div class="panel-heading" *ngIf="heading" (click)="toggleShowHide($event)">
    <span title="New content" (click)="addContent($event)">
    
      toggleShowHide(event : any): void {
        event.stopPropagation();
        this.showPanel = !this.showPanel;
      }
    
      addContent(event : any): void {
        event.stopPropagation();
      }
    

    【讨论】:

    • + 符号在不应该显示/隐藏时仍会切换
    猜你喜欢
    • 2016-04-17
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 2016-12-13
    • 2015-12-08
    • 2022-10-01
    • 2021-06-22
    • 1970-01-01
    相关资源
    最近更新 更多