简体   繁体   中英

nested foreach with knockout binding?

My JSON object and observablarray are like :

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"
    }]
}]
}]);

and I want to access to Name and Version like below: But its not working Could you please help me??

<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>

You can't access "OptInfo" this way because of the structure of your JSON. You are passing an array of one object to the observableArray. Here is the structure of this object:

{
"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"
       }
    ]
 }
]
}

So, this object, which becomes the context in your for each loop does not have a "OptInfo" property. The object only contains an array of two objects ("TupleArray").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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