嵌套的foreach与敲除绑定?

时间:2013-08-02 14:34:00

标签: javascript data-binding knockout.js foreach nested

我的JSON对象和observablarray类似于:

self.myComplexsObject= ko.observableArray([{  
    "TupleArray": [{
    "OptInfo": {
        "Version": "B",
        "Name": "csk_profile"
    },
        "Parameter": [{
        "Value": "1",
        "Name":"min SampleCopunt"
    }]
},
             {
    "OptInfo": {
        "Version": "A",
        "Name": "Dml_profile"
    },
        "Parameter": [{
        "Value": "2",
        "Name":"min SampleCopunt"
    }]
}]
}]);

我希望访问名称和版本,如下所示:但它不起作用你能帮帮我吗?

<div data-bind='template: { foreach: myComplexsObject,
                            beforeRemove: hideElement,
                            afterAdd: showElement }'>
   <div data-bind="foreach: OptInfo">
      <div data-bind='attr: { "class": "complexObject" + Name,"title":Name}, 
                      text: Version'></div>
   </div>
</div>

1 个答案:

答案 0 :(得分:0)

由于JSON的结构,您无法以这种方式访问​​“OptInfo”。您正在将一个对象的数组传递给observableArray。以下是此对象的结构:

{
"TupleArray":[
 {
    "OptInfo":{
       "Version":"B",
       "Name":"csk_profile"
    },
    "Parameter":[
       {
          "Value":"1",
          "Name":"min SampleCopunt"
       }
    ]
 },
 {
    "OptInfo":{
       "Version":"A",
       "Name":"Dml_profile"
    },
    "Parameter":[
       {
          "Value":"2",
          "Name":"min SampleCopunt"
       }
    ]
 }
]
}

因此,此对象成为每个循环的上下文,没有“OptInfo”属性。该对象仅包含两个对象的数组(“TupleArray”)。