returning multible objects with a property of >= number - angularjs

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) ;
};

Related

ng-model is number, select box option values are strings

I have the following code
<select ng-disabled="currentQuestion.id" ng-change="loadTopics()" class="detail-subject-select browser-default" ng-model="currentQuestion.SubjectId">
<option disabled="disabled" value="any">Choose a Subject</option>
<option value="1">K8-English</option>
<option value="2">K8-Math</option>
</select>
The issue is that value 1 and 2 are strings. I need them to be numbers. Everything works fine when I select one on my page, but I need the select box to initialize with the value of (currentQuestion.SubjectId which is a number) when the page loads.
How can I get around this?
Standard HTML attributes (like "value") always mean strings. Unfortunately, You can only achieve this with ng-options:
Support for select models with non-string values is available via
ngOptions.
https://docs.angularjs.org/api/ng/directive/ngValue
It may seem a bit dirty, but with more options You actually save some typing:
<select
ng-disabled="currentQuestion.id"
ng-change="loadTopics()"
class="detail-subject-select browser-default"
ng-model="currentQuestion.SubjectId"
ng-options="value as key for (key, value) in {
'K8-English': 1,
'K8-Math': 2
}"
>
<option
disabled="disabled"
value=""
>Choose a Subject</option>
</select>
One way is to define your options as an array of objects in your controller like this:
$scope.options = [{
value: 1,
name: 'K8-English'
}, {
value: 2,
name: 'K8-Math'
}];
And implement this in your HTML using ng-options like this:
<select ng-disabled="currentQuestion.id"
ng-change="loadTopics()"
class="detail-subject-select browser-default"
ng-model="currentQuestion.SubjectId"
ng-options="option.value as option.name for option in options">
<option disabled="disabled" value="any">Choose a Subject</option>
</select>

Binding select with ng-options using an array of labels and values

I for the life of me cannot figure out how to set both the labal and the value of a select using an array
I have an array of countries
$scope.countries = [
{abbr:"US", name:"United States"},
{abbr:"CA", name:"Canada"},......
]
I want the select to generate as such
<select>
<option value="US">United States</option>
<option value="CA">Canada</option>
</select>
However the closest I have been able to achieve is
<select>
<option value="1">United States</option>
<option value="2">Canada</option>
</select>
I've achieved that using
<select class="form-control" ng-options="country.Name for country in countries" ng-model="selectedCountry">
How do I assign the label AND the value using ng-options?
Without testing I think it's just
ng-options="country.abbr as country.name for country in countries"
For exact structure, you need to do ng-repeat through your <option><option> ng-options will never set the value which which you want, It will always set 0,1,2,3 etc.
HTML
<select ng-model="selectedCountry">
<option ng-repeat="country in countries" value="{{country.abbr}}" ng-bind="country.name"></option>
</select>
JSFiddle
Hope this could help you, Thanks.

$scope.$apply making dropdown act weird

I have a dropdown;
<select class="form-control" data-ng-model="selected_category" data-ng-change="search(true, true)">
<option value="0">Select Category</option>
<option value="{{category.id}}" data-ng-repeat="category in categories">{{category.name}}</option>
</select>
Which works perfect. However, I am manipulating the scope outside of Angular (valid reason) and I use this (It's in coffeescript but easy to understand)
scope.$apply (s) ->
s.units = _me.attr('data-units')
s.selected_category = parseInt(_me.attr('data-category'))
s.search(true,true)
It appears to work in that everything depending on that $scope.selected_category variable changes (The correct products / text comes up) but the dropdown just goes blank if I've selected Select Category (IE: Not changed it since that change) and stays the same if anything else is selected. Looking at the blank dropdown when I use the element inspector in chrome I can see this:
<option value="? number:31 ?"></option>
What's up with that?
It appears that you are missing the ng-selected attribute on option to let Angular know which option is currently selected:
<select class="form-control" data-ng-model="selected_category" data-ng-change="search(true, true)">
<option value="0">Select Category</option>
<option value="{{category.id}}" data-ng-selected="selected_category.id == category.id" data-ng-repeat="category in categories">{{category.name}}</option>
</select>
However, it is prefered to use ng-options if you don't need to do anything to advanced. So, I'd recommend (as #TheSharpieOne mentions in comments), that you change to this:
<select ng-model="selected_category" ng-options="c.name for c in categories">
<option value="">Select Category</option>
</select>
If you need to track by the id then use a track by clause:
<select ng-model="selected_category" ng-options="c.name for c in categories track by c.id">
<option value="">Select Category</option>
</select>

How can I make AngularJS 1.2 rc store the actual values in a <select>?

I have the following object:
[
{"id":"150c67d4-952b-45b0-b287-f651a5f6d82b","name":"xx"},
{"id":"9001f011-3a0c-4d45-a0fb-eabb4c83ff83","name":"yy"},
{"id":"9b8b93af-cfef-451a-8dda-7373d9154f60","name":"zz"}
]
Here's my HTML:
<select
data-ng-model="option.selectedCreatedBy"
data-ng-options="item.id as item.name for item in option.userProfilesPlus">
<option style="display: none" value="">Select User</option>
</select>
The result is:
<select
data-ng-model="option.selectedCreatedBy"
data-ng-options="item.id as item.name for item in option.userProfilesPlus"
><option style="display: none" value="" class="">Select User</option>
<option value="0" selected="selected">*</option>
<option value="1">xx</option>
<option value="2">yy</option>
<option value="3">zz</option></select>
How can I make this so that the values stored are the actual id's ?
When you are using the ng-option to generate the option list, you don't have control over the values generated for options. For an array it is the index into the array.
If you want to have specific value in the option field, you have to use ng-repeat for it.
In any case, you should not be very concern about specific of html generated, angular can update model correctly in any case.

How to set the value attribute for select options?

Source JSON data is:
[
{"name":"Alabama","code":"AL"},
{"name":"Alaska","code":"AK"},
{"name":"American Samoa","code":"AS"},
...
]
I try
ng-options="i.code as i.name for i in regions"
but am getting:
<option value="?" selected="selected"></option>
<option value="0">Alabama</option>
<option value="1">Alaska</option>
<option value="2">American Samoa</option>
while I am expecting to get:
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AS">American Samoa</option>
So, how to get value attributes and get rid of "?" item?
By the way, if I set the $scope.regions to a static JSON instead of AJAX request's result, the empty item disappears.
What you first tried should work, but the HTML is not what we would expect. I added an option to handle the initial "no item selected" case:
<select ng-options="region.code as region.name for region in regions" ng-model="region">
<option style="display:none" value="">select a region</option>
</select>
<br>selected: {{region}}
The above generates this HTML:
<select ng-options="..." ng-model="region" class="...">
<option style="display:none" value class>select a region</option>
<option value="0">Alabama</option>
<option value="1">Alaska</option>
<option value="2">American Samoa</option>
</select>
Fiddle
Even though Angular uses numeric integers for the value, the model (i.e., $scope.region) will be set to AL, AK, or AS, as desired. (The numeric value is used by Angular to lookup the correct array entry when an option is selected from the list.)
This may be confusing when first learning how Angular implements its "select" directive.
You can't really do this unless you build them yourself in an ng-repeat.
<select ng-model="foo">
<option ng-repeat="item in items" value="{{item.code}}">{{item.name}}</option>
</select>
BUT... it's probably not worth it. It's better to leave it function as designed and let Angular handle the inner workings. Angular uses the index this way so you can actually use an entire object as a value. So you can use a drop down binding to select a whole value rather than just a string, which is pretty awesome:
<select ng-model="foo" ng-options="item as item.name for item in items"></select>
{{foo | json}}
If you use the track by option, the value attribute is correctly written, e.g.:
<div ng-init="a = [{label: 'one', value: 15}, {label: 'two', value: 20}]">
<select ng-model="foo" ng-options="x for x in a track by x.value"/>
</div>
produces:
<select>
<option value="" selected="selected"></option>
<option value="15">one</option>
<option value="20">two</option>
</select>
If the model specified for the drop down does not exist then angular will generate an empty options element. So you will have to explicitly specify the model on the select like this:
<select ng-model="regions[index]" ng-options="....">
Refer to the following as it has been answered before:
Why does AngularJS include an empty option in select? and this fiddle
Update: Try this instead:
<select ng-model="regions[index].code" ng-options="i.code as i.name for i in regions">
</select>
or
<select ng-model="regions[2]" ng-options="r.name for r in regions">
</select>
Note that there is no empty options element in the select.
You could modify you model to look like this:
$scope.options = {
"AL" : "Alabama",
"AK" : "Alaska",
"AS" : "American Samoa"
};
Then use
<select ng-options="k as v for (k,v) in options"></select>
It appears it's not possible to actually use the "value" of a select in any meaningful way as a normal HTML form element and also hook it up to Angular in the approved way with ng-options. As a compromise, I ended up having to put a hidden input alongside my select and have it track the same model as my select, like this (all very much simplified from real production code for brevity):
HTML:
<select ng-model="profile" ng-options="o.id as o.name for o in profiles" name="something_i_dont_care_about">
</select>
<input name="profile_id" type="text" style="margin-left:-10000px;" ng-model="profile"/>
Javascript:
App.controller('ConnectCtrl',function ConnectCtrl($scope) {
$scope.profiles = [{id:'xyz', name:'a profile'},{id:'abc', name:'another profile'}];
$scope.profile = -1;
}
Then, in my server-side code I just looked for params[:profile_id] (this happened to be a Rails app, but the same principle applies anywhere). Because the hidden input tracks the same model as the select, they stay in sync automagically (no additional javascript necessary). This is the cool part of Angular. It almost makes up for what it does to the value attribute as a side effect.
Interestingly, I found this technique only worked with input tags that were not hidden (which is why I had to use the margin-left:-10000px; trick to move the input off the page). These two variations did not work:
<input name="profile_id" type="text" style="display:none;" ng-model="profile"/>
and
<input name="profile_id" type="hidden" ng-model="profile"/>
I feel like that must mean I'm missing something. It seems too weird for it to be a problem with Angular.
you can use
state.name for state in states track by state.code
Where states in the JSON array, state is the variable name for each object in the array.
Hope this helps
Try it as below:
var scope = $(this).scope();
alert(JSON.stringify(scope.model.options[$('#selOptions').val()].value));

Resources