What is the best way to automatically select options in a dynamic dropdown menu

In my code, I have a list of user records. One column has a dropdown to change the produced by. The ng-change function is working fine. However, after refreshing the page, the dropdown does not show the last selected option. Please review my code below -

Identity

<select name="repeatSelect" id="repeatSelect" ng-model="model_produced_by" style="width: 62px;" ng-change="change_produced_by(model_produced_by, order.Order.id)">
     <option value="">Select Produced By</option>
     <option ng-repeat="option in produced_by.availableOptions" value="{{option.id}}" ng-selected="option.id == order.Order.assigned_to">{{option.name}}</option>
</select>

Output -

<select ng-change="change_produced_by(model_produced_by, order.Order.id)" style="width: 62px;" ng-model="model_produced_by" id="repeatSelect" name="repeatSelect" class="ng-pristine ng-valid ng-empty ng-touched">
   <option value="">Select Produced By</option>
   <option ng-selected="option.id == order.Order.assigned_to" value="1" ng-repeat="option in produced_by.availableOptions" class="ng-binding ng-scope">Outsourced</option>
   <option ng-selected="option.id == order.Order.assigned_to" value="2" ng-repeat="option in produced_by.availableOptions" class="ng-binding ng-scope">In-House</option> 
  <option ng-selected="option.id == order.Order.assigned_to" value="3" ng-repeat="option in produced_by.availableOptions" class="ng-binding ng-scope">Local Store/Printer</option>
</select>

I want to select the value that matches

option.id == order.Order.assigned_to
, but it's not working. I've also used ng-init in the select tag. The last option is appearing in every dropdown. Can you help me find the error?

https://i.sstatic.net/QwPIv.jpg

Answer №1

The ideal approach is to utilize ngOptions in combination with ngModel:

<select name="repeatSelect" id="repeatSelect" style="width: 62px;" 
    ng-model="order.Order.assigned_to"         
    ng-change="change_produced_by(model_produced_by, order.Order.id)"
    ng-options="option.id as option.name for option in produced_by.availableOptions">
     <option value="">Select Produced By</option>
</select>

Answer №2

To ensure that the select functionality works correctly, it is important to define the ng-model property as shown in the following example:

<select id="inptAdmin" name="inptAdmin" class="form-control" 
  ng-model="res.adminUser.id" ng-options="user.id as user.name + ' - ' + user.email for user in listUser" required="required">
</select>

Within my controller, I have a listUser property which contains a list of user objects. In the ng-options declaration, I specify that the id of these objects should be used as the value, and the ng-model specifies where the selected value will be stored. With this setup, if ' res.adminUser.id ' is filled, the corresponding value in the select will be automatically selected.

I trust this explanation has been helpful to you.

Answer №3

Although I am not familiar with angular js, I do understand your issue. If you can find a way to execute this operation, it should help resolve your problem. It is necessary to create the dropdown html below for the selected option 2:

<select ng-change="change_produced_by(model_produced_by, order.Order.id)" style="width: 62px;" ng-model="model_produced_by" id="repeatSelect" name="repeatSelect" class="ng-pristine ng-valid ng-empty ng-touched">
   <option value="">Select Produced By</option>
   <option value="1" ng-repeat="option in produced_by.availableOptions" class="ng-binding ng-scope">Outsourced</option>
   <option ng-selected="true" value="2" ng-repeat="option in produced_by.availableOptions" class="ng-binding ng-scope">In-House</option> 
  <option value="3" ng-repeat="option in produced_by.availableOptions" class="ng-binding ng-scope">Local Store/Printer</option>
</select>

If you are able to dynamically generate html based on your conditions, then proceed to do so...The code snippet provided is an example and may vary in Angular:

<select name="repeatSelect" id="repeatSelect" ng-model="model_produced_by" style="width: 62px;" ng-change="change_produced_by(model_produced_by, order.Order.id)">
     <option value="">Select Produced By</option>
     <option ng-repeat="option in produced_by.availableOptions" value="{{option.id}}" (option.id == order.Order.assigned_to) ? ng-selected="true":"">{{option.name}}</option>
</select>

I hope this provides a solution to your problem.

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

Exploring the functionality of ngTemplateOutlet, the implementation of @ContentChild, and the benefits of using ng

Lately, I've been dedicating more time to grasp the concepts presented in the blog post titled Creating Reusable Components with NgTemplateOutlet in Angular If you want to see the code in action, it's available on stackblitz. Within the UsageEx ...

Dealing with a standalone 404 error in AngularJS's $q

Currently, I have multiple chained $http requests combined with a single $http request using $q.all([promiseA, promiseB]). Everything is going smoothly as expected - data is returned and errors are being handled effectively. However, there are instances w ...

Issue with Bootstrap tab display of content

I'm having trouble with the tabs in my page. When I click on each tab, the content doesn't display correctly. Here is my code: <div class="w470px exam" style="border: 1px solid #ddd; margin-top: 30px;"> <ul id="myTab" class="nav nav ...

Dynamically populate the dropdown options in Angular as the dropdown is selected during runtime

I have a JSON object that holds the names of different dropdowns. It looks something like this - $scope.dropdowns = { "dp1" :{}, "dp2" :{} } The objects dp1 and dp2 correspond to their respective dropdown menus. These objects will be used by th ...

What is the best way to trigger a controller action using jQuery in your application.js file?

Currently, I am incorporating the jQuery autocomplete plugin into my project and looking to personalize a specific event: select: function(event, ui) { $('.topic_field').val(ui.item.topic.name); return false; This event es ...

Displaying a hand cursor on a bar chart when hovered over in c3.js

When using pie charts in c3js, a hand cursor (pointer) is displayed by default when hovering over a pie slice. I am looking to achieve the same behavior for each bar in a bar chart. How can this be done? I attempted the CSS below, but it resulted in the h ...

Assign the input text field's value programmatically in the code-behind of a C# Asp.net application

I am attempting to change the value of an HTML input field from C# code behind. These inputs are created through a JavaScript loop, so I have not had much success using run at server or assigning values through <%= %>. Below is my script: var mytab ...

Running a Node Fetch POST call with a GraphQL query

I'm currently facing an issue while attempting to execute a POST request using a GraphQL query. The error message Must provide query string keeps appearing, even though the same request is functioning correctly in PostMan. This is how I have configur ...

The comparison between using multiple Vue.js instances and components, and implementing Ajax tabs

I am looking to incorporate ajax tabs using vue js. Each tab will need an ajax request to fetch both the template and data from an api. Here is an example of the tabs: <div id="tabs"> <ul class="nav nav-tabs"> <li class="active">& ...

Is there a way to hide an image once my scrollable div has reached a specific height?

I'm currently working on a project that involves a scrollable div with arrows above and below. My goal is to have the arrow above only appear after the div has reached a certain height. Below is the code I've been using... $("#scrolldivup").hid ...

Deliver transcluded data to the descendant element of a hierarchical roster

I understand that there have been similar questions asked before, but my situation is slightly different. I am currently constructing a nested list and I want to include custom HTML content in each grandchild element alongside some common HTML. The problem ...

Dynamic Next.js Redirects configured in next.config.js

After building the project, I am looking to update Redirects routes. Currently, we have redirect routes on our BE Api. My goal is to fetch these redirect routes dynamically and implement them in next.config.js. Here is what I have attempted: const getUrls ...

Transforming a namespaced function into an asynchronous operation by utilizing setTimeout

Looking for help with making a function that uses namespaces asynchronous. The function is currently being called on the click of a button. var ns = { somemfunc: function (data) { alert("hello"); } } Edit: ...

Tips for saving the status of an accordion controlled by an angular directive

I am currently utilizing the accordion directive from angular-bootstrap. My goal is to save the is-open attribute of this accordion, so that when users navigate to another page on the website, the state of the accordion (i.e. is-open) remains unchanged. ...

The building of the module was unsuccessful due to a failure in the babel-loader located in the webpack module of Nuxt

While working on my Nuxt.js project, I encountered a warning error that I couldn't resolve even after searching for a solution. If anyone can assist me with this issue, it would be greatly appreciated. The error message is as follows: ERROR in ./.nu ...

The Chrome extension I created using JQuery to trigger 'keyup' events is not updating the value of the 'input' field on a website that utilizes knockout JS

I am currently working on developing a Google Chrome extension for a website that appears to be utilizing 'knockout JS'. Let's take a look at the HTML element below: <input type="text" class="form-control text-right" id="amount" autoco ...

What is the best way to input information into my Google spreadsheet with React JS?

After using https://github.com/ruucm/react-google-sheets as a reference, I encountered a persistent gapi 404 error whenever I tried to run the code. It seems like the GitHub link might not exist, causing my app to fail. However, I could be mistaken. Is t ...

What are some ways I can efficiently download large attachments without disrupting the user's experience?

I'm encountering some challenges with handling large attachments in my project. Initially, I had this link code: <a :download="scope.row.name" :href="`data:${scope.row.type};base64,${scope.row.contentBytes}`">download</a& ...

adjusting the height of a div using jQuery UI's layout feature

Recently, I have been playing around with the adjustable grids plugin (jquery ui layout) to set the width of each div using the plugins. However, I've encountered a challenge when it comes to adjusting the height of inner divs within the layout. Speci ...

Trouble loading CSS file in Vue library from npm package

When using vue-cli to build a library (npm package) that functions for both SSR and client-side, everything seems to be functioning correctly except for one issue; the CSS only loads if the component is present on the page being refreshed. However, when ac ...