【问题标题】:Remove selected options from drop down and display the available options - select-form-control - Angular从下拉列表中删除选定的选项并显示可用选项 - 选择表单控件 - Angular
【发布时间】: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;
}

有人可以帮忙吗?

【问题讨论】:

    标签: html angular


    【解决方案1】:

    对不起,我的上一条评论是一个糟糕的解决方案。原因是如果您从下拉菜单 1 中选择了错误的选项,然后从下拉菜单 1 中选择了不同的选项,您现在将在下拉菜单 2 中丢失 2 个选项,而不仅仅是一个。你应该做的是:

    仅使用一个数组禁用列表中的选项: 在您的控制器中:

    arrayOfLocationsForDropdowns = [1, 2, 3, 4, 5];
    selectedOptionFromDropDownOne = selected location;
    

    在您的 html 中:

    下拉 1:

    <select class="form-control" formControlName="location">
        <option *ngFor="let location of locationList" [value]="location">{{location}}</option>
    </select>
    

    ** 仍然使用您的过滤技术,但不要取出选定的选项,而是将该选项设置为等于变量 selectedOptionFromDropDownOne

    下拉2:

    <select class="form-control" formControlName="location2">
        <ng-container *ngFor="let location of locationList" >
             <option *ngIf="location != selectedOptionFromDropDownOne" [value]="location">{{location}}</option>
        </ng-container>
    </select>
    

    这将允许您使用所有数据而无需取出任何数据,它将在第二个下拉列表中显示所有值,但在第一个下拉列表中选择的选项除外。

    为了使这更加动态,将变量 selectedOptionFromDropDownOne 设为一个数组并检查该数组是否有后续的下拉菜单:

    arrayOfSelectedOptionsFromPreviousChoices = [] 然后在未来选择的代码中:

    将 ngIf 从 *ngIf="location != selectedOptionFromDropDownOne" 更改为 *ngIf="!arrayOfSelectedOptionsFromPreviousChoices 中的位置"

    【讨论】:

    【解决方案2】:

    一个问题是,对于您从该列表中提取的信息,所有内容都设置为常量。常数不能改变。

    我建议将您要使用的位置放在一个临时数组中,然后在选择该选项后,在调用的函数中从该临时数组中删除该选项。这样每次您返回下拉菜单时,它就不再提供该选项:

    【讨论】:

    • 感谢您指出这一点。我尝试将 const 更改为临时数组。我能够过滤掉未选择的选项。我遇到的问题是循环的列表 - (locationList) 正在从第一个下拉列表中删除选定的选项。
    • 假设我有 2 个下拉菜单和两个位置。下拉 1 最初显示两个位置,我们选择位置 1。下拉 2 显示剩余的一个位置,即位置 2,但是当我返回查看下拉 1 时,我看到的是位置 2 而不是一个。
    • 所以要确保我有这个正确的。 1)您从下拉列表 1 中选择位置 1 2)将位置 2 留在下拉列表 2 中 3)您没有从下拉列表 2 中选择位置 2,4)您再次单击下拉列表 1,只有位置 2 在那里?
    • 如果这样接缝是预期的行为。给定您的代码,一旦选择了一个选项,它就会从列表中删除,因此根据发生的情况,您可以选择一个下拉菜单,选择一个选项,然后再次单击下拉菜单,该选项将不再存在.
    • 我选择了带有位置 1 的下拉 1。这让我选择了位置 2 作为下拉 2。到目前为止我们都很好。现在,无需单击下拉菜单 1,我就可以看到位置 2 的值已经存在。我知道这是因为我从列表中删除了选定的值,并且列表更新为未选择的位置。但我需要有位置 1 的下拉 1(选定选项)和没有位置 1 的下拉 2。位置 1 正在被删除。您能否建议如何处理这种情况?
    猜你喜欢
    • 2020-01-10
    • 2020-11-13
    • 1970-01-01
    • 2016-07-24
    • 2021-07-25
    • 2017-01-11
    • 1970-01-01
    • 2017-05-19
    相关资源
    最近更新 更多