Search code examples
primengprimeng-datatableprimeng-dropdowns

Primeng autocomplete component with different colors for each item - normally they appear in the same color


Currently I want to use the primeng autocomplete multi select component: https://www.primefaces.org/primeng/#/autocomplete

What I need additionally is that every item which I want to select from as an autosuggestion should be colored differently.

E.g. If I have the options Paris (red), Munich (blue) the background should be shown in a different color.


Solution

  • Once you have assigned a colour for each of your countries, just use templating to apply it :

    <p-autoComplete [(ngModel)]="country" [suggestions]="filteredCountriesSingle" (completeMethod)="filterCountrySingle($event)"
      field="name" [size]="30" placeholder="Countries" [minLength]="1">
    
      <ng-template let-country pTemplate="item">
        <div class="ui-helper-clearfix" [ngStyle]="{'background-color':country.backgroundColor}">
          {{country.name}}
        </div>
      </ng-template>
    
    </p-autoComplete>
    

    Check my Plunker where I defined a random colour for each country :

    this.listOfCountries.forEach(function (item) {
      item.backgroundColor = "#"+((1<<24)*Math.random()|0).toString(16);
    });