Andrew
Andrew

Reputation: 622

How to hide select2 with options jQuery

I have select form with options, for search by keywords. I'm using select2 lib. But my select form is created dynamically, and I want to hide and show some select form depending on user's choice. Now the problem is that, if I choose something from the list:

enter image description here

For ex Production plant, it will appear new select form in which I have options. (it's working good)

If I again go to Production resources and choose another type:

enter image description here

Now I chose Production segment and prev select form is hidden, but in case if I want to go back to the Production plant, it will not appear:

enter image description here

My code:

$("#productionResources").change(function () {
    var prodRes = $("#productionResources :selected").html();
   $("#selectedProdRes").html(prodRes);
    var id = $(this).val();
    if (id != "") {
        $('#' + id).select2().next().show();
    } 
    idChanging(id);

});

function idChanging(id) {
    $("#productionResources").change(function () {
        $('#' + id).select2().next().hide();
    });
}

HTML:

<div class="display-in-row">
        <label>Production resources</label><br>
        <select class="form-control" id="productionResources">
            <option selected></option>
            <option value="productionPlant">Production plant</option>
            <option value="productionSegment">Production segment</option>
            <option value="valueStream">Value stream</option>
            <option value="workCenterGroup">Work center group</option>
            <option value="workCenter">Work center</option>
            <option value="station">Station"</option>
        </select>
    </div>
    <div class="display-in-row">
        <div class="col-12">
            <label id="selectedProdRes"></label><br>
            <select asp-for="ProductionPlantId" asp-items="Model.ProductionPlant" class="form-control content" id="productionPlant"></select>
            <select asp-for="ProductionSegmentId" asp-items="Model.ProductionSegment" class="form-control content" id="productionSegment"></select>
            <select asp-for="ValueStreamId" asp-items="Model.ValueStream" class="form-control content" id="valueStream"></select>
            <select asp-for="WorkCenterGroupId" asp-items="Model.WorkCenterGroup" class="form-control content" id="workCenterGroup"></select>
            <select asp-for="WorkCenterId" asp-items="Model.WorkCenter" class="form-control content" id="workCenter"></select>
            <select asp-for="StationId" asp-items="Model.Station" class="form-control content" id="station"></select>
        </div>
    </div>

Upvotes: 0

Views: 712

Answers (1)

Gugusteh
Gugusteh

Reputation: 128

The problem comes from your function idChanging():

As soon as you change the 1st select, the event of idChanging() is set and remains. So everytime you select #productionResources, all the events defined by idChanging() will get triggered, so all the previously selected selects will be hidden.

The following should work:

I add a class secondary-select to all the secondary selects to facilitate, but this is not required:

<div class="display-in-row">
        <div class="col-12">
            <label id="selectedProdRes"></label><br>
            <select asp-for="ProductionPlantId" asp-items="Model.ProductionPlant" class="form-control content secondary-select" id="productionPlant"></select>
            <select asp-for="ProductionSegmentId" asp-items="Model.ProductionSegment" class="form-control content secondary-select" id="productionSegment"></select>
            <select asp-for="ValueStreamId" asp-items="Model.ValueStream" class="form-control content secondary-select" id="valueStream"></select>
            <select asp-for="WorkCenterGroupId" asp-items="Model.WorkCenterGroup" class="form-control content secondary-select" id="workCenterGroup"></select>
            <select asp-for="WorkCenterId" asp-items="Model.WorkCenter" class="form-control content secondary-select" id="workCenter"></select>
            <select asp-for="StationId" asp-items="Model.Station" class="form-control content secondary-select" id="station"></select>
        </div>
    </div>
$("#productionResources").change(function () {
    // Define title
    var prodRes = $("#productionResources :selected").html();
    $("#selectedProdRes").html(prodRes);
    // Make sure none of the 2nd select is visible
    $('.secondary-select').select2().next().hide();
    // Display the select according to the ID
    var id = $(this).val();
    if (id !== '') {
        $('#' + id).select2().next().show();
    } 
});

Upvotes: 1

Related Questions