【发布时间】:2019-09-24 07:30:21
【问题描述】:
我需要从下拉列表中删除选定的选项,并在后续下拉列表中仅显示可用选项。第一次 getAvailableLocationsList() 返回所有可用选项。从第一个下拉列表中选择值后,第二个下拉列表将显示尚未选择的剩余选项。但是,当我返回查看第一个下拉列表值时,最初选择的选项没有被选中,因为我正在从列表中删除选定的值。下拉菜单和选项的数量是动态的,我需要让第一个下拉菜单显示所有可用的选项,而随后的下拉菜单中没有已经选择的选项。
HTML 代码:
<div formArrayName="locationArray">
<div *ngFor="let location of list.controls[index].get('locationArray').controls;let locationIndex=index">
<div [formGroupName]="locationIndex">
<select class="form-control" formControlName="location" id="{{'location'+index}}" (change)="getLocationName(index, locationIndex)">
<option disabled value=""> Select</option>
<option *ngFor="let item of getAvailableLocationsList(index, locationIndex)" ngDefaultControl [ngValue]="item.address">{{item.address}}
</option>
</select>
</div>
</div>
</div>
打字稿代码:
getAvailableLocationsList(index, locationIndex) {
let locationList = []; // Array- from service;
const list= <FormArray>this.form.get('list');
const locationArray =
<FormArray>list.controls[index].get('locationArray')['controls'][locationIndex].get('location');
const locationVal = locationArray.value;
const selectedLoc = _.findWhere(locationList, { address: locationVal });
if (!_.isUndefined(selectedLoc)) {
locationList = _.filter(locationList, (eachLoc) => {
return eachLoc.address.id!== selectedLoc.id;
});
}
return locationList;
}
有人可以帮忙吗?
【问题讨论】: