我需要将所选的值保留在multiselect的下拉列表的顶部。我将选定值保存在一个数组中(对于每个选定项),将原始选项列表保存在另一个数组中,将索引值和串联的选定值与数组顶部进行了比较。当我选择一个值而不是多个值时,它会起作用
html
<div>
<ng-multiselect-dropdown class ="ng-multiselect" [placeholder]="'Users'"
[data]="userList" [(ngModel)]="selectedUsers" [settings]="multiSelect"
name="user" (onSelect)="onItemSelect($event)" (click)="userSelectedAtTopOnClick()">
</ng-multiselect-dropdown>
</div>
ts
onItemSelect(user: User) {
this.userData = user;
}
userSelectedAtTopOnClick(): void {
this.isUserDropDownWidgetHidden = document.querySelector('.dropdown-list').hasAttribute("hidden");
if(this.isUserDropDownWidgetHidden ) {
this.sortUserListAfterEachSelection(this.userData);
}
}
sortUserListAfterEachSelection(user: User) {
let arrPartner: Partner[];
if(origInst) {
const index = this.userList.findIndex(element => element.partnerId === user.partnerId);
const item: Partner = this.userList[index];
if (index > -1) {
arrPartner = this.userList.splice(index, 1);
arrPartner = arrPartner.concat(this.userList);
this.userList = arrPartner;
}
}
}