我正在实现自定义内容下拉列表。工作不正常。它没有设置selectedTestType
值,它给出了undefined
中的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
是一个类对象的数组,它有以下成员。
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;
}
将optionLabel与要在下拉列表中显示的字段的名称一起使用。例如,如果要使用classificationCode
<p-dropdown name="classTestTypeCombobox"
[options]="TestTypes" [(ngModel)]="selectedTestType"
[style]="{'width':'150px'}" filter="filter"
[disabled]="this.isProdCodeDisabled"
optionLabel="classificationCode"
</p-dropdown>
观察optionLabel不需要[],指定的值也是简单的自定义对象字段的名称。
通过查看PrimeNG SelectItem,我发现该值既是标签又是对象,所以在原始问题中答案看起来像这个{{TestType.value.descLong}}
。我的完整解决方案是这样的:
<ng-template let-group pTemplate="item">
<div style="width: 100%; display: flex;">
<span style="width:30px;">{{group?.value.Code}}</span>
<span style="width:60px;">{{group?.value.Description}}</span>
</div>
</ng-template>
我有同样的问题,它似乎是来自Primeng的一个错误,因为只有参数“label”有效。
这是我打开p-dialog时自定义下拉列表以显示所选值的方法。必须添加@freedeveloper上面提到的optionLabel属性。
<p-dropdown appendTo="body" id="usertypeID" [options]="userTypesList" [(ngModel)]="user.userType" optionLabel="usertypeName">
<ng-template let-ut pTemplate="item">
<div class="ui-helper-clearfix" style="position: relative;height:25px;">
<div style="color:black;">{{ ut.value.usertypeName }}</div>
</div>
</ng-template>
下面是我的模型(它嵌套在User类下):
export class UserType {
userRoleID : number
usertypeID : number
usertypeName : string
}