使用Knockout js获取重复值

时间:2013-01-08 16:54:48

标签: javascript model-view-controller knockout.js knockout-mapping-plugin

我有一个下拉框,我用来通过淘汰赛来填充表格。初始选择工作正常。为了描述发生的事情,第一组有大约5个元素,所选择的第二组有1个元素。但是当选择第二组时,它会填充5个空格。如果我切换回第一组,则它具有相同的五行五次,总共25行。

以下是我的视图模型

var company = company  || {};
company.thing = company.thing || {};

company.thing.app = (function ($) {
    "use strict";

    var my = {};

    my.init = function()
    {

    }

    my.bindBatches = function (id) {

        var url = "/App/Home/SortedBatches";

        var batchesModel = {
            batches: [{
                "NameOne": null,
                "NameTwo": null
            }]
        },
        batchesViewModel = null;

        batchesViewModel = ko.mapping.fromJS(batchesModel);

        $.getJSON(url, { press: id }, function (batchData) {

            var sortedData= {
                batches:batchData
            };

            ko.mapping.fromJS(sortedData, batchesViewModel);
        });

        batchesViewModel.templateName = function (batch) {
            return "batch-list";
        };

        ko.applyBindings(batchesViewModel);

    };

    return my;
}(jQuery));

和我的Html

         <table id="batch-list3" class="table table-striped" >
            <tbody data-bind="foreach: batches">
                <tr>
                    <td data-bind="text: NameOne"></td>
                    <td data-bind="text: NameTwo"></td>
                </tr>
            </tbody>
         </table>

请某人请你好好指出我做错了什么?

0 个答案:

没有答案