如何使用 $filter('filter') 按对象数组中的特定字段进行过滤 [英] How to filter by a specific field in an object array using $filter('filter')

查看:57
本文介绍了如何使用 $filter('filter') 按对象数组中的特定字段进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 $filter('filter') 过滤员工对象数组,以仅根据可以从下拉列表/选择中选择的员工名字字段过滤结果.请注意,我不想在 ng-repeat 中使用| 过滤器:".

I would like to filter an employee object array using $filter('filter') to filter the results based on an employee's first name field ONLY that can be selected from the drop down/select. Notes that I do NOT want to use the "| filter:" in the ng-repeat".

难点在于name字段是另一个名为'details'的字段的属性,所以我不能像下面的代码那样details.name,因为它会出错.

The difficulty is that the name field is a property of another field called 'details', so I can't details.name like the code below because it will error.

$scope.filteredEmployees = $filter('filter')(employees, {details.name: selectedName }); 

请参阅下面的员工数组对象的结构.

See below for structure of the employee array object.

如何告诉它使用 $filter('filter') 根据 details.name 字段过滤记录?

How can I tell it to filter the records based on details.name field using $filter('filter')?

预先感谢您的帮助!

代码如下:

Var selectedName = "An";
$scope.filteredEmployees = $filter('filter')(employees, {**details.name:** selectedName });
Var employees  = [
      {
        Date: 01/01/2012
        details: {
          name: ‘An’,
      age: 25

        }
      },
      {
        Date: 01/01/2012
        details: {
          name: ‘'Bill',
      age: 20

        }
            }];

    //Here is 
    <select ng-model="selectedName" ng-options="e for e in employees" data-ng-show="employees.length > 0" ng-change="filterEmployees">
        <option value="">All Employees</option>
    </select><br>

    function filterEmployees()  {
        $scope.filteredEmployees = $filter('filter')(employees, "Joe");
    };

推荐答案

表达式可以是 属性到过滤器的映射:其中对象中的每个属性映射到结果集中的相应属性.

The expression can be a map of property-to-filters: where each property in the object maps to a corresponding property within the result set.

$filter('filter')(employees, {name:"Joe"});

现场演示

<小时>

使用自定义函数

如果您的数据更复杂,并且您需要更高级的过滤,那么您可以简单地传入一个自定义谓词函数来评估是否应该过滤某个项目.

Live Demo


Using A Custom Function

If your data is more complex, and you need more advanced filtering, then you can simply pass in a custom predicate function to evaluate whether or not an item should be filtered.

函数的输入将数组的每一项作为参数,如果该项应该从结果集中排除,则预计返回false.

The input of the function takes each item of the array as an argument, and is expected to return false if the item should be excluded from the result set.

var people = [{date:new Date(), details:{
    name: 'Josh',
    age: 32
}}, {date:new Date(), details:{
    name: 'Jonny',
    age: 34
}}, {date:new Date(), details:{
    name: 'Blake',
    age: 28
}}, {date:new Date(), details:{
    name: 'David',
    age: 35
}}];

$scope.filteredPeople = $filter('filter')(people, function(person){
    return /^Jo.*/g.test(person.details.name);
});

这篇关于如何使用 $filter('filter') 按对象数组中的特定字段进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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