我正在使用此代码来检查我视图上的所有复选框。
var checked = self.includeAllInSoundscript();
var contents = self.filterContents(self.getFilters());
for (var i = 0; i < contents.length; i++) {
   contents[i].includeInSoundscript(checked);
}
return true;
复选框
<input type="checkbox" data-bind="checked: includeInSoundscript" title="sometitle" />
内容是这样的:
(function (ko) {
ContentViewModel = function (data) {
    this.orderId = data.orderId;
    this.contentReferenceId = ko.observable(data.contentReferenceId);
    this.includeInSoundscript = ko.observable();
});
这是过滤方法:
self.getFilters = function() {
  var filterOrders = $.grep(self.orders(), function (order) {
    return (order.usedInfilter());
  });
  var filterLocations = $.grep(self.locations(), function (location)      {
    return (location.usedInfilter());
  });
  return { orders: filterOrders, locations: filterLocations };
};
self.filterContents = function (filter) {
  var filteredArray = self.contents();
  if (filter.orders.length > 0) {
      filteredArray = $.grep(filteredArray, function (content) {
        return $.grep(filter.orders, function (order) {
          return (order.orderId == content.orderId);
        }).length > 0;
      });
  }
  if (filter.locations.length > 0) {
      filteredArray = $.grep(filteredArray, function (content) {
                         return $.grep(filter.locations, function (location) {
                           return $.inArray(location.location, content.orderedFrom().split('/')) != -1;
  }).length > 0;
});
}
 return filteredArray;
};
检查所有复选框很快,但当我取消选中时,最多可能需要 20 秒。奇怪的是,当过滤的结果很小时,它仍然需要更长的时间,即使过滤的结果大约是 40,从总共 1000 个。
复选框在一个表中,使用 data-bind="foreach: contents" 绑定
我现在已经删除了一些“不必要的”可观察对象,对于很可能不会改变的属性,它的行为会稍微好一些,但仍然很慢,尤其是在 Firefox 中。最大的问题是,为什么这种行为只在取消选中复选框时出现,而不是在过滤、排序、检查等时出现。
注意:它只是取消选中复选框,基本上当“选中”为假时,否则它很快。
编辑:我一次只显示 50 个项目,但我正在检查/取消选中所有过滤的项目。这样,我就可以控制发布到服务器的内容。