To avoid displaying an empty drop-down menu, ensure that the options are populated before showing it using AngularJS

I have a situation where there are 3 different drop-downs - Drop-down A, Drop-down B, Drop-down C.
Depending on the selection made in Drop-down C, additional dropdowns will be populated.

Let's consider another set of 10 drop-downs numbered from 1 to 10 that will appear based on the choice made in Drop-down C.

  • If I choose item 1 from Dropdown C, then DropDown 1, 2, 4, 6 should be displayed.
  • If I select item 2 from Dropdown C, then DropDown 1, 2, 5, 6, 8 should be displayed.

There are many such scenarios like this.

Once the necessary dropdowns are populated and shown on the page, only the selected dropdown values should be saved to the database upon submission.
If any dropdown does not have any options, it should not be visible on the page.



Jsfiddle link : jsfiddle

Can you provide assistance on how to achieve this?

Answer №1

If you want to customize the dropdown options based on specific requirements, you can achieve this in your controller by toggling the show/hide functionality:

angular.module('app', [])
  .controller('Controller', function($scope) {
    $scope.opt1 = ["opt1", "opt2"];
    $scope.opt2 = ["opt1", "opt2"];
    $scope.opt3 = ["opt1", "opt2"];
    $scope.opt4 = ["opt1", "opt2"];
    $scope.opt5 = ["opt1", "opt2"];
    $scope.opt6 = ["opt1", "opt2"];
    $scope.opt7 = ["opt1", "opt2"];
   $scope.opt8 = ["opt1", "opt2"];
    $scope.opt9 = ["opt1", "opt2"];
    $scope.opt10 = ["opt1", "opt2"];
    
    $scope.showDropDownOptions = [];
    $scope.showDropDownOptions[0] = [2,3,6];
    $scope.showDropDownOptions[1] = [1,4];
    $scope.showDropDownOptions[2] = [5,9,10,7];
    $scope.showDropDownOptions[3] = [8];
  })
<!DOCTYPE html>

<head>
  <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.min.js"></script>
  <script src="script.js"></script>
</head>

<body ng-app="app">
  <div ng-controller="Controller">
    <br>
    <hr>
    <select name="main" id="main">
    <option value="mainOption">mainOption1</option>
    <option value="mainOption">mainOption2</option>
    <option value="mainOption">mainOption3</option>
    <option value="mainOption">mainOption4</option>
  </select>
    <hr>
    <select name="sub" id="main">
    <option value="subOption">subOption1</option>
    <option value="subOption">subOption2</option>
    <option value="subOption">subOption3</option>
    <option value="subOption">subOption4</option>
  </select>
    <hr>
    <select ng-model="selectC" name="subSub" id="main">
    <option value="0">subSubOption1</option>
    <option value="1">subSubOption2</option>
    <option value="2">subSubOption3</option>
    <option value="3">subSubOption4</option>
  </select>

    <hr>
    <select ng-if="showDropDownOptions[selectC].indexOf(1) != '-1' && selectC && opt1.length>0" ng-model="selectedName" ng-options="x for x in opt1">
  </select>
    <select ng-if="showDropDownOptions[selectC].indexOf(2) != '-1' && selectC && opt2.length>0" ng-model="selectedName" ng-options="x for x in opt2">
  </select>
    <select ng-if="showDropDownOptions[selectC].indexOf(3) != '-1' && selectC && opt3.length>0" ng-model="selectedName" ng-options="x for x in opt3">
  </select>
    <select  ng-if="showDropDownOptions[selectC].indexOf(4) != '-1' && selectC && opt4.length>0" ng-model="selectedName" ng-options="x for x in opt4">
  </select>
    <select  ng-if="showDropDownOptions[selectC].indexOf(5) != '-1' && selectC && opt5.length>0" ng-model="selectedName" ng-options="x for x in opt5">
  </select>
    <select  ng-if="showDropDownOptions[selectC].indexOf(6) != '-1' && selectC && opt6.length>0" ng-model="selectedName" ng-options="x for x in opt6">
  </select>
    <select  ng-if="showDropDownOptions[selectC].indexOf(7) != '-1' && selectC && opt7.length>0" ng-model="selectedName" ng-options="x for x in opt7">
  </select>
    <select  ng-if="showDropDownOptions[selectC].indexOf(8) != '-1' && selectC && opt8.length>0" ng-model="selectedName" ng-options="x for x in opt8">
  </select>
    <select  ng-if="showDropDownOptions[selectC].indexOf(9) != '-1' && selectC && opt9.length>0" ng-model="selectedName" ng-options="x for x in opt9">
  </select>
    <select  ng-if="showDropDownOptions[selectC].indexOf(10) != '-1' && selectC && opt10.length>0" ng-model="selectedName" ng-options="x for x in opt10">
  </select>
  </div>
</body>

</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Interpret the JSON reply

Could someone please explain why my function B() is not responding? <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/ja ...

Unable to call upon JavaScript code from an external file

Just starting out with Spring and JavaScript! I've put together a JSP file https://i.sstatic.net/XemJ5.png In my first.js, you'll find the following method: function firstmethod() { window.alert("Enter a New Number"); return true; } H ...

Step-by-step guide for implementing an "on change" event for a select box within a dialog box

I recently wrote an HTML code snippet like this: <div id = "dialog-1" title = "Dialog Title goes here..."> <select id= "lang" name= "lang"> <option value="1"> TEXT </option> <option value="2"> HTML </op ...

Developing ECMAScript code that is modular and compatible with both web browsers and node.js

As I venture into the world of heavy lifting in Javascript and node.js, I am transitioning from quick and dirty Web Audio Api experiments to actual projects. The idea of in-browser testing doesn't appeal to me, so I'm looking into using node for ...

Arranging images next to each other using CSS and HTML

I've been trying to arrange four images side by side, with two on the top row and two on the bottom. I want to ensure they stay consistent across all browser sizes except for mobile. Here is what I have attempted so far: #imageone{ position: absol ...

Is there a way to modify initialValue's value following every iteration?

I need assistance in changing the value of initialValue after each operation. For example, if I input 1000, the output should be 11,000 (10,000 + 1,000). Then, if I subtract and input 2000, the output should be 9,000 (11,000 - 2,000). Can anyone provide gu ...

Choosing the right framework for implementing push notifications can be a critical decision. Should

I am currently working on a Java application that requires the server to send push notifications to the client every one second. To achieve this, I am using HTML5 server-sent events for one-way communication from the server to the client. However, my conce ...

Tips for transferring PHP response data between pages in AngularJS without relying on local storage

One question I have is how to transfer data from one page to another in AngularJS without relying on local storage, and ensuring that the data persists even if the user reloads the page. Are there any alternatives or solutions available for this situation ...

Utilize React JS to parse and display a sophisticated JSON structure in a dropdown menu

Looking at the backend data structure provided, we have information on various courses in different departments. { "courseDetails" : [{"departmentId" : 105654, "courses" : [{"stream" : "science","courseIds" : ["104","105 ...

Modify CSS using JavaScript at a specific screen size

Currently, I am in the process of designing a responsive navigation system which involves implementing a button that dynamically changes the styling of the div #navigation using Javascript. The aim is to toggle between display: none; and display: block; ba ...

No content found in jQuery .text() values within iframe

I am experiencing an unusual issue with assigning a value to a variable using jQuery. I am unable to retrieve the values from .text() specifically on Spotify, while other functions on different sites are working fine. Even after the elements have loaded, i ...

The Joi Validation feature is ineffective when used with the schema provided below

{ "email": "{{$randomExampleEmail}}", "dateofbirth": "2000-01-09", "personal_document":{ "document_front":"url", "document_back":"url", ...

EmberJS - how to register a precompiled handlebars template

In my EmberJS application, I have precompiled all of my handlebars templates so that they are loaded as pure Javascript files. However, I am encountering an issue where these precompiled templates are not being added to the Ember container as expected. In ...

Choose an option removed upon clicking

I need to remove the option with a value of 0 when the user selects from the dropdown list. Choose: <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <form:select id="CONTEXTE" path="CONTEXTE" onclick="go()" class="s ...

The issue with session storage persisting even after closing the iframe

Encountering a persistent issue where the sessionStorage remains populated even after closing an iframe and opening another one with the same destination. I assumed that the sessionStorage would be reset and start afresh each time. The iframe is contained ...

Retrieve the input range slider value only after the mouse is released

I am currently developing a project in Angular 4. The following code enables me to retrieve real-time values from a slider when the user slides between 0 and 1. Each time the user slides, the applycontrol function is triggered due to ngModelChange. <in ...

Why does the jQuery .ajax() POST Request result in a 405 (Method Not Allowed) error while the GET request does not?

Currently, I am working on updating a form using ajax. The process runs smoothly when I utilize the GET method in ajax. However, an error of 405 method not allowed is thrown when I switch to using the Post method. This testing is being conducted on Localho ...

JavaScript: How can I execute a count that pertains solely to elements that are currently visible

Using jQuery, I have implemented a search functionality that hides items in a list that do not match a given string. The code loops through a list of divs and utilizes $(this).fadeOut(); to hide the elements. While this functionality works effectively, I ...

What are the steps to downloading a server-generated image with user data from the client using Express?

I am facing a challenge with downloading a server-generated image immediately after it is created. My current approach involves using fetch to send user input data (bypassing HTML forms). Although the image is successfully generated on the server, I am str ...

cannot showcase the reply in a popup (modal)

When a button is clicked, the viewJob function is called and the jobId is passed to it. The response data that I am attempting to display in a bootstrap modal is being assigned as local variables. I even tried this in HTML: {{viewJob(model.citynew)}} If ...