I have a multiselect dropdown bound to an array of objects ($scope.selectedProperties):
<select id="propertyDdl" multiple="multiple"
ng-model="selectedProperties"
ng-options="account.property_name group by account.client_name for account in accountInfo">
</select>
(accountInfo is a scope property containing an array of objects):
[{client_name:"Client 1", is_demo:false, property_name:"Prop 1"},
{client_name:"Client 2", is_demo:false, property_name:"Prop 2" },
{client_name:"Client 3", is_demo:false, property_name:"Prop 3" }]
on a certain event, I store the array of selected items:
$scope.updateSessionProperties = function () {
CachingService.cachedSelectedProperties = $scope.selectedProperties;
};
...Then reapply them later:
if(CachingService.cachedSelectedProperties && CachingService.cachedSelectedProperties.length>0){
$scope.selectedProperties = CachingService.cachedSelectedProperties;
}
the array of objects (selectedProperties) it is bound to is structured identical to accountInfo:
[{client_name:"Client 1", is_demo:false, property_name:"Prop 1"},
{client_name:"Client 2", is_demo:false, property_name:"Prop 2" }]
with no luck... the dropdown list displays "none selected" even though the scope property it is bound to is the identical object. I have tried executing $scope.$apply() as well, but the select element will not rebind.
After reading through ng-options, I would suggest the following:
<select
id="propertyDdl"
multiple="multiple"
ng-model="userSelectedProperties"
ng-options="account.property_name group by account.client_name for account in selectedProperties">
</select>
Related
I would like to use uib-popover to create a popup/tooltip for an option item generated by ng-options for a select box. I have tried two approaches but neither one is quite good enough. Ng-options works well because I have to have a default item selected, but I cannot figure out how to plug in uib-popover. If I use ng-repeat I feel like I have more control over how each option is generated, but I can't figure how to select a defualt item for that scenario. Here is the code I am using for the ng-repeat method:
(function() {
"use strict";
var selectionTestViewModel = function() {
var vm = this;
vm.options = [
{ id: 1, val: "Option 1", desc: "Description of Option 1" },
{ id: 2, val: "Option 2", desc: "Description of Option 2" }
];
vm.selectedOption = vm.options[0];
};
angular.module("angularApp").controller("selectionTestController", [selectionTestViewModel]);
}());
<div class="page-content" ng-controller="selectionTestController as st">
<h2>Selection Test</h2>
<div>
<select ng-model="st.selectedOption">
<option ng-repeat="op in st.options track by op.id" value="{{op.id}}" title="{{op.desc}}">{{op.val}}</option>
</select>
<div>
<span>Selected Value: {{st.selectedOption}}</span>
</div>
</div>
</div>
im trying to get my second dropdownlist to work, but cant figure out what is the problem. The first one is showing the data, byt the second is not working.
html:
<select class="form-control"
ng-options="option as option.label for option in myCtrl.options track by option._id"
ng-model="myCtrl.selected"></select>
<select ng-disabled="!myCtrl.selected" class="form-control">
<option ng-repeat="child in myCtrl.options">#{{child.childs}}</option>
</select>
JS:
var vm = this;
vm.options = {};
//get populate data for cascading options
$http.get("data/support.json").success(function(response){
vm.options = response;
vm.selected = vm.options[0];
});
support.json
[{
"_id": "1",
"label": "Title 1",
"childs": [
"Title 1 - sub 1",
"Title 1 - sub 2"
]
},
{
"_id": "2",
"label": "Title 2",
"childs": [
"Title 2 - sub 1",
"Title 2 - sub 2"
]
}]
The second one should be
<option ng-repeat="child in myCtrl.selected.childs">{{child}}</option>
If you are trying to display the children of the selected parent option.
Also, your first select can have its options simplified to
ng-options="option.label for option in myCtrl.options track by option._id"
I am using Javascript with Angular Js, In my controller I have the following Items :
$scope.address = [];
$scope.states = [
{ name:"State 1", cities:[ "City Mexico 1", "City Mexico 2","City Mexico 3"] },
{ name:"State 2", cities:["City 1", "City 2", "City 3"] }
];
In my view I am doing :
<select ng-model="address.state" placeholder="Estado" >
<option ng-repeat="state in states" value="{{state}}"> {{state.name}}</option>
</select>
If I select an option, and then I do console.log(address.state) I get the object as an String instead of as array. How can I get the object as array (json).
The console output look like this :
[state: "{"name":"State 1","cities":["City Mexico 1","City Mexico 2","City Mexico 3"]}"]
I want to get the object as array, because then I do $Scope.address.state.city
Instead of doing an ng-repeat on the <option> tag you can do ng-options in the <select> tag:
<select ng-model="address.state" ng-options="state.name for state in states" placeholder="Estados"></select>
That way the object of state is passed into $scope.address and you can do $scope.address.state.cities
Working example: https://plnkr.co/edit/k2gK5GNEt2xTrMbwih7V?p=preview
Scenario: I make a request to the server for a part. It gives me this back (it's pseudo, but represents what I'm looking at):
{
PartNumber : "XYZ",
Description: "ABCFOO",
ProductClass: "Widget",
FieldList:[
{Name: "PROGRAM TYPE", Value: "Program3"},
{Name: "SHIP", Value: false},
{Name: "NOTES", Value: "SomeValue1"}
],
MoreStuff : [{
...
}]
}
Note the FieldList list of elements, that's the focus here.
The server also gave me a list of certain fields, their types and default values. It looks like this:
[
{FieldName : "PROGRAM TYPE", FieldType: "List", Defaults: [{Name:"Program 1", Value: "Program1"},{Name:"Program 2", Value: "Program2"},{Name:"Program 3", Value: "Program3"}]},
{FieldName : "SHIP", FieldType : "Boolean", Defaults: []},
{FieldName : "NOTES", FieldType: "TextArea", Defaults: []}
]
That comes in a seperate REST call, and the prior to loading my Part. I use it to create part of the HTML page for the Part. You can see they're similarly related to the FieldList section from when I ask for Part.
From that "list of fields" and defaults -- I generate the appropriate HTML elements on the page. If it's a Boolean field type, I create a checkbox - if it's a list, I create a SELECT (with options given in Defaults), TextArea is a text-area, etc. That all works fine. It ends up looking like:
<input data-bind="textInput: PartNumber"/>
<textarea data-bind="textInput: Description"></textarea>
<!-- generating fieldlist - i create a pseudo attr because the field name can have spaces-->
<select field_label="PROGRAM TYPE"> <!-- how the heck do i bind to this??-->
<option value="Program1">Program 1</option>
<option value="Program2">Program 2</option>
<option value="Program3">Program 3</option>
</select >
<input type="checkbox" field_label="SHIP" value="true"/> <!-- or this, how to bind to it?!-->
<!-- end of field list generation -->
Now I take the object (the part I'm given) and put that into my ViewModel - that is all working just swimmingly. I make it easy and just use ko.mapping.fromJS(rest_data); Works just fine.
Data binding is ducky -- for what I am able to bind it to. My issue comes from -- how the heck do I map my FieldList to the HTML I generated for the fields the server gave me?. My data / my viewmodel object has FieldList in it, with a buncha stuff I want to map to that generated stuff. The only real "key" I have is the self-created field_label I have, because the server's FieldName can have spaces.
So I guess what I'm asking is, I have that array of FieldList from my part. I have the whole Part object in my view model and it's all fine. How do I take that FieldList and map it into my self-generated set of fields from the other object (ie., take the FieldList name and tie it to the element with field_label of the same value?)
Spelled out - it'd be like: How to map FieldList with Name of "PROGRAM TYPE" to HTML element having field_label of "PROGRAM TYPE".
I begin to think something like this might be the direction I should be going:
http://jsfiddle.net/MhdZp/128/
but it goes over my head.
One way to approach this:
function Option(definition) {
this.definition = definition;
this.value = ko.observable();
this.templateName = 'input-template-' + definition.FieldType;
}
function ViewModel() {
var self = this;
// from REST call
var fieldDefinition = [{
FieldName: "PROGRAM TYPE",
FieldType: "List",
Defaults: [
{ Name: "Program 1", Value: "Program1" },
{ Name: "Program 2", Value: "Program2" },
{ Name: "Program 3", Value: "Program3" }
]
}];
self.options = ko.observableArray();
// for the sake of the example
self.options.push(new Option(fieldDefinition[0]));
// methods
self.optionByName = function (name) {
return ko.utils.arrayFirst(self.options(), function (option) {
return option.Name = name;
});
};
// poor man's init, imagine 2nd rest call instead
self.optionByName("PROGRAM TYPE").value("Program3");
}
and
<script type="text/html" id="input-template-List">
<label data-bind="text: definition.FieldName"></label>
<select data-bind="
value: value,
options: definition.Defaults,
optionsText: 'Name',
optionsValue: 'Value',
optionsCaption: 'Please select...'
"></select>
</script>
and
<div data-bind="foreach: options">
<div data-bind="template: templateName"></div>
</div>
Add more templates as needed, this should be very easy to extend.
jsFiddle: http://jsfiddle.net/0nxt2zte/
Maybe I am misunderstand how this works. I want to use knockout.js to populate a select element options. I am using the following markup to achieve this:
<select data-bind="options: type_options, optionsText: function(item) {
return item.text;
}, optionsValue: function(item) {
return item.value;
}, optionsCaption:'Select a type...',
value: type">
Here is the relevant model code:
var myModel = {
type: ko.observable(),
type_options: ko.observableArray([
{text: "String 1", value:1},
{text: "String 2", value:2},
{text: "String 3", value: 3},
{text: "String 4", value: 4},
{text: "String 5", value: 5}
]),
}
Now the drop down renders correctly, with all the correct text and values, but when I select the an option from the drop down it doesn't set the value of 'type' correctly.
For instance if I selected the option labeled "String 4", and run the following command in the browser:
myModel.type()
I would expect it to return the value "4". Instead i get the object entire object:
Object
text: "String 4"
value: 4
__proto__: Object
My question is how do i get knockout to set the value of type based on the option's value attribute, instead of the entire object?
Well, you should be able to pass the text for the variable in you options array instead of a function. I don't know if that's what's causing the issue but your markup would look better like
<select data-bind="options: type_options, optionsText: 'text', optionsValue: 'value', optionsCaption:'Select a type...', value: type"></select>
That should get you what you want, see fiddle for full example.