如何在AngularJs中使用ng-repeat过滤(key, value) ?

时间:2021-05-13 07:41:53

I am trying to do something like :

我想做的是:

<div ng-controller="TestCtrl">
    <div ng-repeat="(k,v) in items | filter:hasSecurityId">
        {{k}} {{v.pos}}
    </div>
</div>

AngularJs Part:

AngularJs部分:

function TestCtrl($scope) 
{
    $scope.items = {
                     'A2F0C7':{'secId':'12345', 'pos':'a20'},
                     'C8B3D1':{'pos':'b10'}
                   };

    $scope.hasSecurityId = function(k,v)
    {
       return v.hasOwnProperty('secId');
    }
}

But somehow, it is showing me all items. How can I filter on (key,value) ?

但不知何故,它向我展示了所有的东西。我如何过滤(key,value) ?

8 个解决方案

#1


124  

Angular filters can only be applied to arrays and not objects, from angular's API -

角滤波器只能应用于阵列而不是物体,从角的API。

"Selects a subset of items from array and returns it as a new array."

“从数组中选择项的子集,并将其作为一个新数组返回。”

You have two options here:
1) move $scope.items to an array or -
2) pre-filter the ng-repeat items, like this:

这里有两个选项:1)移动$scope。对一个数组或- 2)预先过滤ng重复项,像这样:

<div ng-repeat="(k,v) in filterSecId(items)">
    {{k}} {{v.pos}}
</div>

And on the Controller:

和控制器:

$scope.filterSecId = function(items) {
    var result = {};
    angular.forEach(items, function(value, key) {
        if (!value.hasOwnProperty('secId')) {
            result[key] = value;
        }
    });
    return result;
}

jsfiddle: http://jsfiddle.net/bmleite/WA2BE/

jsfiddle:http://jsfiddle.net/bmleite/WA2BE/

#2


45  

My solution would be create custom filter and use it:

我的解决方案是创建自定义过滤器并使用它:

app.filter('with', function() {
  return function(items, field) {
        var result = {};
        angular.forEach(items, function(value, key) {
            if (!value.hasOwnProperty(field)) {
                result[key] = value;
            }
        });
        return result;
    };
});

And in html:

在html中:

 <div ng-repeat="(k,v) in items | with:'secId'">
        {{k}} {{v.pos}}
 </div>

#3


20  

Or simply use

或简单地使用

ng-show="v.hasOwnProperty('secId')"

See updated solution here:

看到更新的解决方案:

http://jsfiddle.net/RFontana/WA2BE/93/

http://jsfiddle.net/RFontana/WA2BE/93/

#4


19  

Also you can use ng-repeat with ng-if:

你也可以使用ng-if:

<div ng-repeat="(key, value) in order" ng-if="value > 0">

#5


11  

You can simply use angular.filter module, and then you can filter even by nested properties.
see: jsbin
2 Examples:

你可以简单地用角。过滤模块,然后你可以通过嵌套的属性来过滤。看到:jsbin两个例子:

JS:

JS:

angular.module('app', ['angular.filter'])
  .controller('MainCtrl', function($scope) {
  //your example data
  $scope.items = { 
    'A2F0C7':{ secId:'12345', pos:'a20' },
    'C8B3D1':{ pos:'b10' }
  };
  //more advantage example
  $scope.nestedItems = { 
    'A2F0C7':{
      details: { secId:'12345', pos:'a20' }
    },
    'C8B3D1':{
      details: { pos:'a20' }
    },
    'F5B3R1': { secId:'12345', pos:'a20' }
  };
});

HTML:

HTML:

  <b>Example1:</b>
  <p ng-repeat="item in items | toArray: true | pick: 'secId'">
    {{ item.$key }}, {{ item }}
  </p>

  <b>Example2:</b>
  <p ng-repeat="item in nestedItems | toArray: true | pick: 'secId || details.secId' ">
    {{ item.$key }}, {{ item }}
  </p> 

#6


6  

It is kind of late, but I looked for e similar filter and ended using something like this:

有点晚了,但是我找了e类似的过滤器,最后用了这样的东西:

<div ng-controller="TestCtrl">
 <div ng-repeat="(k,v) in items | filter:{secId: '!!'}">
   {{k}} {{v.pos}}
 </div>
</div>

#7


0  

I made a bit more of a generic filter that I've used in multiple projects already:

我在多个项目中已经使用了一个通用的过滤器:

  • object = the object that needs to be filtered
  • 对象=需要过滤的对象。
  • field = the field within that object that we'll filter on
  • 字段=我们要筛选的对象中的字段。
  • filter = the value of the filter that needs to match the field
  • 筛选器=需要匹配该字段的筛选器的值。

HTML:

HTML:

<input ng-model="customerNameFilter" />
<div ng-repeat="(key, value) in filter(customers, 'customerName', customerNameFilter" >
   <p>Number: {{value.customerNo}}</p>
   <p>Name: {{value.customerName}}</p>
</div>

JS:

JS:

  $scope.filter = function(object, field, filter) {
    if (!object) return {};
    if (!filter) return object;

    var filteredObject = {};
    Object.keys(object).forEach(function(key) {
      if (object[key][field] === filter) {
        filteredObject[key] = object[key];
      }
    });

    return filteredObject;
  };

#8


0  

Although this question is rather old, I'd like to share my solution for angular 1 developers. The point is to just reuse the original angular filter, but transparently passing any objects as an array.

虽然这个问题相当老,但我还是想分享一下我的解决方案。关键是重用原始的角度过滤器,但是透明地将任何对象作为数组传递。

app.filter('objectFilter', function ($filter) {
    return function (items, searchToken) {
        // use the original input
        var subject = items;

        if (typeof(items) == 'object' && !Array.isArray(items)) {
            // or use a wrapper array, if we have an object
            subject = [];

            for (var i in items) {
                subject.push(items[i]);
            }
        }

        // finally, apply the original angular filter
        return $filter('filter')(subject, searchToken);
    }
});

use it like this:

使用它是这样的:

<div>
    <input ng-model="search" />
</div>
<div ng-repeat="item in test | objectFilter : search">
    {{item | json}}
</div>

here is a plunker

这是一个一美元

#1


124  

Angular filters can only be applied to arrays and not objects, from angular's API -

角滤波器只能应用于阵列而不是物体,从角的API。

"Selects a subset of items from array and returns it as a new array."

“从数组中选择项的子集,并将其作为一个新数组返回。”

You have two options here:
1) move $scope.items to an array or -
2) pre-filter the ng-repeat items, like this:

这里有两个选项:1)移动$scope。对一个数组或- 2)预先过滤ng重复项,像这样:

<div ng-repeat="(k,v) in filterSecId(items)">
    {{k}} {{v.pos}}
</div>

And on the Controller:

和控制器:

$scope.filterSecId = function(items) {
    var result = {};
    angular.forEach(items, function(value, key) {
        if (!value.hasOwnProperty('secId')) {
            result[key] = value;
        }
    });
    return result;
}

jsfiddle: http://jsfiddle.net/bmleite/WA2BE/

jsfiddle:http://jsfiddle.net/bmleite/WA2BE/

#2


45  

My solution would be create custom filter and use it:

我的解决方案是创建自定义过滤器并使用它:

app.filter('with', function() {
  return function(items, field) {
        var result = {};
        angular.forEach(items, function(value, key) {
            if (!value.hasOwnProperty(field)) {
                result[key] = value;
            }
        });
        return result;
    };
});

And in html:

在html中:

 <div ng-repeat="(k,v) in items | with:'secId'">
        {{k}} {{v.pos}}
 </div>

#3


20  

Or simply use

或简单地使用

ng-show="v.hasOwnProperty('secId')"

See updated solution here:

看到更新的解决方案:

http://jsfiddle.net/RFontana/WA2BE/93/

http://jsfiddle.net/RFontana/WA2BE/93/

#4


19  

Also you can use ng-repeat with ng-if:

你也可以使用ng-if:

<div ng-repeat="(key, value) in order" ng-if="value > 0">

#5


11  

You can simply use angular.filter module, and then you can filter even by nested properties.
see: jsbin
2 Examples:

你可以简单地用角。过滤模块,然后你可以通过嵌套的属性来过滤。看到:jsbin两个例子:

JS:

JS:

angular.module('app', ['angular.filter'])
  .controller('MainCtrl', function($scope) {
  //your example data
  $scope.items = { 
    'A2F0C7':{ secId:'12345', pos:'a20' },
    'C8B3D1':{ pos:'b10' }
  };
  //more advantage example
  $scope.nestedItems = { 
    'A2F0C7':{
      details: { secId:'12345', pos:'a20' }
    },
    'C8B3D1':{
      details: { pos:'a20' }
    },
    'F5B3R1': { secId:'12345', pos:'a20' }
  };
});

HTML:

HTML:

  <b>Example1:</b>
  <p ng-repeat="item in items | toArray: true | pick: 'secId'">
    {{ item.$key }}, {{ item }}
  </p>

  <b>Example2:</b>
  <p ng-repeat="item in nestedItems | toArray: true | pick: 'secId || details.secId' ">
    {{ item.$key }}, {{ item }}
  </p> 

#6


6  

It is kind of late, but I looked for e similar filter and ended using something like this:

有点晚了,但是我找了e类似的过滤器,最后用了这样的东西:

<div ng-controller="TestCtrl">
 <div ng-repeat="(k,v) in items | filter:{secId: '!!'}">
   {{k}} {{v.pos}}
 </div>
</div>

#7


0  

I made a bit more of a generic filter that I've used in multiple projects already:

我在多个项目中已经使用了一个通用的过滤器:

  • object = the object that needs to be filtered
  • 对象=需要过滤的对象。
  • field = the field within that object that we'll filter on
  • 字段=我们要筛选的对象中的字段。
  • filter = the value of the filter that needs to match the field
  • 筛选器=需要匹配该字段的筛选器的值。

HTML:

HTML:

<input ng-model="customerNameFilter" />
<div ng-repeat="(key, value) in filter(customers, 'customerName', customerNameFilter" >
   <p>Number: {{value.customerNo}}</p>
   <p>Name: {{value.customerName}}</p>
</div>

JS:

JS:

  $scope.filter = function(object, field, filter) {
    if (!object) return {};
    if (!filter) return object;

    var filteredObject = {};
    Object.keys(object).forEach(function(key) {
      if (object[key][field] === filter) {
        filteredObject[key] = object[key];
      }
    });

    return filteredObject;
  };

#8


0  

Although this question is rather old, I'd like to share my solution for angular 1 developers. The point is to just reuse the original angular filter, but transparently passing any objects as an array.

虽然这个问题相当老,但我还是想分享一下我的解决方案。关键是重用原始的角度过滤器,但是透明地将任何对象作为数组传递。

app.filter('objectFilter', function ($filter) {
    return function (items, searchToken) {
        // use the original input
        var subject = items;

        if (typeof(items) == 'object' && !Array.isArray(items)) {
            // or use a wrapper array, if we have an object
            subject = [];

            for (var i in items) {
                subject.push(items[i]);
            }
        }

        // finally, apply the original angular filter
        return $filter('filter')(subject, searchToken);
    }
});

use it like this:

使用它是这样的:

<div>
    <input ng-model="search" />
</div>
<div ng-repeat="item in test | objectFilter : search">
    {{item | json}}
</div>

here is a plunker

这是一个一美元