Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use orderBy and filter with ng-repeat, using an object (instead of array)?

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)

like image 224
easyX Avatar asked Aug 09 '14 18:08

easyX


2 Answers

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>
like image 63
sylwester Avatar answered Oct 18 '22 10:10

sylwester


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>
like image 44
ivan.sim Avatar answered Oct 18 '22 09:10

ivan.sim