I have data "like" this:
$scope.persons = {
"ID123": {
name_de: "Andre",
name_en: "Anrew",
age: 30,
description: "He is the father of xyz and . . .",
. . .
},
"IDabc": {
name_de: "Markus",
name_en: "Mark",
age: 20,
. . .
},
"IDxyz": {
name_de: "Isaak",
name_en: "Isaac",
age: 23,
. . .
}
. . .
}
and i have an input/ng-repeat:
<input ng-model="query" placeholder="Suche . . .">
<ul>
<li ng-repeat="p in persons | orderBy:'name_de' | filter:query"> Some output here . . . </li>
</ul>
Now is the question how can i order and filter this?
It worked with an Array of persons, but i need the "ID's" so the object is necessary!?
I am looking also for a way to filter the object by N-properties e.g for name_de AND name_en (so it will show ID123 if i search for Andre and also if i search for Andrew) BUT ignoring the text of "description" (first i had the problem that the filter checks ALL properties)
You can use toArrayfilter module more info please see here :http://ngmodules.org/modules/angular-toArrayFilter
and there sample demo for your solution http://jsbin.com/nimoxa/1/edit JS:
angular.module('angular-toArrayFilter', [])
.filter('toArray', function () {
return function (obj, addKey) {
if (!(obj instanceof Object)) {
return obj;
}
if ( addKey === false ) {
return Object.values(obj);
} else {
return Object.keys(obj).map(function (key) {
return Object.defineProperty(obj[key], '$key', { enumerable: false, value: key});
});
}
};
});
var app = angular.module('app', ['angular-toArrayFilter']);
app.controller('firstCtrl', function($scope){
$scope.persons = {
"IDabc": {
name_de: "Markus",
name_en: "Mark",
age: 20,
},
"ID123": {
name_de: "Andre",
name_en: "Anrew",
age: 30,
description: "He is the father of xyz and . . .",
},
"IDxyz": {
name_de: "Isaak",
name_en: "Isaac",
age: 23,
}
};
});
html:
body ng-app="app">
<div ng-controller="firstCtrl">
<input ng-model="query" placeholder="Suche . . .">
<ul>
<li ng-repeat="p in persons | toArray | orderBy:'age' | filter:query"> {{p.name_de}} -> {{p.name_en}} </li>
</ul>
</div>
</body>
To answer your question on
It worked with an Array of persons, but i need the "ID's" so the object is necessary!?
can you include ID as a property of your person objects so that they will look like this?
$scope.persons = [
{ id: "ID123",
name_de: "Andre",
name_en: "Anrew",
age: 30,
description: "He is the father of xyz and . . .",
. . .
},
{ id: "IDabc",
name_de: "Markus",
name_en: "Mark",
age: 20,
},
. . .
];
To search by name_de AND name_en (or any other properties for the matter), you can try writing your custom filter. It's fairly easy.
var app = angular.module("YourApp");
app.filter('MyCustomFilter', function(){
return function(objects, criteria){
if(!criteria)
return objects;
var filterResult = new Array();
for(var index in objects)
if(objects[index].name_de.indexOf(criteria) != -1 || objects[index].name_en.indexOf(criteria) != -1)
filterResult.push(objects[index]);
return filterResult;
}
});
Your HTML will look like this:
<input type="text" data-ng-model="personFilter" />
<div data-ng-repeat="person in persons | MyCustomFilter:personFilter"> ... </div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With