4

我想使用 Knockout JSON Mapping 和 Twitter bootstrap 创建一个动态 Accordion 控件。

var ProductViewmodel;

function bindProductModel(Products) {
    var self = this;
    self.items = ko.mapping.fromJS([]);
    ProductViewmodel = ko.mapping.fromJS(Products.d, self.items);
    ko.applyBindings(ProductViewmodel);
}

var Data = {
    "d": [
        {
        "__type": "Warehouse.Tracntrace.Members_Only.DLL.StockMovement.AvailibleStock",
        "WarehouseID": 1,
        "ProductSKUID": 2,
        "ProductSKUName": "Decoder 1131",
        "WarehouseName": "SoftwareDevelopmentTest",
        "Status": "Staging",
        "QtyUnassigned": 5},
    {
        "__type": "Warehouse.Tracntrace.Members_Only.DLL.StockMovement.AvailibleStock",
        "WarehouseID": 1,
        "ProductSKUID": 3,
        "ProductSKUName": "Decoder 1132",
        "WarehouseName": "SoftwareDevelopmentTest",
        "Status": "Staging",
        "QtyUnassigned": 15}
    ]
};

    function BuildLinkFromJSON() {
        bindProductModel(Data);
        var link;
        link = '<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" data-bind="text: d.ProductSKUName" href="#collapse"></a>';
        return link;
    }


function BuildAccordian(Pane) {
        var link = BuildLinkFromJSON();
        var eventsContainer = document.getElementById('accordion');
        var events = eventsContainer.innerHTML;
        events = events + '<div class="accordion-group"><div class="accordion-heading">'+link+'</div><div id="collapseOne" class="accordion-body collapse in"><div class="accordion-inner">My Content Here</div></div>';
        eventsContainer.innerHTML = events;
    }

    $(document).ready(function () {
        BuildAccordian();
    });

HTML:

<div class="accordion" id="accordion">
</div>

我基本上需要我的 JSON 数据中的每个产品,如果可能的话,标题应该在标题中显示 ProductSKUName 和 QtyUnnasigned。我用发布的 JSON 数据和名为 Build Accordion 的手风琴 HTML 函数创建了这个Fiddle 。

4

1 回答 1

4

这个怎么样?http://jsfiddle.net/billaraw/xZHbh/4/

我基本上添加了循环,因此它可以获取每个项目:

<div class="accordion" id="accordion2">

<!-- ko foreach: $data -->
<h3>
    <span data-bind="text: ProductSKUName"></span> :
    <span data-bind="text: QtyUnassigned"></span>                
</h3 >
<div>
    MyContent
</div>
<!-- /ko -->

</div >​
于 2012-12-19T00:23:08.247 回答