Ng2-select: Drop Down close issue

Created on 21 Mar 2017  ·  10Comments  ·  Source: valor-software/ng2-select

Hello
We are having two ng2-select one under the other. when we are focusing on second drop-down and at the same time focusing on the first one, second one is not closing. We want that second ng2-select to close automatically when we focus the first ng2-select if the second ng2-select in opened.

Most helpful comment

@gauzpan I also did a work around for this without modifying the SelectCompoenent:

in html:

in typescript:
import { Component, OnInit, ViewChildren, QueryList } from '@angular/core';
import { SelectComponent } from "ng2-select/ng2-select";

@ViewChildren(SelectComponent) selectElements: QueryList;

public closeOtherSelects(element) {
if (element.optionsOpened == true) {
let elementsToclose= this.selectElements.filter(function (el: any) {
return (el != element && el.optionsOpened == true)
});
elementsToclose.forEach(function (e: SelectComponent) {
e.clickedOutside();
})
}
}

All 10 comments

Hello, I added a PR for that, I still waiting to be merged.. you might be able to fork it from my repo, but it would be nice to have a response about that PR

I also did a little workaround of my own, I created a _(dropdownOpen)_ event which I listen to at my ng-select element component and call a function which will close all the other SelectComponent opened apart from the currently opened SelectComponent.
I modified one function inside _select.ts_ file like below to emit event :-

private open():void {
    this.options = this.itemObjects
      .filter((option:SelectItem) => (this.multiple === false ||
      this.multiple === true && !this.active.find((o:SelectItem) => option.text === o.text)));

    if (this.options.length > 0) {
      this.behavior.first();
    }
    this.optionsOpened = true;
    this.dropdownOpened.emit(true);
  }

In html I added event listener for _(dropdownOpened)_

<ng-select #elem (dropdownOpened)="closeOtherElems(elem)"
                [multiple]="true"
                [items]="items"
                [disabled]="disabled"
                [isInputAllowed]="true"
                (data)="refreshValue($event)"
                (selected)="selected($event)"
                (removed)="removed($event)"
                placeholder="No city selected"></ng-select>

This is my calling function on event trigger inside my component containg ng-select

  @ViewChildren(SelectComponent) selectElem :QueryList<SelectComponent>;

  public closeOtherElems(element){
   let a = this.selectElem.filter(function(el){
                  return (el != element)
            });

    a.forEach(function(e:SelectComponent){
      e.closeDropdown();
    })
  }

@Manny91 Thanks for the PR. I merged your change and it works well. For others - This is the PR link from Manny91 https://github.com/valor-software/ng2-select/pull/712/files

@saravanaselvan Thank you! hopefully will help everyone who needs it

is there an official fix for this?

@gauzpan I also did a work around for this without modifying the SelectCompoenent:

in html:

in typescript:
import { Component, OnInit, ViewChildren, QueryList } from '@angular/core';
import { SelectComponent } from "ng2-select/ng2-select";

@ViewChildren(SelectComponent) selectElements: QueryList;

public closeOtherSelects(element) {
if (element.optionsOpened == true) {
let elementsToclose= this.selectElements.filter(function (el: any) {
return (el != element && el.optionsOpened == true)
});
elementsToclose.forEach(function (e: SelectComponent) {
e.clickedOutside();
})
}
}

@iosifpetre18 if you want to use it in your package json just do:

dependencies: { .... "ng2-select-base": https://github.com/Manny91/ng2-select/tarball/close-on-click-outside }

Duplicate #913
It was fixed in that fork: https://github.com/optimistex/ng2-select-ex

Thanks a lot @gauzpan@hucheng91 and @iosifpetre18 for the code you shared, it helped me a lot.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ronzeidman picture ronzeidman  ·  3Comments

Garybhardwaj picture Garybhardwaj  ·  3Comments

Cadenei picture Cadenei  ·  4Comments

cherrydev picture cherrydev  ·  5Comments

bissolli picture bissolli  ·  6Comments