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

查看:858
本文介绍了如何使用$ 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"的字段的属性,因此我无法像下面的代码一样对detail.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天全站免登陆