简体   繁体   English

触发primeng多选对话框关闭的事件

[英]Trigger an event on primeng multiselect dialog close

I am trying to use primeng multiselect component. 我正在尝试使用primeng multiselect组件。 I want to get all the selected values as soon as the user dismisses the multiselect dropdown. 我想在用户解除多选下拉列表后立即获取所有选定的值。 Is there a way to do it? 有办法吗? I know we have onChange() event but it triggers every time user selects or deselects a value from the list. 我知道我们有onChange()事件,但每当用户从列表中选择或取消选择一个值时它就会触发。 I want to trigger it when the user is done selecting the values. 我想在用户选择值时触发它。

As primeng does not provide any api to achieve what you need directly, here is one way i am able to do it. 由于primeng没有提供任何直接达到你需要的api,这是我能够做到的一种方式。

your.component.html your.component.html

<p-multiSelect #ms [options]="cars" [(ngModel)]="selectedCars"></p-multiSelect>

your.component.ts your.component.ts

import { Renderer} from '@angular/core';
import {MultiSelect} from 'primeng/primeng';

@ViewChild('ms') multiselect: MultiSelect;

constructor(private renderer  :Renderer) {}

ngOnInit(){
     this.renderer.listenGlobal('document', 'click', ()=> {
        if (!this.multiselect.selfClick && !this.multiselect.panelClick && this.multiselect.overlayVisible) {
          console.log('multiselect will hide'); // write code here to execute when multiselect overlay get dismissed/hidden
          // this.selectedCars will have all the selected values.
        }
    });
}

Primeng use same logic to dismiss multiselect overlay when user click anywhere other than multiselect itself. 当用户点击多选自身以外的任何地方时,Primeng使用相同的逻辑来关闭多选叠加。 If in the future primeng provide direct way of achieving the desired result , you may not need to use this work around. 如果将来primeng提供了实现预期结果的直接方法,您可能不需要使用此工作。

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

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