嵌套ForEach Knockout 2.2.1没有显示到第4级

时间:2017-08-10 12:30:58

标签: knockout.js

我正在试图找出一个MVC / Durandal / Knockout项目,为什么我无法绑定一个 ContractsList() = ko.observableArray();进入第4级的foreach。 它工作正常,直到第3级。

这是我试图绑定的Json。

    {"Contracts":
        {
            "Contract":
                    [  
                        {
                            "NrContract":null,
                            "IdStore":"5",
                            "FlagStatus":"P",
                            "Date":"30/11/2017",                        
                            "Sale":"3168.23",                       
                            "DsName":"TEST1",                       
                            "Items":
                                { 
                                    "Item":
                                    [
                                        {"NrItems":"1","VlTotalItems":"1112.23","DsItems":"3123123"},
                                        {"NrItems":"1","VlTotalItems":"2056","DsItems":"bbb"}
                                    ]
                                }
                        },
                        {   
                            "NrContract":null,
                            "IdStore":"5",
                            "FlagStatus":"P",
                            "Date":"30/11/2017",                        
                            "DsName":"TEST2",                       
                            "Items":
                                {   
                                    "Item":[
                                                {"NrItems":"7","VlTotalItems":"7785,61","DsItems":"3123123"},
                                                {"NrItems":"1","VlTotalItems":"3","DsItems":"oi"}
                                            ]
                                }
                        }
                    ]
        }
    }

foreach简化:

<div data-bind='foreach: ContractsList'>
    <div data-bind='foreach: Contract'>
        <div data-bind='foreach: Items'>    
            <div data-bind='foreach: Item'>
                <span data-bind='text: DsItems'></span>
            </div>
        </div>
   </div>

1 个答案:

答案 0 :(得分:2)

这是因为items是一个对象而不是一个数组:

"Items":
    {   
        "Item":[
                    {"NrItems":"7","VlTotalItems":"7785,61","DsItems":"3123123"},
                    {"NrItems":"1","VlTotalItems":"3","DsItems":"oi"}
                ]
    }

更新到数组,如果出现错误或

,这将有效
<div data-bind='foreach: Items.Item'>
    <span data-bind='text: DsItems'></span>
</div>