question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with item selection when customFilter used

See original GitHub issue

Problem with item selection when customFilter used

I try to use customFilter option to display firstName and lastName values in select.

I succed to have values appear in autocomplete dropdown, but when click to select one, the select stay blank.

My entity :

export class Exploitant {
    id:number;
    nom:string;
    prenom:string;
    mail:string;
    tel:string;
    adresse:string;
    codePostal: number;
    commune: string;
}

My filter function :

filterName(items: any[], query: string) {
    var results:any[] = [];
    items.forEach(
      (item) => {
        if( (item['prenom']+' '+item['nom']).toLowerCase().indexOf(query.toLowerCase()) > -1 ) {
          results.push(item);
        }
      }
    );
    return results;
  }

and the html I use :

<div class="ng-autocomplete">
    <ng-autocomplete 
      [data]="exploitants"
      [initialValue]=""
      [customFilter]="filterName"
      formControlName="exploitant"
      notFoundText="Not found"
      [itemTemplate]="itemExploitantTemplate"
      [notFoundTemplate]="notFoundTemplate">                                 
    </ng-autocomplete>
    
    <ng-template #itemExploitantTemplate let-item>
    <a>
        <span [innerHTML]="item.prenom"></span>&nbsp; 
        <span [innerHTML]="item.nom"></span>
    </a>
    </ng-template>
    
    <ng-template #notFoundTemplate let-notFound>
        <div [innerHTML]="notFound"></div>
    </ng-template>
</div>

Expected result

Selected item should appear in the select with a cross at the right to clear it

Actual result

Nothing appear in the select after click on an item in the dropdown, and autocomplete seems to be broken. When trying to write more stuff in select, in get some errors in console :

ERROR TypeError: text.replace is not a function at HighlightPipe.transform (angular-ng-autocomplete.js:1587) at pureFunction3Internal (core.js:25603) at Module.ɵɵpipeBind3 (core.js:25778) at AutocompleteComponent_li_10_div_2_Template (angular-ng-autocomplete.js:92) at executeTemplate (core.js:9518) at refreshView (core.js:9387) at refreshEmbeddedViews (core.js:10507) at refreshView (core.js:9411) at refreshEmbeddedViews (core.js:10507) at refreshView (core.js:9411)

Steps to reproduce

  1. Click in the select
  2. Click on an item in the dropdown
  3. Bug

Context

Autocomplete on items having firstName and lastName.

Your Environment

  • Version used: 2.0.5
  • Browser Name and version: Chromium 88.0.4324.150
  • Operating System and version: Ubuntu (latest)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jusabatiercommented, Feb 18, 2021

Huge thanks ! This solution work well :

  1. Add a fullName property on the model :
export class Exploitant {
    id:number;
    nom:string;
    prenom:string;
    mail:string;
    tel:string;
    adresse:string;
    codePostal: number;
    commune: string;

    fullName:string;
}
  1. Set this field value in the REST service which handle the update of the data :
[...]
  getExploitants():Promise<any> {
    return this.httpClient.get<Exploitant[]>(environment.API_URL+'/exploitant').toPromise().then(
      (exploitants) => {
        exploitants.map( exploitant => exploitant.fullName = `${exploitant.prenom} ${exploitant.nom}`);
        return exploitants;
      }
    );
  }

  getExploitant(id:number):Promise<any> {
    return this.httpClient.get<Exploitant>(environment.API_URL+'/exploitant/'+id).toPromise().then(
      (exploitant) => {
        exploitant => exploitant.fullName = `${exploitant.prenom} ${exploitant.nom}`;
        return exploitant;
      }
    );
  }
[...]
  1. Use the fullName property as searchKeyword value :
<div class="ng-autocomplete">
    <ng-autocomplete 
      [data]="exploitants"
      [initialValue]=""
      [searchKeyword]="'fullName'"
      formControlName="exploitant"
      notFoundText="Not found"
      [itemTemplate]="itemExploitantTemplate"
      [notFoundTemplate]="notFoundTemplate">                                 
    </ng-autocomplete>
    
    <ng-template #itemExploitantTemplate let-item>
    <a [innerHTML]="item.fullName"></a>
    </ng-template>
    
    <ng-template #notFoundTemplate let-notFound>
        <div [innerHTML]="notFound"></div>
    </ng-template>
</div>

And with this it work well, no more customFilter to use.

0reactions
gmerabishvilicommented, Feb 18, 2021

@jusabatier Great!

Read more comments on GitHub >

github_iconTop Results From Across the Web

ListView custom filter gives wrong item selected when filtered ...
ListView custom filter gives wrong item selected when filtered [Android] - Stack Overflow. Stack Overflow for Teams – Start collaborating and ...
Read more >
Quick start: Filter data by using an AutoFilter - Microsoft Support
You can filter based on choices you make from a list, or you can create specific filters to focus on exactly the data...
Read more >
Filter data in a range or table - Microsoft Support
Select Data > Filter. ... Select Text Filters or Number Filters, and then select a comparison, like Between. ... Enter the filter criteria...
Read more >
Video: Advanced filter details - Microsoft Support
Training: You can use the Advanced Filter to create more powerful filters, ... Remove all the filters in a worksheet ... Sort data...
Read more >
Custom Filters - Sisense Community
I am having 2 issues with the Filter Widget. First, if you have more than one of these filters on a dashboard and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found