内部过滤器在淘汰赛JS中无法正常工作 [英] Inner Filter Not working Properly in Knockout JS

查看:61
本文介绍了内部过滤器在淘汰赛JS中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个静态数据数组.

I have array of static data sized 3.

我在右侧显示数组.

上方有一个用于过滤的文本框. 我为此选择了2个模型.

There's one textbox above it for filter. I have taken 2 models for it.

一个是field,第二个是subfield.

一个字段可以有多个sub fields. 我在fieldsubfields上进行过滤. 它适用于田地,但显示了subfields上的一些堰输出. 唯一的条件是,如果将field's filtered data添加到数组中,则不应将其用于subfield,如果在字段中未找到匹配项,则可以进入subfields,然后将数据添加到已过滤的数组中.

one field can have multiple sub fields. I am filtering on field as well as on subfields. It works on fields but shows me some weir output on subfields. there's only one condition that if field's filtered data is added in array then it should not go for subfield, if no match found in field then it can go in subfields and then add data to filtered array.

我的小提琴

推荐答案

问题在于您正在使用过滤数据的条件.随着您拥有两个以上子字段,因此对于每个子字段匹配项,相同的数据都被推送到arr中产生意外结果的主要原因.

Problem is in condition which you are using for filtering data.As you have more then two subfield so for every match of subfield same data pushed into arr that is the main reason of getting unexpected result.

 self.filteredList = ko.computed(function() {
  var filter = self.filter(),
  arr = [];
if (filter) {
  ko.utils.arrayForEach(self.controlFields(), function(item) {
    if (item.code().match(filter) || item.title().toLowerCase().match(filter.toLowerCase())) {
      arr.push(item);
    }
      ko.utils.arrayForEach(item.subFields(), function(sf) {
        if (sf.title().toLowerCase().match(filter.toLowerCase())) {
          var found = ko.utils.arrayFirst(arr, function(k) {
            return item.title() === k.title() && item.code()===k.code();
          });
          if (!found) {
            arr.push(item);
          }
        }
      });

   });
  } else {
   arr = self.controlFields();
  }
 return arr;
});

演示

这篇关于内部过滤器在淘汰赛JS中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆