Working with select boxes in angularjs (selected index and default option) - angularjs

I'm new at angular. I used to have a select box that was populated using ng:repeat:
<select ng-model="selectedAddress" ng-change="handleStoredAddressClick2()" id="address_dropdown">
<option value="-1" selected="selected">Add a new address</option>
<option ng:repeat="stored_address in address_dropdown" value="{{$index}}">{{stored_address.dropdown_display}}</option>
</select>
The HTML it generated looks like this:
<select id="address_dropdown" ng-change="handleStoredAddressClick2()" ng-model="selectedAddress" class="ng-pristine ng-valid">
<option value="? undefined:undefined ?"></option>
<option selected="selected" value="-1">Add a new address</option>
<option value="0" ng:repeat="stored_address in address_dropdown" class="ng-scope ng-binding">602 BLOWING ROCK RD APT. 2 BOONE, NC 28607</option>
<option value="1" ng:repeat="stored_address in address_dropdown" class="ng-scope ng-binding">TEST HOUSE: 9884 BLUEBONNET BLVD BATON ROUGE, LA 70810</option>
</select>
I have a function that runs on ng-click:
$scope.handleStoredAddressClick2 = function () {
var address = $scope.address_dropdown[$scope.selectedAddress];
$scope.streetaddr = address.address
$scope.search_near = address.zip;
var disp_addr = address.address;
if (address.address_two) {
disp_addr += ', ' + address.address_two;
}
if (typeof disp_addr != 'undefined' && disp_addr.toTitleCase) {
disp_addr = disp_addr.toTitleCase();
}
$scope.streetaddr = disp_addr;
$scope.stored_addr_key = address.key;
$scope.cityname = address.city;
$scope.statename = address.state;
$scope.fetch();
};
The second line of the function grabbed the {{$index}} value that was set in the ng:repeat value property. (So, 0 or 1 in this case.)
I was recently told that you shouldn't use ng:repeat to populate select boxes; that you should use ng-options instead. So I updated my select box to this:
<select ng-model="selectedAddress" ng-init="selectedAddress = selectedAddress || address_dropdown[0].value" ng-change="handleStoredAddressClick2()" ng-options="a.dropdown_display for a in address_dropdown" id="address_dropdown">
<option value="-1" ng-selected="selected">Add a new address</option>
</select>
This is the HTML that generates:
<select id="address_dropdown" ng-options="a.dropdown_display for a in address_dropdown" ng-change="handleStoredAddressClick2()" ng-model="selectedAddress" class="ng-pristine ng-valid">
<option value="?" selected="selected"></option>
<option value="0">602 BLOWING ROCK RD APT. 2 BOONE, NC 28607</option>
<option value="1">TEST HOUSE: 9884 BLUEBONNET BLVD BATON ROUGE, LA 70810</option>
</select>
It's essentially the same, especially the option values. But I now get an error on change, saying address is undefined. So for some reason, $scope.address_dropdown[$scope.selectedAddress]; no longer works to grab the selected index value of the dropdown on change, even though the actual index values are the same regardless of how the code is generated.
Can anyone explain why that is the case, and how I can get the selected index value of a select box populated with ng-options? Can I also get an answer as to why the text of the default selected option I set in the second set of code (repeated below) ends up displaying blank?
Code:
Add a new address
Rendered:

I'm not following this example completely, but it looks like you are trying to retrieve the address selected in the dropdown?
So you have this:
ng-options="a.dropdown_display for a in address_dropdown"
and this:
ng-model="selectedAddress"
Your dropdown is bound to the members of address_dropdown, so this code is unnecessary:
var address = $scope.address_dropdown[$scope.selectedAddress];
$scope.selectedAddress should already contain the complete address object that is a member of address_dropdown. No need to muck about with an index.

Related

Angular - Cascading dropdown behaviour for Single Select Box

Usually We do cascading Dropdown list with separate Select Box. Like below Image.
My requirement is quite different. I want to add only Single Select Box which will act as Cascading behaviour.
Simple Example process : (yourSelectBox - name of Select Box)
1) At First Instance It will show countries name in yourSelectBox
2) lets say , If you select/click India as Coutry,
3) now yourSelectBox should have States List like Maharashtra, Gujrat etc
4) If you select Maharashtra as your state,
5) how yourSelectBox should have City List.
6) finally you select your city.
Refer below image.
Can we flush values of yourSelectBox by keeping eye on event(using $watch on selected Value) ?
HTML
<select ng-mode="selcountry" ng-change="getStates();">
<option value="coutry.id" ng-repeat="coutry in countries"> {{coutry.name}}</option>
</select>
<select ng-mode="selstate" ng-change="getCities();">
<option value="state.id" ng-repeat="state in states"> {{state.name}}</option>
</select>
<select ng-mode="selcity" >
<option value="city.id" ng-repeat="city in cities"> {{city.name}}</option>
</select>
Controller :
$scope.getStates = function (){
//$scope.selcountry
//call get states where counties is $scope.selcountry
}
$scope.getStates = function (){
//$scope.selcountry
//call get states where counties is $scope.selcountry
$scope.states= result //set states to
}
$scope.getCities = function (){
//$scope.selstate
//call get states where counties is $scope.selstate
$scope.cities = result //set cities to
}
if you want the complete Login in Single Select Menu Then:
<select>
<optgroup label="India">
<optgroup label="Gujarat">
<option value="1">Gandhi Nagar</option>
<option value="2">Porubandar</option>
</optgroup>
<optgroup label="Delhi">
<option value="3">Delhi-6</option>
<option value="4">city</option>
</optgroup>
</optgroup>
</select>
Refer

angularjs ng-repeat selected option

How to get the object of selected option in angular js using ng-repeat? Sample fiddle is here where myself populating the dropdown as,
<select ng-model="id" ng-init="id = '-1'" ng-change="select(option)">
<option value="-1">--Select--</option>
<option ng-repeat="option in list" value="{{option.id}}">{{option.name}}</option>
</select>
If someone chose option with id=1, i want to get the object {id:1, name:'A' in $scope variable.
EDIT: Myself tried with ng-options but filtering is not happening as required based on previous dropdown selection. sample fiddle
Use option object in ng-value={{option}} and whenever u select an option your scope.id will have the desired object.
Basically ur ng-model gets populated with selected value.
Just you have to change your option to this
<option ng-repeat="option in list" value="{{'Id : ' + option.id + ' Name : ' + option.name }}">{{option.name}}</option>
Hope this will work for you and you were looking for the same result
You have 2 option to get as Object.
option 1 :
you can use Filter to get Object
<select ng-model="id" ng-init="id = '-1'" ng-change="select((list|filter:{id:id}))">
<option value="-1">--Select--</option>
<option ng-repeat="option in list" value="{{option.id}}">{{option.name}}</option>
</select>
updated fiddler here
option 2 :
set your value as Object (option)
<select ng-model="id" ng-init="id = '-1'" ng-change="select(option)">
<option value="-1">--Select--</option>
<option ng-repeat="option in list" value="{{option}}">{{option.name}}</option>
</select>

AngularJS ng-model for select is not setting the selected="selected" attribute

I have an AngularJS page for editing surgeries, but I can't get any of the <select>'s to show the current value. All the other fields in the form show their current values so I'm not sure what I'm doing wrong.
I have a factory set up with ngResource, this is in my controller.
// Supporting Data
SurgeryData.get({'accessToken':$rootScope.accessToken})
.$promise.then(function(response){
$scope.surgeryData = response.surgeryData;
});
// Surgery Data
SurgeryServices.get({'surgeryId':$stateParams.surgeryId,'accessToken':$rootScope.accessToken})
.$promise.then(function(response){
$scope.surgeryDetails = response;
$scope.formData.surgeryId = $scope.surgeryDetails.surgery.id;
...
$scope.formData.surgeryStatus = $scope.surgeryDetails.surgery_status;
...
$log.log( angular.toJson( $scope.formData.surgeryStatus ) );
$log.log( angular.toJson( $scope.surgeryData.surgery_status ) );
});
This is the HTML for Surgery Status
<select class="form-control" ng-model="formData.surgeryStatus" ng-options="status.name for status in surgeryData.surgery_status"></select>
and this is what I get in the console from my 2 $logs
$scope.formData.surgeryStatus
{"id":16,"name":"Ready For Pick-Up"}
$scope.surgeryData.surgery_status
[{"id":13,"name":"Inventory Not On Site"},{"id":14,"name":"Inventory On Site"},{"id":16,"name":"Ready For Pick-Up"},{"id":15,"name":"Sterilized For Surgery"}]
The <select>'s in the form have all the options but (for this example) Ready for Pick-Up is not selected and I have an empty ? option added to the select
<select class="form-control ng-pristine ng-valid" ng-model="formData.surgeryStatus" ng-options="status.name for status in surgeryData.surgery_status">
<option value="?"></option>
<option value="0">Inventory Not On Site</option>
<option value="1">Inventory On Site</option>
<option value="2">Ready For Pick-Up</option>
<option value="3">Sterilized For Surgery</option>
</select>
ng-init should be the way to go.
View:
<select class="form-control ng-pristine ng-valid" ng-init="formData.surgeryStatus = selected" ng-model="formData.surgeryStatus" ng-options="status.name for status in surgeryData.surgery_status track by status.id">
<option value="?"></option>
<option value="0">Inventory Not On Site</option>
<option value="1">Inventory On Site</option>
<option value="2">Ready For Pick-Up</option>
<option value="3">Sterilized For Surgery</option>
</select>
Viewmodel:
$scope.selected = $scope.surgeryDetails.surgery_id;
ng-init will find the matching value from your options and select it when loading. Be sure to set your ng-model equal to whatever value you want selected, which looks like your Id in this case?

Default selected value not working using ng-init

Simply trying to make a default selected value in Angular but thus far I am having no luck.
HTML Before Compile:
<select ng-options="select.Value as select.Name for select in ruleSelect" ng-model="rule.MatchLogic" ng-init="rule.MatchLogic = 0" ></select>
HTML In Browser Source:
<select ng-options="select.Value as select.Name for select in ruleSelect" ng-model="rule.MatchLogic" class="ng-pristine ng-valid">
<option value="?" selected="selected"></option>
<option value="0">All</option>
<option value="1">At Least</option>
<option value="2">At Most</option>
</select>
How do I make value=0 (All) the default selected value.
All you need to do is set your ng-model value rule.MatchLogic to equal the corresponding value in the options. The options you use are ruleSelect.
So for example, set rule.MatchLogic = ruleSelect[someSelector].Value; inside your controller.

returning multible objects with a property of >= number

I have managed to filter out what i want, the problem is it only brings back the first object in the database that matches it. i am trying to have a select box with filter by the job salary minimum. $50,000 $60,000 and so on. for example, when i select the $50,000 option the only result returned is jobs that have there salary set at exactly 50000. I need everything returned that is above 50000. any help is appreciated.
Controller
$scope.salaryFilter = function (min) {
return min.salaryMin >= $scope.salary_Min ;
}
View
<label>Salary Desired:</label><br />
<select class="form-control" ng-model="search.salaryMin">
<option value="">Select</option>
<option ng-repeat="salary in jobArray | filter:salaryFilter"></option>
<option ng-model="salary_salaryMin" value="50000">$50,000</option>
<option ng-model="salary.salaryMin" value="60000">$60,000</option>
<option ng-model="salary.salaryMin" value="70000">$70,000</option>
<option ng-model="salary.salaryMin" value="80000">$80,000</option>
<option ng-model="salary.salaryMin" value="90000">$90,000</option>
<option ng-model="salary.salaryMin" value="100000">$100,000</option>
</select><br />
Here's a plunker with a basic example of how to do this.
From the behaviour you describe, it sounds like you may be matching strings rather than integers, but we won't know if you don't show us more code
Controller:
$scope.data = [
{
"val":27000,
"text":"$27,000"
},{
"val":29000,
"text":"$29,000"
},{
"val":50000,
"text":"$50,000"
},{
"val":70000,
"text":"$70,000"
}];
$scope.name = "John";
$scope.salaryMin = 27000;
$scope.salaryFilter = function(salary) {
if (salary.val >= $scope.salaryMin)
return true;
}
Template:
<select >
<option ng-repeat="salary in data | filter: salaryFilter" value="{{ salary.val }}">{{ salary.text }}</option>
</select>
Changing the value of $scope.salaryMin will filter the data
Edit
The problem is shown in this plunker, and i've created a fork here which fixes the issue
There were a couple problems
We're setting the individual salary options manually in the option list, so there's no need for the ng-repeat in our salary select box. We also need this select box to set the $scope.salary_Min variable, so we shouldn't have ng-model set to salary.salaryMin on the option tags.
<!-- REMOVE THIS -->
<option ng-repeat="salary in jobArray | filter:salaryFilter"></option>
<!-- REMOVE ng-model FROM these -->
<option ng-model="salary_salaryMin" value="50000">$50,000</option>
<option ng-model="salary.salaryMin" value="60000">$60,000</option>
<option ng-model="salary.salaryMin" value="70000">$70,000</option>
<option ng-model="salary.salaryMin" value="80000">$80,000</option>
<option ng-model="salary.salaryMin" value="90000">$90,000</option>
<option ng-model="salary.salaryMin" value="100000">$100,000</option>
Because we want the select box to set the $scope.salary_Min variable (which is used in our filter), we add ng-model to the select tag
<select class="form-control" ng-model="salary_Min">
We previously had the salaryFilter on our option tag, but we want to use it to filter the table. So we add it to the table where we're using ng-repeat. It's okay to keep the existing search filter because filters in Angularjs are stackable
<tr ng-repeat="job in jobArray | filter:search | filter:salaryFilter">
<td>{{job.jobTitle}}</td>
<td>{{job.companyName}}</td>
<td>{{job.description}}</td>
<td>{{job.city}}</td>
<td>{{job.salaryMin}}</td>
</tr>
This is nearly done. The one issue left is that these values are being interpreted as strings by our filter, which can cause some confusing results. We just need to make sure they're interpreted as integers using parseInt()
$scope.salaryFilter = function (min) {
return parseInt(min.salaryMin) >= parseInt($scope.salary_Min) ;
};

Resources