[英]PrimeNg dropdown in editable datatable not holding the selected value
Below is the code for the column in the datatable 以下是数据表中该列的代码
<p-column field="organization.description" header="Partner" [editable]="dtCostShare" [style]="{'width':'30%'}"> <ng-template let-col let-csp="rowData" pTemplate="editor"> <span class="required-lbl">* <p-dropdown name="organization" [(ngModel)]="csp.organization.organizationId" (onChange)="addPartnerDescription(csp.index)" [options]="partners" [style]="{'width':'10px'}" appendTo="body" [ngClass]="{'errorCol':csp.organization.organizationId === ''}"> </p-dropdown> </span> <span *ngIf="(csp.organization.organizationId === '' )" class="text-danger">Partner is required</span> </ng-template> </p-column>
ts code to populate the droupdown ts代码以填充droupdown
getPartners() { this.partners.push({ label: 'Please Select', value: '' }); this.parameterService.getPartners().subscribe((data) => { for (let record of data) { this.partners.push({ label: record.description, value: record.organizationId }); } }); }
Whenever i edit the grid the dropdown shows the "Please select" instead of showing the selected Organization name. 每当我编辑网格时,下拉列表就会显示“请选择”,而不是显示所选的组织名称。 When i print the "csp.organization.organizationId" it is giving the selected organizationId but [(ngModel)] doesn't seem to set the selected value.
当我打印“ csp.organization.organizationId”时,它给出了选定的organizationId,但是[[ngModel)]似乎没有设置选定的值。
where am i going wrong? 我要去哪里错了?
Had the same issue, I found that, in my case, the app was trying to search the value set in ngModel when the dropdown was still empty. 遇到相同的问题,我发现在我的情况下,当下拉列表仍然为空时,应用程序正尝试搜索ngModel中设置的值。
The solution was to add a *ngIf to be sure that the list wasn't null, in your case it'll be smthng like this : 解决方案是添加一个* ngIf,以确保列表不为空,在您的情况下,它将像这样:
<p-dropdown *ngIf="partners != null && partners.lenght > 0"
name="organization" [(ngModel)]="csp.organization.organizationId"...
Hope this will help :) 希望这会有所帮助:)
I have fixed this by calling a method (onfocus) of the dropdown and doing dropdown.updateSelectedOption(val); 我通过调用下拉菜单的方法(onfocus)并执行dropdown.updateSelectedOption(val)来解决此问题。 in the method.
在方法中。
<p-dropdown #dd1 name="organization" [(ngModel)]="csp.organization.organizationId" placeholder="Please Select" (onFocus)="setSelectedPartner(dd1,label of the selectItem,value of the selectItem)" [options]="SelectItemList" ></p-dropdown>
setSelectedPartner(dropdown: Dropdown,label:any ,val:any){ if(val!='') dropdown.updateSelectedOption(val); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.