我在淘汰表中有一个数量选择选项,但我想根据可用库存对其设置最大限制。
假设我有以下条目的值
ID = 1
stock = 8
我想用数字 1-stock 填充数量字段,在本例中为 1-8。数量将始终小于或等于库存。
function ItemEntry(ID, stock, quantity) {
var self = this;
self.ID= ID;
self.stock= stock;
self.quantity = quantity;
}
// viewmodel
function EntryViewModel() {
var self = this;
self.itemNumbers = ko.observableArray([
new ItemEntry("1", 8, QUANTITY HERE!) //initial starting values
]);
self.removeItem = function(item) { self.itemNumbers.remove(item) }
}
//custom binding for dropdown outside of models
ko.bindingHandlers.quantityDropdown = {
update: function(quantityDropdown, stock, EntryViewModel) {
var quantity = ko.utils.unwrapObservable(stock());
for(var i = 0; i < stock(); i++)
{
$(quantityDropdown).append('<option value="' + i + '">' + i + '</option>');
}
}
};
ko.applyBindings(new EntryViewModel());
这是HTML
<table border="1">
<thead><tr>
<th>ID</th><th>Stock</th><th>Quantity</th><th></th>
</tr></thead>
<tbody data-bind="foreach: itemNumbers">
<tr>
<td data-bind="text: ID"></td>
<td data-bind="text: stock"></td>
<td><select data-bind="quantityDropdown: quantity"></select></td>
<td><a href="#" data-bind="click: $root.removeItem">Remove</a></td>
</tr>
</tbody>
</table>