Focus Filterfield of ng-multiselect-dropdown automatically when clicked

JackTheRealPi

I'm using the ng-multiselect-dropdown package (https://www.npmjs.com/package/ng-multiselect-dropdown) in my angular project. Everything works fine, but I want to focus the filter input field automatically when I open the multiselect dropdown.

Here is my html code:

<div class="input-group" style="display: flex">
                <span class="input-group-text"><i class="fas fa-building"></i></span>
                <ng-multiselect-dropdown style="flex: 1"
                                         [placeholder]="'Unternehmen'"
                                         [data]="this.companies"
                                         [formControl]="filterCompany"
                                         [settings]="dropdownSettingsCompanies"
                                         (onFilterChange)="updateCompanyList($event)"
                                         (onSelect)="onItemSelect($event)">
                </ng-multiselect-dropdown>
</div>

And here are my settings for the multiselect:

dropdownSettingsCompanies: any = {
        singleSelection: false,
        idField: 'id',
        textField: 'name',
        showSelectedItemsAtTop: true,
        clearSearchFilter: true,
        allowSearchFilter: this.ShowFilter,
        enableCheckAll: false,
        allowRemoteDataSearch: true,
        searchPlaceholderText: 'Bitte geben Sie einen Suchbegriff ein',
    };

Naren Murali

The project does not have this feature internally, you need to write your own way. I am using a directive and getting the input element using querySelector and focusing the input!

import { Directive, ElementRef, HostListener } from '@angular/core';

@Directive({
  selector: '[appFocusOnClick]',
})
export class FocusOnClickDirective {
  constructor(private elementRef: ElementRef) {}

  @HostListener('click', ['$event.target'])
  onClick(btn) {
    console.log('button', btn, 'number of clicks:');
    const input = this.elementRef.nativeElement.querySelector(
      '.filter-textbox > input'
    );
    if (input) {
      input.focus();
    }
  }
}

stackblitz

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Bootstrap dropdown closing when clicked

how to focus a button when a textbox is clicked

ui.bootstrap.dropdown keeps focus when other controls are clicked

How to reset the ng-multiselect-dropdown

When "ng-select" clicked somewhere outside "dropdown" not closing

angular/typescript: ng-multiselect-dropdown mutliple properties in textField

Close dropdown when dropdown list is clicked

How to set default selected checkbox in Ng Multiselect dropdown (Angular Control)

ng-multiselect-dropdown custom css

set width of ng-multiselect-dropdown

ng-multiselect-dropdown - TypeError: Cannot read property 'idField' of undefined

How to pass multiple items for onselect event for ng-multiselect-dropdown

Anchor elements do not receive focus when clicked

creating a dropdown menu when focus

closing dropdown single or multiselect when clicking outside

Dropdown jumps on focus when scrolling

GetOpenFileName Multiselect:= True error when cancel is clicked

How to set Selection Limit of Multiselect Dropdown with ng-repeat value

ng-multiselect-dropdown onDeSelectAll() not working

how to bind data of api into ng-multiselect-dropdown?

ng-multiselect-dropdown not showing proper result after search

ng-multiselect-dropdown-angular7 (onDeSelect) not working

How to store values from ng-dropdown-multiselect in sessionStorage in AngularJS?

angular ng-multiselect-dropdown get id

Ng-multiselect-dropdown and data filtering

ng-multiselect-dropdown data binding error

Close dropdown menu when clicked on different dropdown

How to extract data from Ng Multiselect dropdown to from group

Change the icon when clicked on the dropdown