angular - primeNG 多选下拉列表在删除然后再次动态添加相同的值后保留复选框值
问题描述
我正在填充关于在另一个下拉列表中选择的值的 primeNG 多选下拉列表。.html 文件中 Dropdown 1 的代码:
<p-multiSelect [options]="destinations" [panelStyle]="{minWidth:'12em'}" [showToggleAll]="false" [filter]="false" (onChange)="ondestinationSelect($event)" defaultLabel="Select Destination(s)" maxSelectedLabels="0" selectedItemsLabel="{0} Destination(s) Selected">
<ng-template let-destination>
<span>{{destination.label}}</span>
</ng-template>
</p-multiSelect>
.ts 文件中下拉列表 1 的代码:
ondestinationSelect(event) {
let destinationIdArray = <FormArray>this.addteammateform.get('destinationsresponsiblefor');
let destinationId = new FormControl();
if(event.value.length > destinationIdArray.length) {
this.usermanagementService.get_residences(event.itemValue).subscribe(response => {
if(response.length > 0) {
for(let residenceIndex of response) {
let residencePush = {label : residenceIndex.residence_name, value:{id:residenceIndex.id, destination_id : residenceIndex.destination_id}};
this.residences.push(residencePush);
}
}
});
destinationId.setValue(event.itemValue.id);
destinationIdArray.push(destinationId);
}
else {
let residenceCount = this.residences.length;
let deleteResidenceArray : any = [];
for(let i=residenceCount - 1; i >=0 ; i--) {
if(this.residences[i].value.destination_id == event.itemValue) {
this.residences.splice(i, 1)
}
}
let destinationIndex = destinationIdArray.controls.indexOf(event.itemValue.id);
destinationIdArray.removeAt(destinationIndex);
}
}
.html 文件中下拉列表 2 的代码:
<p-multiSelect [options]="residences" [optionLabel]="label" [showToggleAll]="false" [filter]="false" showHeader="false" defaultLabel="Select Residence(s)" maxSelectedLabels="0" selectedItemsLabel="{0} Residence(s) Selected" [panelStyle]="{minWidth:'12em'}">
<ng-template let-residence>
<div style="font-size:14px;float:right;margin-top:4px">{{residence.label}}</div>
</ng-template>
</p-multiSelect>
下拉菜单也是 primeNG 多选下拉菜单,并在页面加载时预先填充。
- 勾选下拉菜单 1中的任何选项。它使用与下拉列表 1 对应的值填充下拉列表 2。
- 勾选下拉菜单 2中的任何选项。它显示选择的“x”项。
- 现在,从下拉 1中取消选中选项。
- 下拉列表 2中的值已删除,但选择的“x”项保持不变且未更新。
- 现在,再次检查下拉列表 1 中的值(与步骤 1 中的值相同)。
- 下拉列表 2填充了与下拉列表 1对应的 值,但此时值已被检查(在步骤 2 中选择的值)。
下拉 2 中的复选框在删除并再次添加后保留值。提前致谢。
解决方案
推荐阅读
- javascript - 如何使用js类属性作为另一个类属性数组中的键
- performance - 将非典型 json 字符串转换为 JSON
- excel - 根据过滤条件创建包含重复项的降序列表
- gcc - stm32CubeIDE 因为 arm-eabi-gcc 不能刷机
- java - mp3agic 的负查找偏移
- zooming - VCL Windows 应用程序中的手势缩放和平移
- php - CakePHP:在我们拥有类别ID的数组后搜索产品
- javascript - 为什么我不能使用 esitFullscreen 方法退出全屏?
- emc - 输入加速服务器的问题
- python - document.scrollingElement 不工作。无法向下滚动以检查元素