根据输入过滤掉Knockout阵列

时间:2014-01-30 22:10:18

标签: javascript html arrays knockout.js ko.observablearray

我有一个用于填充表格的淘汰数组。我也有输入数据绑定到Filtered Array中的变量。 。 。我需要使用这些输入来过滤我的数组,只显示那个数组....我怎么能在我的FilteredAray下面这样做呢?是。

<td><input data-bind="value: First, valueUpdate: 'afterkeydown'" /></td>
<td><input data-bind="value: Second, valueUpdate: 'afterkeydown'"/></td>
<td><input data-bind="value: Third, valueUpdate: 'afterkeydown'" /></td>
<td><input data-bind="value: Fourth, valueUpdate: 'afterkeydown'"/></td>

淘汰视图模型:

   self.First = ko.observable('');
   self.Second = ko.observable('');
   self.Third = ko.observable('');
   self.Fourth = ko.observable('');

   self.FilteredArray = ko.computed(function () {

        var First = self.First();
        var Second = self.Second();
        var Third = self.Third();
        var Fourth = self.Fourth();


        ? Filter self.PeopleArray()


    }, self);

我试图根据输入过滤一个可观察的数组PeopleArray()

1 个答案:

答案 0 :(得分:1)

您正在寻找阵列过滤器。它返回一个新数组,只返回从回调中返回true的结果。

    self.array = ko.obserableArray();
    self.filter1 = ko.observable();
    self.filter2 = ko.observable();
    self.filter3 = ko.observable();
    self.filter4 = ko.observable();
    self.array = ko.observableArray(); 
    self.filteredArray = ko.computed(function () {
         return ko.utils.arrayFilter(self.array(), function (item) {
            //logic for filter1
            //logic for filter2
            //logic for filter3
            //logic for filter4
            //if result matches the filter for return true, if not test next filter
             });
          });
     });