2

我使用 ng-repeat 制作了一个基于数组的重复行的简单表。我添加了一个与此示例类似的过滤器。

当我尝试过滤嵌套对象时,我的问题就出现了。它始终是单个对象,基本上它是我当前对象的父对象,但它是完整对象,而不仅仅是 FK 值。

如果我使用 filterText.educationCenter.name 它会过滤表中的所有行并且即使我清除过滤器也不会返回它们。我必须完全重新加载。

如果我只过滤 filterText.educationCenter 它将起作用,但它会搜索整个对象的所有字段。

我想保持 JSON 原样,并根据educationCenter 对象的名称过滤表结果。

我的 HTML

Search: <input ng-model="filterText.$"/>
<input ng-model="filterText.city"/>
<input ng-model="filterText.educationCenter.name"/>

<tr ng-repeat="course in courses | filter:filterText | startFrom:currentPage*pageSize | limitTo:pageSize">
                    <td width="145">{{course.city}}</td>
                    <td width="145">{{course.educationCenter.name}}</td>
...

我的 JSON 用于数组中的单个对象

{"id":"108"
,"city":"My City"
,"educationCenter":{"id":"3"
                   ,"description":"This is the ed center description"
                   ,"name":"test ed center 1"}
,"noOfDays":"1"}
4

1 回答 1

0

正如@fastreload 所建议的那样,我认为您必须制作自己的过滤器。

这是递归过滤器的示例。(虽然它不支持$符号,我确信它缺少一些边缘情况支持。)

app.filter('recursiveFilter', function() {
  var compare = function(input_, cond_) {
    if (!cond_) return true;
    if (typeof input_ == "string" && typeof cond_ == "string") {
      return (new RegExp(cond_)).test(input_);
    } else if (typeof input_ == "object" && typeof cond_ == "object") {
      for (var s in cond_) {
        if (!compare(input_[s], cond_[s])) return false;
      }
      return true;
    }
    return false;
  };

  return function(inputs, cond) {
    return $.grep(inputs, function(input) {
      return compare(input, cond);
    });
  };
});
于 2012-11-28T07:40:13.730 回答