简体   繁体   English

问题绑定到敲出JS中的可观察数组

[英]problems binding to observable array in knockoutJS

I am currently having problems binding data to an observable array in knockoutJS. 我目前在将数据绑定到敲出JS中的可观察数组时遇到问题。 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/ 小提琴可从http://jsfiddle.net/jwayne2978/k0coh1fz/3/获得。

My HTML looks like the following. 我的HTML如下所示。

<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. 我的JavaScript如下所示。

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. 另外,我在div上针对valuescategories迭代未显示,我不确定为什么。

Any idea on what I am doing wrong? 有什么想法我做错了吗?

your <select> element is missing a closing tag and causing issues further down in the view. 您的<select>元素缺少结束标记,并在视图中进一步导致了问题。

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

updated fiddle: http://jsfiddle.net/ragnarok56/69q8xmrp/ 更新的提琴: http : //jsfiddle.net/ragnarok56/69q8xmrp/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM