使用 ng-repeat 按多列过滤 [英] Filter by multiple columns with ng-repeat

查看:24
本文介绍了使用 ng-repeat 按多列过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在 Angular 中是否有一种简单的方法可以使用 or 逻辑在特定列上使用 ng-repeat 过滤表,而不是 and.现在,我的过滤器正在搜索表中的所有内容(10 多列数据),而实际上只需要过滤 2 列数据(ID 和名称).

I'm wondering if there's an easy way in Angular to filter a table using ng-repeat on specific columns using or logic, rather than and. Right now, my filter is searching everything in the table (10+ columns of data), when it really only needs to filter on 2 columns of data (ID and Name).

我设法在过滤时只查看那两列 (根据文档在过滤器表达式中使用一个对象并查看this SO answer),但它使用了 and 逻辑,这太具体了.我想让它使用 逻辑,但遇到了麻烦.

I've managed to get it down to look only at those 2 columns when filtering (by using an object in the filter expression as per the docs and looking at this SO answer), but it's using and logic, which is too specific. I'd like to get it to use or logic, but am having trouble.

我的 HTML

<input type="text" ng-model="filterText" />
<table>
      <tr ng-repeat="item in data"><td>{{ item.id }}</td><td>{{ item.name }}</td>...</tr>
</table>

我的过滤逻辑:

$filter('filter')(data, {id:$scope.filterText, name:$scope.filterText})

过滤工作,但同样,它采用匹配列的交集而不是联合.谢谢!

The filtering works, but again, it's taking the intersection of the matching columns rather than the union. Thanks!

推荐答案

我想通了 - 我必须编写自己的自定义过滤器.这是我的解决方案:

I figured it out- I had to write my own custom filter. Here is my solution:

var filteredData;

filteredData = $filter('filter')(data, function(data) {
  if ($scope.filter) {
    return data.id.toString().indexOf($scope.filter) > -1 || data.name.toString().indexOf($scope.filter) > -1;
  } else {
    return true;
  }
});

这篇关于使用 ng-repeat 按多列过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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