gpt4 book ai didi

html - knockoutjs 获取满足条件的项目数

转载 作者:搜寻专家 更新时间:2023-10-31 23:03:59 24 4
gpt4 key购买 nike

我有一个 View 模型,其中有一个可观察数组,里面有一个可观察数组。我想跟踪用户何时从他们的列表中勾选项目。我有以下 View 模型和 html:

var mydata = {
Incomplete: ko.computed(function () {
// return the count of items that are not IsActive
}),
Categories: ko.observable([
{
Description: "Dairy", ListItems: ko.observableArray([
{ Description: "Eggs", Quantity: "1 Dz.", IsActive: ko.observable(false) },
{ Description: "Milk", Quantity: "1 Gallon", IsActive: ko.observable(false) }
])
},
{
Description: "Produce", ListItems: ko.observableArray([
{ Description: "Lettuce", Quantity: "1 Head", IsActive: ko.observable(false) },
{ Description: "Oranges", Quantity: "5 ea.", IsActive: ko.observable(false) },
{ Description: "Greenbeans", Quantity: "1 Thingy", IsActive: ko.observable(false) },
])
},
])
};

<div data-bind="foreach: Categories" id="list-items">
<h3 data-bind="text: Description" id="list-heading"></h3>
<ul data-bind="foreach: ListItems">
<li>
<div class="title">
<input type="checkbox" data-bind="checked: IsActive"/>
<input type="text" data-bind="value: Quantity, disable: IsActive" class="input-readonly" readonly />
<input type="text" data-bind="value: Description, disable: IsActive" class="input-readonly" readonly />
</div>
</li>
</ul>
</div>
<div >
<span data-bind="text: Incomplete"></span>
<span >Items left in your list</span>
</div>

最佳答案

使用您当前的代码,您可能希望循环遍历类别,然后为每个类别循环遍历各个项目。这是一种可能的实现方式:

Incomplete: ko.computed(function () {
var count = 0;
ko.utils.arrayForEach(mydata.Categories(), function(category) {
ko.utils.arrayForEach(category.ListItems(), function(item) {
if (!item.IsActive()) {
count++;
}
});
});

return count;
}),

关于html - knockoutjs 获取满足条件的项目数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23068901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com