简体   繁体   English

Primeng p-dropdown 应在滚动文档页面上关闭

[英]Primeng p-dropdown should close on scrolling document page

Had p-dropdown which is inside p-table as column header.将 p-table 内的 p-dropdown 作为列标题。 While scrolling p-table, p-dropdown should close.在滚动 p-table 时,p-dropdown 应该关闭。

<p-table #dTable [columns]="cols">
<ng-template pTemplate="header">
  <tr class="table-header-row">
    <th *ngFor="let col of cols" [ngStyle]="{'width': col.widthPer ? col.widthPer + '%' : col.widthPx + 'px'}">
      {{col.header}}
    </th>
  </tr>
  <tr class="table-header-row">
    <th *ngFor="let col of cols" [ngSwitch]="col.field"
      [ngStyle]="{'width': col.widthPer ? col.widthPer + '%' : col.widthPx + 'px'}">
      <input [(ngModel)]="designNameFilterValue" *ngSwitchCase="'designName'" pInputText class="design-name-filter"
        (keyup)="refreshTree(hideNonSelected, defaultFilter)">
      <div *ngSwitchCase="'cost'">
        Value >= {{cost| currency}}
        <i class="fa fa-close"
          (click)="costFilterValue = 0; refreshTree(hideNonSelected, defaultFilter);"
          style="cursor:pointer" *ngIf="costFilterValue"></i>
        <p-slider [(ngModel)]="cost" [min]="0" [max]="maxCost"
          (onSlideEnd)="refreshTreeTable(hideNonSelected, defaultFilter)"></p-slider>
      </div>
      <p-dropdown class="filter" *ngSwitchCase="'risk'" [options]="filterdropdown" (click)="hide()"
        [(ngModel)]="FilterValue" (onChange)="refreshTree(hideNonSelected, defaultFilter)">
      </p-dropdown>
      <p-dropdown class="state-filter" *ngSwitchCase="'state'" [options]="stateItemsForFilterDropDown"
        (click)="hide()" [(ngModel)]="defaultFilter"
        (onChange)="refreshTree(hideNonSelected, defaultFilter)">
      </p-dropdown>
    </th>
  </tr>
</ng-template>

Please assist on this.请对此提供帮助。

Well, I also ran in a similar requirement, and after implementing that feature.好吧,我也遇到了类似的要求,并且在实现了该功能之后。 We realized its not the valid one, considering the below situation.考虑到以下情况,我们意识到它不是有效的。

Think about Mobile users and drop down have the search or filter feature with an input box.考虑一下移动用户,下拉菜单有带有输入框的搜索或过滤功能。

So whenever the user focuses on the input field it will open the drop-down and at the same time, it opens the mobile keypad that reduced the viewport and fire scroll event.因此,每当用户关注输入字段时,它都会打开下拉菜单,同时打开移动键盘,减少视口并触发滚动事件。 And on the scroll, you want to close the drop-down.在滚动条上,您想关闭下拉菜单。 Considering the above situation it will be in a loop (open-close) that we have to avoid.考虑到上述情况,它将处于我们必须避免的循环(打开-关闭)中。 And if you have only desktop users than its okay.如果您只有桌面用户,那还好。

So if you have only a desktop user, you implement this所以如果你只有一个桌面用户,你可以实现这个

app.component.ts app.component.ts

import { Component , Inject} from "@angular/core";
import { FormBuilder } from "@angular/forms";
import { HostListener} from "@angular/core";
@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  cities1 = [];
  constructor() {
    this.cities1 = [
            {label:'Select City', value:null},
            {label:'New York', value:{id:1, name: 'New York', code: 'NY'}},
            {label:'Rome', value:{id:2, name: 'Rome', code: 'RM'}},
            {label:'London', value:{id:3, name: 'London', code: 'LDN'}},
            {label:'Istanbul', value:{id:4, name: 'Istanbul', code: 'IST'}},
            {label:'Paris', value:{id:5, name: 'Paris', code: 'PRS'}}
        ];
  }
  @HostListener("window:scroll", [])
  onWindowScroll() {
    const ele = document.getElementById('mydiv');
    ele.click();
  }
}

here is stackblitz link这是stackblitz 链接

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

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