简体   繁体   English

淘汰赛计算的写入不触发

[英]Knockout computed write not firing

I have a simple grid, with checkable rows. 我有一个简单的网格,具有可检查的行。

Each row represents basically this object: 每行基本上代表这个对象:

function Person(name, isChecked, isDisabled) {
        this.name = name;
        this.isChecked = ko.observable(isChecked);
        this.isDisabled = ko.observable(isDisabled);
    };

There's also a check all checkbox in the grid header, which should check all the non-disabled rows or un-check them (depending on the state). 网格标题中还有一个全选复选框,该复选框应检查所有未禁用的行或取消选中它们(取决于状态)。 This should also work the other way around, meaning that when I check all the rows within the grid (clickin on each row's checkbox), the header checkbox should be checked. 这也应该以其他方式起作用,这意味着当我检查网格中的所有行时(单击每行的复选框),应选中标题复选框。

The problem is, the computed bound to the check all checkbox fails to execute. 问题是,计算到的“ 检查所有”复选框的绑定无法执行。

See this example: http://jsfiddle.net/eww5dn8q/ . 请参见以下示例: http : //jsfiddle.net/eww5dn8q/

Notice that when you check them all, the alert within the write function is executed. 请注意,当您全部选中它们时,将执行write功能内的警报。 However, when you uncheck, it no longer works. 但是,当您取消选中时,它将不再起作用。

Consider this version where I comment the if statement (lines 30 and 32): http://jsfiddle.net/qws3f7js/ . 考虑这个版本,在其中注释if语句(第30和32行): http : //jsfiddle.net/qws3f7js/

In this version, it seems to run well whether it's check or uncheck, but at the cost of not taking into account the disabled rows. 在此版本中,无论选中还是取消选中,它似乎都可以很好地运行,但是代价是不考虑disabled行。

I'm pretty sure there's a minor thing I'm missing there.. 我很确定那里有一件小事我想念..

Cf. 参看 comment on question: The 'error' was indeed minor. 对问题的评论:“错误”确实很小。 Slightly modifying the computed's read function did the trick (plus it simplifies the if else clause to an if ): 稍微修改计算的read函数就可以了(加上它将if else子句简化为if ):

  read: function () {
      var isAllSelected = true;
      $.each(self.myData(), function(i, person){
          if(!person.isChecked() && !person.isDisabled()){
              isAllSelected = false;
          }
      });
      return isAllSelected;
  },

See fiddle: http://jsfiddle.net/kevinvanlierde/eww5dn8q/1/ 参见小提琴: http : //jsfiddle.net/kevinvanlierde/eww5dn8q/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM