使用Knockout JS管理数组中的数组

时间:2013-12-29 00:13:26

标签: javascript arrays knockout.js

我在数组中有一个数组,例如我有以下对象:

{ruleGroups: [{ 
  rules: [{ 
       dataField1:ko.observable()
      ,operator:ko.observable()
      ,dataField2:ko.observable()
      ,boolean:ko.observable()
      ,duration:ko.observable()
     }] 
   }] 
};

如何在数组中编辑数组?

我能够改善这个问题但是在添加组时仍然存在添加行的问题,新组工作但旧组运行死了: 这里有一个工作示例(http://jsfiddle.net/abarbaneld/UaKQn/41/

使用Javascript:

var dataFields = function() {
    var fields = [];
    fields.push("datafield1");
    fields.push("datafield2");
    return fields;
 };

var operators = function() {
    var operator = [];
     operator.push("Plus");
     operator.push("Minus");
     operator.push("Times");
     operator.push("Divided By");
     return operator;
};

var booleanOperators = function() {
    var operator = [];
    operator.push("Equal");
    operator.push("Not Equal");
    operator.push("Greater Than");
    operator.push("Less Than");
    operator.push("Contains");
    operator.push("Etc...");
    return operator;
};

var ruleObj = function () {
    return {
        dataField1:ko.observable()
        ,operator:ko.observable()
        ,dataField2:ko.observable()
        ,boolean:ko.observable()
        ,duration:ko.observable()
    }
};

var ruleGroup = function() {
    return rg = {
        rules:  ko.observableArray([new ruleObj()]),
        addRow: function() {               
              rg.rules.push(new ruleObj());
              console.log('Click Add Row', rg.rules);
        },
        removeRow : function() {
              if(rg.rules().length > 1){
                  rg.rules.remove(this);
              }
        }
    }
};

var ViewModel = function() {
    var self = this;
    self.datafields = ko.observableArray(dataFields());
    self.operators = ko.observableArray(operators());
    self.booleanOperators = ko.observableArray(booleanOperators());

    self.groupRules = ko.observableArray([new ruleGroup()]);

    self.addGroup = function() {
         self.groupRules.push(new ruleGroup());
    };

    self.removeGroup = function() {
        if(self.groupRules().length > 1){
            self.groupRules.remove(this);
        }
    };

    self.save = function() {
        console.log('Saving Object', ko.toJS(self.groupRules));
    };

};
ko.applyBindings(new ViewModel()); 

HTML

<div data-bind="foreach: { data: groupRules, as: 'groupRule' }" style="padding:10px;">
    <div>
        <div data-bind="foreach: { data: rules, as: 'rule' }" style="padding:10px;">
            <div>
                <select data-bind="options: $root.datafields(), value: rule.dataField1, optionsCaption: 'Choose...'"></select>
                <select data-bind="options: $root.operators(), value: rule.operator, optionsCaption: 'Choose...'"></select>
                <select data-bind="options: $root.datafields(), value: rule.dataField2, optionsCaption: 'Choose...',visible: operator"></select>
                <select data-bind="options: $root.booleanOperators(), value: rule.boolean, optionsCaption: 'Choose...'"></select>
                <input data-bind="value: rule.duration" />
                <span data-bind="click: groupRule.addRow">Add</span>
                <span data-bind="click: groupRule.removeRow">Remove</span> 
            </div>
        </div>
        <span data-bind="click: $parent.addGroup">[Add Group] </span>
        <span data-bind="click: $parent.removeGroup">[Remove Group]</span>
    </div>
</div>
<div>
    <span data-bind="click:save">[Save]</span>
</div>

1 个答案:

答案 0 :(得分:0)

我能够通过将ruleGroup的功能重新排列为:

来解决问题
var ruleGroup = function() {
    var rg = {
        rules:  ko.observableArray([new ruleObj()]),
        addRow: function() {               
                  rg.rules.push(new ruleObj());
                  console.log('Click Add Row', rg);
        },
        removeRow : function() {
            if(rg.rules().length > 1){
                rg.rules.remove(this);
            }
        }
    }

    return rg;
};

不完全确定为什么这会产生影响,但我认为由于现在正在创建和引用新的var。 在这里找到工作JSFiddle http://jsfiddle.net/abarbaneld/UaKQn/