AngularJS - ng-repeat中的条件过滤器

时间:2014-10-03 15:06:22

标签: angularjs angularjs-ng-repeat angularjs-filter

这是我的代码:

<div ng-repeat="cat in categs | filter:searchText">
      <div><h1 id="{{cat.id}}">{{cat.title}}</h1></div>
       <table>
        <tbody>
            <tr ng-repeat-start="subcat in cat.data | filter:searchText">
                <th><h2 id="{{cat.id}}_{{subcat.id}}">{{subcat.title}}</h2></th>
            </tr>
            <tr ng-repeat-start="subsubcat in subcat.data | filter:searchText">
                <td><h3 id="{{cat.id}}_{{subcat.id}}_{{subsubcat.id}}">{{subsubcat.title}}</h3></td>
            </tr>
            <tr>
                <th>Nombre</th>
                <th>Descripcion</th>
                <th>Enlace</th>   
            </tr>
            <tr ng-repeat-end ng-repeat="item in subsubcat.data | filter:searchText">
                <td>{{item.name}}</td>
                <td>{{item.about}}</td>
                <td><a href="{{item.url}}" target="_blank">{{item.urltext}}</a></td>
            </tr>
            <tr ng-repeat-end>
                <td></td>
            </tr>
        </tbody>
    </table>
  </div>

并且当前的过滤不适用于我想要做的事情,即:

如果h1,h2或h3的标题与searchText匹配,则显示嵌套在里面的所有内容(不要过滤内部的任何内容)。

但是如果h1,h2或h3都没有匹配searchText但是最后一次ng-repeat-start中的内容与某些内容匹配,则只过滤匹配的内容。

为了更好地解释自己,这是一个例子:

  • 车辆
    • 汽车
      • BMW
        • item1:green
        • item2:red
        • item3:black
      • 丰田
    • 自行车

如果searchText是:

  • 车辆:它显示了一切。
  • 汽车:它显示车辆标题和汽车标题(以及所有内容)但不显示自行车(除非文本“汽车”出现在自行车或某些项目中的某些副标题中)
  • 黑色:仅显示车辆,汽车和宝马标题,仅显示第3项。

当前代码的问题在于,如果我从上一次ng-repeat-start中删除过滤器,它会在 cars 示例中执行我想要执行的操作但不适用于黑色示例。

0 个答案:

没有答案
相关问题