Search code examples
angularprimengprimeng-dropdowns

Custom content dropdown not working in PrimeNG?


I'm implementing a custom content dropdown. Is not working properly. It does not set selectedTestType value and It gives undefined value in the onChangeTestTypes.

<p-dropdown name="classTestTypeCombobox"
            [options]="TestTypes" [(ngModel)]="selectedTestType"
            [style]="{'width':'150px'}" filter="filter"
            [disabled]="this.isProdCodeDisabled"
            appendTo="body"
            required
            #classTestTypeCombobox="ngModel"
            (ngModelChange)="onChangeTestTypes($event)">
    <ng-template let-TestType pTemplate="item">
        <div class="ui-helper-clearfix" style="position: relative;height: 25px;">
            <div>{{TestType.descLong}}</div>
        </div>
    </ng-template>
</p-dropdown>

TestTypes is an array of class object, which has the following members.

id: number;
classificationCode: string;
descLong: string;
classificationParent: string;
codeType: number;

onChangeTestTypes(TestType) {
    this.selectedTestTypeDesc = this.TestTypes.filter(x => x.priceCode == TestType)[0].descLong;
    this.price.Type = this.TestTypes.filter(x => x.priceCode == TestType)[0].Type;
}

Solution

  • Use optionLabel with the name of the field that you want to show in the drop down list. For example if you want to use classificationCode

     <p-dropdown name="classTestTypeCombobox"
                [options]="TestTypes" [(ngModel)]="selectedTestType"
                [style]="{'width':'150px'}" filter="filter"
                [disabled]="this.isProdCodeDisabled"
                optionLabel="classificationCode"
    </p-dropdown>
    

    Observe that optionLabel does not need [] also the assigned value is simple the name of the custom object field.