Multiple ng-options set default option select not working - javascript

I´m having an issue with to select a default value on a select with Angular.
If my select options only has one value it´s ok and works fine
<select id="customerId"
ng-model="customer"
ng-options="customer.id for customer in customersSelect">
</select>
In my angular js:
$scope.customer = customer;
But if I try with multiple elements on ng-options and I set the default option, is not working
<select id="customerId"
ng-model="customer"
ng-options="customer.id as customer.name for customer in customersSelect">
</select>
Any ideas?

You can use a option tag.like below:
<select
id="customerId"
ng-model="customer"
ng-options="customer.id as customer.name for customer in customersSelect">
<option value="">PLease select one</option>
</select>
Thats it.

Related

How to get default value in dropdown list?

I have a drop down list and it has two values. if user didn't select none of the value it should set a default value.
<select class="form-control" ng-model="abc.code" required>
<option ng-repeat="x in codeList" value="{{x}}" ng-selected="codeList[1]" >
{{x}} </option>
</select>
Can someone help me.
you can add something like this
<option value="didnt select" selected hidden>please select one option</option>
for default value statically in your select input

Create a dynamic submenu in dropdown

I have reasons for rescheduling job and also sub-reason under a reason. I want that on selecting the reason, its sub-reason should appear. How do I do that?
<select>
<option value="" disabled="disable" selected="selected">
Select Reason
</option>
<option ng-repeat="reasons in Resonse_schedule ">
{{reasons.reasons}}
</option>
</select>
Use ng-options
AngularJS provides a directive ng-options to dynamically list down the options
<select ng-options="reason.reason as reason.reason for reason in Resonse_schedule">
<option value="" selected disabled >Select Reason</option>
</select>
I would suggest you to go through the documentation of ngOptions to understand it better
You can use this:
<select ng-model="reasonVar" ng-options="reason as reason.name for reason in Resonse_schedule">
<option value="" disabled="disable" selected="selected">Select Reason
</option>
</select>
<select ng-model="reasonSelected">
<option ng-repeat="subReason in reasonVar.subReasons" value="subReason.name">
{{subReason.name}}
</option>
</select>
Where reasonVar is the selected value from the Resonse_schedule list.
Here's a working example for this code:
http://jsfiddle.net/U3pVM/35122/
And here's more complicated one:
http://jsfiddle.net/miparnisari/u50hgn5a/

Toggling ng-selected

I am trying to toggle ng-selected options in Angular, but am running into some difficulty. Here's what I'm trying:
<select ng-model="datacut.ages" multiple>
<option value="" disabled="disabled">Please Select</option>
<option value="0-15" ng-click="toggleSelect(datacut, '0-15')" ng-selected="datacut.ages.indexOf('0-15') !== -1">15 and Younger</option>
<option value="16-19" ng-selected="datacut.ages.indexOf('16-19') !== -1">16 - 19</option>
<option value="20-24" ng-selected="datacut.ages.indexOf('20-24') !== -1">20 - 24</option>
</select>
Controller:
$scope.toggleSelect = function(dc, str){
dc.ages.splice(dc.ages.indexOf(str), 1);
//e.currentTarget.selected = !e.currentTarget.selected;
};
The problem is that when I click on an option gets selected on mousedown, and on release gets unselected. The commented out code also does the same thing.
I feel like this should have a simple solution, but I can't really figure out an elegant solution.
Any help would be much appreciated.
Edit: For clarification - I need to be able to have nothing selected, so clicking a single selected element needs to unselect it. I also need to be able to select multiple options.
Your are using ng-model which does the magic for you. So ng-click and ng-selected is not necessary. See my working fiddle
<select ng-model="datacut.ages" multiple>
<option value="" disabled="disabled">Please Select</option>
<option ng-value="'0-15'">15 and Younger</option>
<option ng-value="'16-19'" >16 - 19</option>
<option ng-value="'20-24'" >20 - 24</option>
</select>
It looks like you want to remove the selected age from the ages model. Then you need to use the ng-change directive. And remove the ng-click or ng-selected. But leave the ng-model to access the value on the method that remove the selected item.
See my plunker
<select multiple name="ages" unseletable forbiden-opt="datacut.MIN_AGE" ng-model="datacut.chosenAge">
<option value="" disabled="disabled">Please Select</option>
<option ng-repeat="age in datacut.ages" value="{{age}}">
{{age}}
</option>
</select>
EDIT: This should be made in a directive because you need to update the option html to unselect the option.

How do I set a prompt on a select using ng-options?

I have a select that uses ng-options to populate the select as follows:
<select class="form-control"
ng-model="Data.selectedPerson"
ng-options="v as (v.Name ) for v in Data.people track by v.Name">
</select>
I don't want to add a value to the collection for a default if the people collection is empty or no value is selected yet. I would like to have a prompt in the select to encourage them to use the select. Thanks for your suggestions.
Just add a default option, just so angular will use this option when there is nothing selected in the ngModel or an invalid item is populated in the model. This way you don't need to add an empty value in your collection.
<select class="form-control"
ng-model="Data.selectedPerson"
ng-options="v as v.Name for v in Data.people track by v.Name">
<!-- Add your default option here -->
<option value="">Please select a person</option>
</select>
You could also change the text based on the condition:-
<option value="">{{ Data.people.length ? "Please select a person" : "No one available for selection" }}</option>
You can also remove it from DOM if it has already a selected value.
<option ng-if="!Data.selectedPerson" value="">Please select a person</option>

AngularJS - extra blank option added using ng-repeat in select tag

I have a listbox that I am creating with a select, using AngularJS ng-repeat. The listbox is created correctly, and when I select one of the items and click my button, I get to the function and have the information I need.
My html code is as follows:
<select size="6" ng-model="item" ng-options="s.name for s in itemlist"></select>
<button class="btn tt-btn-blue" ng-model="singleModel" ng-click="onLoadClicked(item.id)">Load</button>
My problem is that when the listbox paints, it has one item at the top that is blank. When I inspect the listbox during a run in Chrome, I get the following output in the console:
<select size="6" ng-model="item" ng-options="s.name for s in itemlist" class="ng-pristine ng-valid">
<option value="?" selected="selected"></option>
<option value="0">Item 1</option>
<option value="1">Item 2</option>
<option value="2">Item 3</option>
<option value="3">Item 4</option>
<option value="4">Item 5</option>
<option value="5">Item 6</option>
</select>
I am wondering how I can get rid of the first option inserted by the ng-repeat. I don't want to see a blank space at the top of the listbox. I realize one option would be to set the first actual option (value="0") as the selected item, but I would rather have no selected items to start.
Any time you see <option value="?" selected="selected"></option> in the select, it means that your ng-model is set to a value that isn't in the ng-options. So, if you don't want the blank option, you need to make sure item is set to a value in your itemlist. This could be as easy as having the following in your controller:
$scope.item = $scope.itemlist[0];
This will give it an inital value, and then angular will update it for you thereafter.
simplest way to make sure that automatically added option by angular is hidden is ng-if directive.
<select ng-options="option.id as option.description for option in Options">
<option value="" ng-if="false"></option>
</select>
I have found the solution after a long RND
Please find the following code it will work for you.
Here is the HTML Code
<div class="col-xs-3" ng-controller="GetUserADDetails">
<label>Region</label>
<select id="DDLRegion" ng-model="selectedregion" class="form-control" ng-change="getBranched(selectedregion)">
<option value="" >--Select Region--</option>
<option ng-repeat="region in Regions" value="{{region.LookupID}}">{{region.LookupValue}}</option>
</select>
</div>
Here is the Module Code
var app = angular.module("UserMasterModel", [])
.controller("GetUserADDetails", function ($scope, $http,$log) {
$http({
method: 'GET',
url: '/UserMaster/GetAllRegion'
})
.then(function (response) {
$scope.Regions = response.data;
//$log.info(response);
});
});

Categories

Resources