简体   繁体   English

带过滤器作用域对象的AngularJS ng-repeat

[英]Angularjs ng-repeat with filter scope object

I have a requirement to filter rows in ng-repeat based on whats in $scope.filterObject . 我需要根据$scope.filterObject来过滤ng-repeat的行。 I have a data in $scope.customerData that contains all the customer information, but when looping through each customer, I would only like to show results based on whats in $scope.filterObject . 我在$scope.customerData中有一个数据,其中包含所有客户信息,但是在遍历每个客户时,我只想根据$scope.filterObject显示结果。 For example, only show rows of customers that have jobs that are in $scope.filterObject . 例如,仅显示具有$scope.filterObject工作的客户行。

Here is the JSFiddle link . 这是JSFiddle 链接

Here is what I have so far: 这是我到目前为止的内容:

HTML: HTML:

<table ng-app="app" ng-controller="AppsCtrl">
  <thead>
    <tr>
      <th>+</th>
      <th>Name</th>
      <th>Email</th>
      <th>DOB</th>
      <th>Hobby</th>
    </tr>
  </thead>
  <tbody ng-repeat="customer in customerData">
    <tr>
      <td><a href="#" class="main" ng-click="expandTable($event)">+</a></td>
      <td>{{customer.name}}</td>
      <td>{{customer.email}}</td>
      <td>{{customer.DOB}}</td>
      <td>{{customer.Hobby}}</td>
    </tr>
    <tr class="showhide">
      <td> </td>
      <td>Degree</td>
      <td>{{customer.Degree}}</td>
      <td>Job title</td>
      <td>{{customer.Job}}</td>
    </tr>
    <tr class="showhide">
      <td></td>
      <td>Country</td>
      <td>{{customer.Country}}</td>
      <td>City</td>
      <td>{{customer.City}}</td>
    </tr>
  </tbody>
</table>

JS: JS:

// AngularJS toggle table

var app = angular.module('app', []);
app.controller('AppsCtrl', function($scope) {
  $scope.expandTable = function() {
    console.log($(event.target).closest('tr').nextUntil("tr.main"));
    $(event.target).closest('tr').nextUntil("tr.main").slideToggle();
    // console.log($(event.currentTarget).parent().parent());
  }
  var data = [{
    "name": "John",
    "email": "JSmith@yahoo.com",
    "DOB": "01/05/1968",
    "Degree": " BSC",
    "Job": "Engineer",
    "Hobby": "Football",
    "Country": "US",
    "City": "Houston"
  }, {
    "name": "Jessica",
    "email": "Jess@yahoo.com",
    "DOB": "03/05/1976",
    "Degree": " Accounting",
    "Job": "CPA",
    "Hobby": "Video games",
    "Country": "US",
    "City": "Fredericks"
  }, {
    "name": "Andrew",
    "email": "AJoan@yahoo.com",
    "DOB": "06/12/1998",
    "Degree": " PHD",
    "Job": "Professor",
    "Hobby": "Writing",
    "Country": "US",
    "City": "San Diago"
  }, {
    "name": "Rich",
    "email": "Rschol@yahoo.com",
    "DOB": "09/21/1988",
    "Degree": " PHD",
    "Job": "Technician",
    "Hobby": "Writing",
    "Country": "US",
    "City": "San Diago"
  }];
  $scope.customerData = data;
  $scope.filterObject = [{
    "Job": "CPA"
  }, {
    "Job": "Engineer"
  }]
});

\\ Any suggestion and help is really appreciated. \\任何建议和帮助都非常感谢。 :) :)

You can use angular filter function in ng-repeat to compare customerArray with customerObject and then return only element that match customerObject properties. 您可以在ng-repeat中使用角度过滤器功能将customerArray与customerObject比较,然后仅返回与customerObject属性匹配的元素。

//Instead of array    
$scope.filterObject = {
    "Job": "CPA"
  };

HTML : HTML:

  <tr ng-repeat="customer in customerData | filter: filterObject">

Otherwise you can create a custom filter function to compare field or array of fields : 否则,您可以创建一个自定义过滤器功能来比较字段或字段数组:

 app.filter('customerFilter', function() {
  return function( items, filterParams) {
    var filtered = [];
    angular.forEach(items, function(item) {
      if(filterParams.Job.indexOf(item.Job) !== -1) {
        filtered.push(item);
      }
    });
    return filtered;

}; }; }); });

HTML : HTML:

   <tr ng-repeat="customer in customerData | customerFilter: filters">

Customer controller: 客户总监:

$scope.filters = {
  "Job" :["Technician","CPA"]
  }

working example here : https://jsfiddle.net/Nedev_so/wb1hqeca/ 这里的工作示例: https : //jsfiddle.net/Nedev_so/wb1hqeca/

 $scope.customerData = data.filter(function(user){ var found = false; $scope.filterObject.forEach(function(filter){ if(filter.Job === user.Job) found = true; }) return found; }); 

Fiddle: https://jsfiddle.net/nandorpersanyi/yLsxqkx8/ 小提琴: https : //jsfiddle.net/nandorpersanyi/yLsxqkx8/

Regarding Nedev's answer: DOM filters can have an impact on performance, presuming we are handling large datasets (eg $scope.customerData has thousands of customers). 关于Nedev的答案:假定我们正在处理大型数据集(例如$ scope.customerData有数千个客户),则DOM过滤器可能会对性能产生影响。 In this case you are better off filtering within the controller, unless you need user interaction with the filter. 在这种情况下,除非需要用户与过滤器进行交互,否则最好在控制器内进行过滤。 And if your filter object updates dynamically, just $watch it for changes, then rerender customerData when changed 而且,如果您的过滤器对象动态更新,只需$ watch监视其更改,然后在更改后重新呈现customerData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM