PrimeNG auto-complete dropdown setup with Angular

rgoal picture rgoal · Apr 11, 2018 · Viewed 10.8k times · Source

auto-complete is not working , dropdown is loaded the first time and once i try to type something is not filtering then dropwdown values are gone service.ts

 getUserLocations(UserID: string, allList:string  ) {
        return this._http.get(environment.BASE_API_URL + 'xxxxx/' + ID + '/' + allList)
            .map((response: Response) => response.json())
              .do(data => console.log('locations ' + JSON.stringify(data)))
            .catch(this.handleError);
    }

compnent.ts

        brand: any [];
        filteredBrands: any[];
       GetUserLocations(PNSLUID: string, allList: string) {
                   this._searchService.getUserLocations(ID, allList)
                .subscribe(
                data => {
                    this.brand= data.result.name;//not sure how to bind the id
                },
                error => console.log('GetControls Method: ' + <any>error, 'alert alert-danger'));
        }

   filterBrands(event) {
        this.filteredBrands = [];
        for (let i = 0; i < this.brand.length; i++) {
            let brand = this.brand[i];
            if (brand.toLowerCase().indexOf(event.query.toLowerCase()) == 0) {
                this.filteredBrands.push(brand);
            }
        }
    }

html

<p-autoComplete  [(ngModel)]="brand" [suggestions]="filteredBrands" (completeMethod)="filterBrands($event)" [size]="30"
                                                [minLength]="1" placeholder="Office" [dropdown]="true">
                                    <ng-template let-brand pTemplate="item">
                                        <div class="ui-helper-clearfix" style="border-bottom:1px solid #D5D5D5">

                                            <div style="font-size:18px;float:right;margin:10px 10px 0 0">{{brand}}</div>
                                        </div>
                                    </ng-template>
                                </p-autoComplete>

The issue is it is not populating the auto-complete dropdown..there s an issue with binding

*************************************************UPDATE***************************************************** issue: example: my dropdown has the following values

  1. region a

  2. region b

  3. region c

  4. HBV office

  5. Di office

if I type region auto-complete does not work it still display all values, however, if I select region b then start removing b then auto complete works..in other words..it works only if I select a value but if I start from empty text and start typing it does not work

html

 <p-autoComplete name="OfficeList" [(ngModel)]="val" [suggestions]="results" (completeMethod)="search($event)"  field="name" dataKey="id" [dropdown]="true"></p-autoComplete>

interface

export interface IOffices {
    id: number,
    name:string
}

component

    val: IOffices;
    results: IOffices[];  
    originalResults: IOffices[];

 GetUserLocations(PNSLUID: string, allList: string) {
              this._searchService.getUserLocations(PNSLUID, allList)
            .subscribe(
              data => {
                  this.results = data.result;
                 this.originalResults = data.result; 
          },
          error => console.log('GetControls Method: ' + <any>error, 'alert alert-danger'));
            })
    }

search(event) {
            console.log("result 2 : " + this.originalResults.length.toString());
    console.log("event : " + event.query.toString());

    this.results = this.originalResults;

    this.results = this.results.filter(data => 
       data.name.indexOf(event.query) !== -1);

    }

Answer

JuanDM picture JuanDM · Apr 12, 2018

Seems that you are using the code posted in the primeng site.. Take a look to the section Objects in their docs

This code works with strings and if i understood your code you are trying to bind an object.

So my suggestion is:

  • If you are binding objects you should use the attribute field in the autocomplete to set the object property you wanna show to the user.

  • The variable that you put in your NgModel(you can use an entire object) should has "the same type" that the objects in the array of suggestions.

  • For dropdown functionallity on click use the onDropdownClick event

HTML

<p-autoComplete name="OfficeList" [(ngModel)]="brand" [suggestions]="filteredBrands" (completeMethod)="filterBrands($event)" 
                 [size]="30" [minLength]="1" placeholder="Office" [dropdown]="true" (onDropdownClick)="GetUserLocations()">
 </p-autoComplete>

Component.ts

brand: any [];
filteredBrands: any[];

GetUserLocations(PNSLUID: string, allList: string) {
    this._searchService.getUserLocations(ID, allList)
        .subscribe(
            data => {
                this.results = data.result; // You should put the entire array of objects here
            },
            error => console.log('error'));
    }

search(event) {
        this.results = this.results
                           .filter(data => data..toString()
                           .toLowerCase()
                           .indexOf(event.query.toString().toLowerCase()) !== -1);
}

Doing this adjustments it should work.

A working example: https://primeng-autocomplete-example.stackblitz.io/

hope it helps.