我在以下方面遇到了一些麻烦。我正在使用淘汰赛来编辑包含嵌套列表的列表,有点像淘汰赛网站上的联系人编辑器:
http://knockoutjs.com/examples/contactsEditor.html
在我的示例中,“设计”与上述示例中的“联系人”相关,“设计交付”与“电话号码”相关。
所以我在每个“设计”中都有一个名为 MaxQuantity 的值,在每个“designDelivery”中都有一个名为 Quantity 的值。实际上,我想要实现的是在每个名为“Remaining”的“设计”中都有另一个值,它基本上是“MaxQuantity”减去与特定“设计”相关的所有“designDeliveries”中“Quantity”的总和”。当我添加新的“designDeliveries”并填充“Quantity”字段时,这当然应该从该特定设计的新“Remaining”值中扣除
到目前为止,我有以下代码:
var DesignsModel = function (designs) {
var self = this;
self.designs = ko.observableArray(ko.utils.arrayMap(designs, function (design) {
return { BookingDesignId: design.BookingDesignId,
DesignName: design.DesignName,
MaxQuantity: design.Quantity,
DesignDeliveries: ko.observableArray(design.DesignDeliveries) };
}));
self.addDesignDelivery = function (design) {
design.DesignDeliveries.push({
Quantity: "",
DepotId: ""
});
};
self.removeDesignDelivery = function (designDelivery) {
$.each(self.designs(), function () { this.DesignDeliveries.remove(designDelivery) })
};
self.save = function () {
self.lastSavedJson(JSON.stringify(ko.toJS(self.designs), null, 2));
};
self.lastSavedJson = ko.observable("");
};
ko.applyBindings(new DesignsModel(initialData));
初始数据看起来像这样:
[{"DesignId":"1","DesignName":"Design1","Quantity":50,"DesignDeliveries":[]},{"DesignId":"2","DesignName":"Design2","Quantity":50,"DesignDeliveries":[]},{"DesignId":"3","DesignName":"Design3","Quantity":500,"DesignDeliveries":[]}]
和这样的html:
<div data-bind="foreach: designs">
<div>
<span data-bind="text: DesignName"></span>
<a href="#" data-bind="click: $root.addDesignDelivery">Add</a>
</div>
<div>
<span data-bind="text: MaxQuantity"></span>
</div>
<div>
<span data-bind="text: Remaining"></span>
</div>
<div data-bind="foreach: DesignDeliveries">
<div>
<input data-bind="value: Quantity" />
</div>
<div>
<a href="#" data-bind="click: $root.removeDesignDelivery">
Delete
</a>
</div>
</div>
</div>
我希望这足够清楚。有人知道我将如何实现这一目标吗?
提前致谢