Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

value binding in KnockoutJs fails for select when having complex types

Tags:

knockout.js

I am trying to use the value binding on the select element, as described in this fiddle: http://jsfiddle.net/MikeEast/nM6dd/2/

However, I cannot seem to be able to set the selected option (value binding).

I know I can use the optionsValue binding, but that makes the value a string instead of an object which is not preferable. If that is the only way to go, how would I do to ensure that the selected option is written back to the view model?

Thanks!

like image 245
Mikael Östberg Avatar asked Dec 22 '11 23:12

Mikael Östberg


People also ask

What is binding in Knockout?

This binding is used to bind the child elements of an object in the specified object's context. This binding can also be nested with other type of bindings such as if and foreach. Syntax with: <binding-object> Parameters. Pass the object which you want to use as context for binding child elements as the parameter.

What is two-way data binding in knockout JS?

KO is able to create a two-way binding if you use value to link a form element to an Observable property, so that the changes between them are exchanged among them. If you refer a simple property on ViewModel, KO will set the form element's initial state to property value.

How do I assign a value to Knockout observable?

To create an observable, assign the ko. observable function to the variable. A default value can be specified in the constructor of the call. Knockout then converts your variable into a function and tracks when the value changes, in order to notify the UI elements associated with the variable.

What is KnockoutJS value binding?

KnockoutJS - Value Binding. This binding is used to link respective DOM element's value into ViewModel property. Mostly, this is used with elements such as input, select, and textarea. This is similar to text binding, the difference being, in value binding data can be changed by the user and the ViewModel will update it automatically.

Can knockout take values that are not corresponding to the selected element?

Technically this is a separate binding, so it has its own documentation. If you want Knockout to allow your model property to take values that have no corresponding entry in your <select> element (and display this by making the <select> element blank), then see documentation for valueAllowUnset.

What is Data-Bind syntax in knockout?

The data-bind syntax. Knockout’s declarative binding system provides a concise and powerful way to link data to the UI. It’s generally easy and obvious to bind to simple data properties or to use a single binding. For more complex bindings, it helps to better understand the behavior and syntax of Knockout’s binding system.

Why do I need to learn knockout’s binding system?

It’s generally easy and obvious to bind to simple data properties or to use a single binding. For more complex bindings, it helps to better understand the behavior and syntax of Knockout’s binding system.


1 Answers

Here is the solution. You have to add the attribute optionsValue: 'id' to the data-bind. You also have to put a function in you viewModel that returns the selected object.

var viewModel = function() {
  this.items = ko.observableArray([
    { id: 1, name: "Apple" }, 
    { id: 2, name: "Orange"},
    { id: 3, name: "Banana"}
  ]);
  this.selectedItemId = ko.observable(3);
  this.selectedItem = function() {
    var self = this;
    return ko.utils.arrayFirst(this.items(), function(item) {
      return self.selectedItemId() == item.id;
    });
  }.bind(this);
};

var vm = new viewModel();
ko.applyBindings(vm);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/1.2.1/knockout-min.js"></script>
<select data-bind="options: items, optionsText: 'name', optionsValue: 'id', value: selectedItemId"></select>
<span data-bind="text: selectedItem().name"></span>

Cheers!

like image 187
Andreas Helgegren Avatar answered Oct 08 '22 06:10

Andreas Helgegren