Selected element in Select - angularjs

I need to pre-select a specfic element in a Select tag.
My model is this:
{
user: 'mjones',
idprofile: 2,
profiles: [
{idprofile: 1, profile: 'admin'},
{idprofile: 2, profile: 'read-write'},
{idprofile: 3, profile: 'read-only'},
]
}
This represents a "user", and I attached a list of profiles to it, before opening a form with user-data, containing a select, for choosing the appropriate profile.
As you can see, the user object doesn't contain a "profile" object, but just it's idprofile. That's the difference between my object and those published in ngSelect documentation.
My view contains the select element as this:
<select class="form-control"
ng-model="user"
ng-options="profile.idprofile for profile in user.profiles">
</select>
This populates the list as expected, but it does not selects the idprofile: 2.
How can I match the idprofile from the user object with one element in the profiles array?, I prefer to work only on the view.

Your ng-model must be set to user.idprofile and not to the user.
<select class="form-control"
ng-init="user = user || profile[0]"
ng-model="user.idprofile"
ng-options="item.idprofile as item.profile for item in user.profiles">
</select>
the ng-options pattern is "select as label for value in array" (https://docs.angularjs.org/api/ng/directive/select)
The code would be easier to read with a few renames:
{
user: 'mjones',
idprofile: 2,
profiles: [
{id: 1, name: 'admin'},
{id: 2, name: 'read-write'},
{id: 3, name: 'read-only'},
]
}
<select class="form-control"
ng-init="user = user || profile[0]"
ng-model="user.idprofile"
ng-options="profile.id as profile.name for profile in user.profiles">
</select>

Related

Angular dropdown doesn't load ng-model value

I am trying to load a dropdown with these values and I got the values correctly from the service but when I assign a value or load a dropdown with a value for ng-model, the dropdown doesn't show the correct option.
I print the "selected" value on the screen and show the correct information, but anyway, the dropdown selection is empty.
What is the suggestion for that?
Thanks in advance
Data: Class type
selected: number
Ex. Object:
works = [
{
$type: class,
Id: 1,
Name: "Test 1",
Description: "Test 1"
},
{
$type: class,
Id: 2,
Name: "test 2",
Description: "Test 2"
}
];
ng-options structure
<button type="button"
class="btn btn-default lg"
ng-model="selected"
name=""
bs-options="item.Id as item.Name for item in vm.works"
bs-select>
I checked also bs-options="item.Name for item in vm.works track by item.Id"

how to make dropdown select a certain value from getting the id from the database?

The code below show a dropdown list from using ng-repeat. When I edit this entity, I have to make them show a certain value selected already than just showing a whole list. I know that I can put selected keyword when I want to select a certain value.
Then what if I used ng-repeat to populate a dropdown? How can I make a dropdown choose a certain value using the id?
<div ng-if="field.name=='ClienteleId'" class="input-group inputFill">
<select ng-disabled="defaultSaveButtons[$index]" class="form-control inputFill2"
ng-model="field.value" ng-options="clientele.id as clientele.name for clientele in dropdown.clienteleOptions | orderBy:'name' track by clientele.id"></select>
/div>
To set the value of a <select> in AngularJS, you need to change the variable that is set as its ng-model to one of the values in your options.
In your case, you have field.value as your ng-model, and your options use the name attribute as their label and the id attribute as their value. To have one of the items in dropdown.clienteleOptions selected by default, you need to set field.value to the id attribute of the corresponding entry in dropdown.clienteleOptions.
Imagine your options and fields look like this:
$scope.clienteleOptions = [
{id: 1, name: 'test 1'},
{id: 2, name: 'test 2'}
];
$scope.fields = [
{name: 'ClienteleId', value: null}
// ...
];
In your controller somewhere, you would have something like this to have test 2 selected:
$scope.fields[0].value = 2;
Or to make it more dynamic:
$scope.fields.forEach(function (field) {
if (field.name === 'ClienteleId') {
field.value = 2;
}
});

Assign option's description to a variable on select

I am using ng-options to populate my select. I assign the value to be the id of the field however I need to assign the description of my option as well.
Not sure how to do that without iterating through my list separately afterwards to find the matching id.
So I am assigning the user's id to the vm.model.user but how can I possibly assign name to a different variable and still make this generic. The key part is this being generic as the description name might vary (sometimes it's description or userDescription etc. and I cannot control that).
$scope.users = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Adam' },
{ id: 3, name: 'Chloe' },
{ id: 4, name: 'Chris' }
];
<select ng-model="vm.model.user" ng-options="user.id as user.name for user in users | orderBy: 'name'"></select>
I have tried doing this on ng-change but it seems it is not aware of user, only ng-options is.
ng-change="vm.model.userDescription = user.name"
One solution would be to set ng-model with the the selected user object, so vm.model.user would have both id and name:
<select ng-model="vm.model.user" ng-options="user as user.name for user in vm.users | orderBy: 'name'"></select>
Check demo: DEMO
I found this interesting answer
https://stackoverflow.com/a/28323031/9559251
Basically we could just do
ng-change="vm.model.userDescription = (users | filter: {id: vm.model.user})[0].name"

AngularJS: refresh ng-options when property source object changes

Full description:
I have list of options with multiple properties (not just key-value pair):
[
{
id: 1,
name: "111",
some: "qqq"
},
{
id: 2,
name: "222",
some: "www"
}
];
And select defined as:
<select name="mySelect" id="mySelect"
ng-options="option.name for option in opts track by option.id"
ng-model="mod1"></select>
Depending on app logic, extra property may change:
{
id: 2,
name: "222",
some: "www1"
}
But actual list in Select doesn't change!
However, if name changes, then entire optionList will be refreshed.
In short you can see demo on JSFiddle OR JSFiddle. I prepared 2 very similar examples:
When button is clicked only extra property updates
When button is clicked - both extra property and key receive new value
Does anybody know solution?
UPDATE
For now I'm solving that issue with update + delay + update solution:
this.click = function(){
$scope.opts = {};
$timeout(function() {
$scope.opts = { /* NEW OBJECT */};
}, 0);
}
OK, so I think I understand what you want, which is to be able to select an option whose nested values may have changed since the list was rendered in the DOM.
Based on that understanding, I believe that the plunker I have created illustrates a solution for you. If you select one of the options, and change the child value in the input field, two-way binding will update the model.
Basically, it is taking the users selection, and on select change, re-assigning the selected object to reference the original option in the options array. This will allow two-way binding to occur. If you view the code, you will see that the input fields are updating the option list itself ($scope.options), where-as the model that is being displayed is $scope.formData.model.
https://plnkr.co/edit/DLhI7t7XBw9EbIezBCjI?p=preview
HTML
<select
name="mySelect"
id="mySelect"
ng-model="formData.model"
ng-change="onChange(formData.model)"
ng-options="option.name for option in options track by option.id"></select>
SELECTED CHILD: {{formData.model.child.name}}
<hr>
<div ng-repeat="option in options">
Child Name for {{ option.name }}: <input ng-model="option.child.name">
</div>
JS
$scope.onChange = function(option) {
angular.forEach($scope.options,function(optionItem){
if (optionItem.id == option.id){
$scope.formData.model = optionItem;
}
})
}
$scope.options = [
{
id: 1,
name: "111",
child: {
id: 11,
name: "111-1"
}
},
{
id: 2,
name: "222",
child: {
id: 22,
name: "222-1"
}
}
];
$scope.formData = {
model: $scope.options[0]
};
Call $apply whenever you want to apply changes made.
$scope.$apply();
This will tell AngularJS to refresh.

Angular select empty value

I have an application for managing user roles. I have a bunch of users in an array $scope.users. The structure of a user object looks like that:
{
name: "Larry",
role: {
id: 2,
name: "Work"
class: "role-work"
},
mustHaveRole: true
}
I also have a bunch of roles in an array $scope.roles. The structure of each role element is the same as each users role field.
I need to create a <select> element, which contains every role in $scope.roles. Each <option> element needs to have the class specified in its class field. The role, which has a matching id with the user's role.id, must be initialy selected. The <select> element must inherit it's class from surrently selected <option> element. For users, who have mustHaveRole set to false, there must also be a empty element which sets it's role field to null.
How can this be achieved? I feel like ng-options is not flexible enough, but when I create all the <option> elements with ng-repeat, Angular sometimes creates an extra empty <option> elementy, which should not be there-
Assign the ng-model with user role id
<div ng-controller="MyCntrl">
<select ng-model="user.role.id" ng-options="v for v in roles">
</select>
</div>
function Ctrl($scope) {
$scope.roles= { "type": "select",
"values": [ "Role 1", "Role 2", "Role 3", "Role 4"]
};
}
Check this plnkr

Resources