Search code examples
javascriptjqueryarraysknockout.jsko.observablearray

problems binding to observable array in knockoutJS


I am currently having problems binding data to an observable array in knockoutJS. What I am trying to do is display new values based on the user's selection from a select box.

The fiddle is available at http://jsfiddle.net/jwayne2978/k0coh1fz/3/

My HTML looks like the following.

<select data-bind="options: categories, 
    optionsText: 'name',
    optionsValue: 'id',
    value: selectedCategory,
    optionsCaption: 'Choose...',
    event: { change: categoryChanged }
    ">
<div data-bind="foreach: values">
    <div data-bind="text: name"></div>
</div>
<div data-bind="foreach: categories">
    <div data-bind="text: name"></div>
</div>

My JavaScript looks like the following.

var categories = [
    { "name" : "color", "id": "1" },
    { "name" : "names", "id": "2" }
];
var values0 = [ { "name" : "empty1" }, { "name" : "empty2" } ];
var values1 = [ { "name" : "white" }, { "name" : "black" } ];
var values2 = [ { "name" : "john" }, { "name" : "name" } ];
var Category = function(data) {
    this.name = ko.observable(data.name);
    this.id =  ko.observable(data.id);
};
var Value = function(data) {
    this.name = ko.observable(data.name);
}
var ViewModel = function(categories, values) {
    var self = this;
    self.categories = ko.observableArray(ko.utils.arrayMap(categories, function(category) {
        return new Category(category);
    }));
    self.selectedCategory = ko.observable();
    self.values = ko.observableArray(ko.utils.arrayMap(values, function(value) {
        return new Value(value);
    }));
    self.categoryChanged = function(obj, event) {
        if(self.selectedCategory()) {
            console.log(self.selectedCategory());
            if("1" == self.selectedCategory()) {
                //console.log(values1);
                self.values.push(new Value({"name":"test1"}));
            } else if("2" == self.selectedCategory()) {
                //console.log(values2);
                self.values.push(new Value({"name":"test2"}));
            }
        }
    };
};
var viewModel;
$(document).ready(function() { 
    viewModel = new ViewModel(categories, values0);
    ko.applyBindings(viewModel);
});

When a category is changed, what I really want to do is something like this.

self.values.removeAll();
for(var v in values1) {
 self.values.push(new Value(v));
}

But that doesn't work and so I simply have the line to push a new value into the observable array.

Also, my iterations on the div for the values and categories are not showing and I am unsure why.

Any idea on what I am doing wrong?


Solution

  • your <select> element is missing a closing tag and causing issues further down in the view.

    <select data-bind="options: categories, 
        optionsText: 'name',
        optionsValue: 'id',
        value: selectedCategory,
        optionsCaption: 'Choose...',
        event: { change: categoryChanged }"></select>
    

    updated fiddle: http://jsfiddle.net/ragnarok56/69q8xmrp/