Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 784 Vote(s) - 3.43 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to filter (key, value) with ng-repeat in AngularJs?

#1
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:

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) ?
Reply

#2
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:


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

Reply

#3
Or simply use

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

See updated solution here:

[To see links please register here]

Reply

#4
You can simply use [angular.filter][1] module, and then you can filter even by nested properties.<br/>
see: [jsbin][2]<br/>
**2 Examples:**<br/>

**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:**

<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>

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#5
Also you can use `ng-repeat` with `ng-if`:

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

#6
Angular [filters](

[To see links please register here]

) can only be applied to arrays and not objects, from angular's 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:

<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**:

[To see links please register here]

Reply

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

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

#8
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:**

<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:**

$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;
};
Reply

#9
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]


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through