到目前为止,我还没有找到解决这个问题的方法。当主列表中的数量框已更改时,我正在尝试填充汇总表。这是我目前的代码:
<div class="mainItems">
<ul data-bind="foreach: finalListItems">
<li> <input class="qty" data-bind="value: qty, valueUpdate: 'afterkeydown'" size="4"/><label data-bind="text: item" ></label></li>
</ul>
</div>
<div>
<table>
<thead>
<tr>
<th>Item</th>
<th>Quantity</th>
</tr>
</thead>
<tbody data-bind="foreach: finalListItems">
<tr>
<td data-bind="text: item"></td>
<td data-bind="text: qty"></td>
</tr>
</tbody>
</table>
</div>
和视图框:
function sortedListItem(qty, listItemName) {
var self = this;
self.qty = ko.observable(qty);
self.item = listItemName;
}
var trialViewModel = function(){
var self = this;
self.availableListItems = [
{itemName: "Here is Item 1"},
{itemName: "Here is Item 2"},
{itemName: "Here is Item 3"},
{itemName: "Here is Item 4"},
{itemName: "Here is Item 5"}
];
self.finalListItems = ko.observableArray([
new sortedListItem(ko.observable.qty, self.availableListItems[0].itemName),
new sortedListItem(ko.observable.qty, self.availableListItems[1].itemName),
new sortedListItem(ko.observable.qty, self.availableListItems[2].itemName),
new sortedListItem(ko.observable.qty, self.availableListItems[3].itemName),
new sortedListItem(ko.observable.qty, self.availableListItems[4].itemName)
]);
};
ko.applyBindings(trialViewModel());
我还在这里包含了一个 JSFiddle,这样你就可以看到我想要做什么的想法:
问题是如果数量框已更改,我只希望该项目在摘要中可见。我一直无法将可见性绑定绑定到数组中的单个项目,而且我还没有看到任何类似的东西。有什么建议么?