Javascript 如何通过引导multi-select使用angular8使多个选定值的布尔值为真,其余值为假
您好,我使用的是Angular8应用程序,在该应用程序中,我希望将对象数组中的多选择值id设置为boolValue true,并将未选中的id对象设置为boolValue false 因此,在这里,基于复选框的选中/取消选中,我能够获得'selectedName'变量中的值数组。通过使用这些值数组,“用户”数组中的对象数组值将在布尔值中变为true,如果没有与“selectedName”匹配的id,则这些对象的布尔值应为false TS: HTML:Javascript 如何通过引导multi-select使用angular8使多个选定值的布尔值为真,其余值为假,javascript,angular,Javascript,Angular,您好,我使用的是Angular8应用程序,在该应用程序中,我希望将对象数组中的多选择值id设置为boolValue true,并将未选中的id对象设置为boolValue false 因此,在这里,基于复选框的选中/取消选中,我能够获得'selectedName'变量中的值数组。通过使用这些值数组,“用户”数组中的对象数组值将在布尔值中变为true,如果没有与“selectedName”匹配的id,则这些对象的布尔值应为false TS: HTML: private getSelected
private getSelectedRoles(id) {
let selectedId = id.split(',');
let agentRoleList = this.user;
for (let i = 0; i < agentRoleList.length; i++) {
for (let j = 0; j < selectedId.length; j++) {
let selectedValue = +selectedId[j]
if (agentRoleList[i].id == selectedValue) {
agentRoleList[i].boolValue = true
} else {
agentRoleList[i].boolValue = false
}
}
}
console.log(agentRoleList, agentRoleList)
return agentRoleList;
}
multiselectValue() {
setTimeout(() => {
$('#multiselectUser').multiselect({
buttonWidth: '400px'
}).on('change',(e)=>{
let selectedFilename = $('#multiselectUser').val()
selectedFilename = selectedFilename.filter(function (item, index, inputArray) {
return inputArray.indexOf(item) == index;
});
let selectedName = selectedFilename ? selectedFilename.toString() : '';
this.getSelectedRoles(selectedName)
})
},100)
}
<form >
<div class="form-group">
<label for="">Select User</label>
<select name="user" id="multiselectUser" multiple="multiple" >
<option *ngFor="let field of user" [value]="field.id" >
{{field.label}}</option>
</select>
</div>
</form>