I am creating select element and set the values with array
In my controller I set
$scope.numbers = _.range(20);
$scope.selectedNumber = 5;
and my html
<select ng-model='selectedNumber'
class='some-class'
ng-options="item as item for item in numbers" >
</select>
the css of the class is
.some-class{
border:1px solid gray;
border-radious:xxx;
width:xxpx;
heihgt:xxpx;
}
my problem is that the selected value does not rendered on page load
only after clicking inside the select element.
(also happens when I use ng-class instead of class).
if I remove the class then then it just renders fine.
(another quick question : why if I don't add ng-model the ng-options does not display any content
I've prepared few examples for you that I hope will help you understand, I don't know what's your $scope.numbers type if it's an array of integers, strings, or objects but the model has to be the same type, and if you want model to be object then it has to be passed by reference. Although to answer your question ng-model is mandatory when ng-options are used, if you don't want model you can use this pattern
<select>
<option ng-repeat="number in numbers">{{number}}</option>
</select>
And here are different input types for the select, please visit plunker to see it working
$scope.numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
$scope.selectedNumber = 5
$scope.numbersTwo = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
$scope.selectedNumberTwo = '6'
$scope.numbersThree = [{
num: 0
}, {
num: 1
}, {
num: 2
}, {
num: 3
}, {
num: 4
}, {
num: 5
}, {
num: 6
}, {
num: 7
}, {
num: 8
}, {
num: 9
}, {
num: 10
}]
$scope.selectedNumberThree = $scope.numbersThree[7]
$scope.selectedNumberFour = 8
http://plnkr.co/edit/7mzuj8Fg46xVv6bwhiOt?p=preview
ng-options needs to be matching with ng-model in order to render the value.
For example if you ng-model is giving value of "Boston", you need to have that key/value pair or just the value in the object/array of your ng-options list. Its case sensitive as well.
Controller:
$scope.city = "Boston";
$scope.cityDetails = service.cityList;
Service:
$scope.cityList = {"Boston", "San Jose", "Chicago", "New York"};
so while trying to loop down, you need to loop in this way.
<select ng-model="city" ng-options="cityData for cityData in cityDetails"></select>
Related
let's say with the select option(dropdown) on the visualforce page I have INDIA, USA, UK. if I select INDIA that should be displayed in visual force page and if I open again dropdown the selected value or displayed value on visualforce page, it should not display INDIA, it should display the only USA and the UK
You could do that using javascript in your visualforce page.
you need to include a js file like this:
<apex:includeScript value="{!$Resource.MyJavascriptFile}"/>
Store the values as options like this:
Options = [
{ label: '1', value: '1' },
{ label: '2', value: '2' },
{ label: '3', value: '3' },
{ label: '4', value: '4' },
];
Then your code need to detect a selected option you could do this like this:
var options = document.getElementsByName('{!$Component.foo}')
for(option in options)
if(options[option].selected)
alert('The selected value is: ' + options[option].value)
Once a value is selected you need to deleted from the option array like this:
let Selectedvalue = 3
let arr = [1, 2, 3, 4, 5, 3]
arr = arr.filter(item => item !== value)
I am trying to filter a list by a property that is set by a dropdown:
<select ng-model="filterItem" ng-options="item.name for item in filterOptions.stores">
</select>
This is the the data for the dropdown:
$scope.filterOptions = {
stores: [
{id : 2, name : 'Show All', rating: 6 },
{id : 3, name : 'Rating 5', rating: 5 },
{id : 4, name : 'Rating 4', rating: 4 },
{id : 5, name : 'Rating 3', rating: 3 },
{id : 6, name : 'Rating 2', rating: 2 },
{id : 7, name : 'Rating 1', rating: 1 }
]
};
The filter is actually not working properly through the rating property: it returns more that just the one selected property:
<li data-ng-repeat="item in data | filter:filterItem.rating" >
Name: {{item.name}} Price: {{item.price}} Rating: {{item.rating}}
</li>
How can I fix the filter here?
This is a plunkr:http://plnkr.co/edit/vAebEb?p=preview
Right now you are setting filterItem.rating as the filter, so you're setting a string as the filter. From docs (https://docs.angularjs.org/api/ng/filter/filter):
The string is used for matching against the contents of the array. All
strings or objects with string properties in array that match this
string will be returned. This also applies to nested object
properties. The predicate can be negated by prefixing the string with
!.
What this is doing is matching ANY property in your array with the value filterItem.rating; so product's with the number in their product name are also being matched.
Since you want the filter to ONLY apply to the rating property, you want to set an object like this:
<li data-ng-repeat="item in data | filter:{rating:filterItem.rating}" >
That will now filter properly, but Show All doesn't work properly since filterItem.rating is undefined when it's show all. Since we don't want the filter to apply when Show All is selected, you can check for the filterItem.rating property first:
<li data-ng-repeat="item in data | filter:filterItem.rating && {rating:filterItem.rating}" >
I've updated your Plunkr here: http://plnkr.co/edit/q5Ns8c6HN1eIGZANlI4s?p=preview
You need to make a couple of changes:
In your $scope.filterOptions.stores object: Remove the rating for Show All option like:
{id : 2, name : 'Show All'},
In your html where you are using filter:
<li data-ng-repeat="item in data | filter:filterItem.rating && {rating:filterItem.rating}">
Hope this helps.
I'm binding a select element to data that looks like this:
var gearConditions = [
{ id: 1, value: 'New' },
{ id: 2, value: 'Like New' },
{ id: 3, value: 'Excellent' },
{ id: 4, value: 'Good' },
{ id: 5, value: 'Fair' },
{ id: 6, value: 'Very Used' },
{ id: 7, value: 'Other' }
];
My select looks like this:
<select name="condition"
class="form-control"
ng-model="postModel.condition"
ng-options="condition.id as condition.value for condition in gearConditions">
</select>
This binds condition.id to the ng-model and displays condition.value as the select option. My question is: can I keep the current behavior but bind the whole object to the ng-model?
For example if the first option is selected the model value would then be {id:1, value: 'New'} rather than just 1.
Is this possible or do I need to use ng-changed to accomplish this?
Thanks!
Yes, change condition.id to condition:
ng-options="condition as condition.value for condition in gearConditions"
I think you you try similar:
// I took this from my code
ng-options="key as value for (key, value) in ...
Now you could try to combine key+value and see if it appears like you wish.
I have an JSON array from where i am creating Dropdown and default selected using ng-model
$scope.repeats = [{
'title': 'Never',
'value': 'Never'
}, {
'title': 'Daily',
'value': 'Daily'
}, {
'title': 'Weekdays (M-F)',
'value': 'Weekdays'
}, {
'title': 'Weekly',
'value': 'Weekly'
}, {
'title': 'Monthly',
'value': 'Monthly'
}, {
'title': 'Yearly',
'value': 'Yearly'
}, {
'title': 'Hourly',
'value': 'Hourly'
}, {
'title': 'Minutely',
'value': 'Minutely'
}];
$scope.def_sele = $scope.repeats[0]
In the view:
<select ng-model="def_sele" ng-options="repeat.title for repeat in repeats"></select>
From the above code the default selected dropdown is Daily. Till here it was all fine.
But i am creating an TODO application where each TODO has different selection(Never, Daily etc.. from above $scope.repeats)
So, how to set default selected for each TODO ????? Because the above example i've given it applies every where and i get Daily selected on each TODO, but i want to select different different for each TODO, how to do that????
Updated
My code is like:
<ul ng-repeat="todo in todos_detail">
<select ng-model="todo.dropdown_position_index" ng-options="repeat.title for repeat in repeats"></select>
</ul>
In the above example for each todo has a different dropdown_position_index, so i want the dropdown is selected = selected using todo.dropdown_position_index.
You need to make sure the value you're selecting in your <select> lines up with the value in your selected todo.
I haven't seen your code, so I put together a working example in jsbin.
The basic idea is this:
<select ng-model="X" ng-options="x.A as x.B for x in Z"></select>
Where:
Z = your collection of options
x = an individual item variable from Z
x.A = the value of the option
x.B = the text of the option
X = the value to update AND read from
as long as the array Z has an object in it x where the property x.A is equal to X, it will be selected.
... or for a less confusing answer, check the code example. ;)
Best of luck
given for the example object structure
$scope.todos = [{
'title': 'Weekly',
'value': 'Weekly'
}, {
'title': 'Yearly',
'value': 'Yearly'
}];
you could do something like this
<div ng-repeat="todo in todos">
<select ng-model="todo.value" ng-options="repeat.value as repeat.title for repeat in repeats"></select>
</div>
here an example plunker
I have a <select> that I am populating with a list of values. Everything works and I see the expected list. But now I would like to have the value set to a locally stored value if available.
I have the code below. When I run this code the number after the Type in the label changes to match that in the localstorage but the select box does not change.
getContentTypeSelect: function ($scope) {
entityService.getEntities('ContentType')
.then(function (result) {
$scope.option.contentTypes = result;
$scope.option.contentTypesPlus = [{ id: 0, name: '*' }].concat(result);
$scope.option.selectedContentType
= localStorageService.get('selectedContentType');
}, function (result) {
alert("Error: No data returned");
});
},
<span class="label">Type {{ option.selectedContentType }}</span>
<select
data-ng-disabled="!option.selectedSubject"
data-ng-model="option.selectedContentType"
data-ng-options="item.id as item.name for item in option.contentTypesPlus">
<option style="display: none" value="">Select Content Type</option>
</select>
Here is the code I have that sets the value in localstorage:
$scope.$watch('option.selectedContentType', function () {
if ($scope.option.selectedContentType != null) {
localStorageService.add('selectedContentType',
$scope.option.selectedContentType);
$scope.grid.data = null;
$scope.grid.selected = false;
}
});
Here is the data stored in contentTypesPlus:
0: Object
id: 0
name: "*"
__proto__: Object
1: b
id: 1
name: "Page"
__proto__: b
2: b
id: 2
name: "Menu"
__proto__: b
3: b
id: 3
name: "Content Block"
__proto__: b
How can I make the select box go to the localstorage value if there is one?
Update
Still hoping for an answer and I would be happy to accept another if some person can help me. The answer given is not complete and I am still waiting for more information. I am hoping someone can give me an example that fits with my code. Thanks
I think you need to make sure selectedContentType needs to be the id field of the option object according to the comprehension expression defined in the select directive.
The comprehensive expression is defined as
item.id as item.name for item in option.contentTypesPlus
item.id will be the actual value stored in the ng-model option.selectedContentType
Say this is your select with ng-model
<select
ng-model="language.selected"
ng-init="language.selected = settings.language[settings.language.selected.id]"
ng-options="l.label for l in settings.language"
ng-change="updateLanguage(language.selected)">
In your controller
$scope.settings = {
enableEventsNotification: true,
enableiBeaconNotification: true,
hours: [
{ id: 0, label: '3h', value: 3 },
{ id: 1, label: '6h', value: 6 },
{ id: 2, label: '9h', value: 9 },
{ id: 3, label: '12h', value: 12 },
{ id: 4, label: '24h', value: 24 }
],
language: [
{ id: 0, label: 'English', value: 'en-us' },
{ id: 1, label: 'Francais', value: 'fr-fr' }
]
};
$scope.hours = [];
$scope.hours.selected = $localstorage.getObject('hours', $scope.settings.hours[1]);
$scope.settings.hours.selected = $localstorage.getObject('hours', $scope.settings.hours[1]);
$scope.hours.selected = $scope.hours.selected;