1

Using Angular, I'm trying to filter using ng-repeat on FactorName given the following schema.
Filtering using <... ng-model="query.Factors.FactorName" ...> doesn't work (not surprisingly). I haven't seen literature on anyone trying to filter in this way.

Does Angular support filtering based on a subarray element property ?

[
   {
      "Name":"1",
      "Factors":[
         {
            "FactorName":"FactorOne",
            "Type":"SomeType"
         },
         {
            "FactorName":"FactorTwo",
            "Type":"SomeType"
         }
      ]
   },
   {
      "Name":"2",
      "Factors":[
         {
            "FactorName":"FactorThree",
            "Type":"SomeType"
         },
         {
            "FactorName":"FactorFour",
            "Type":"SomeType"
         }
      ]
   }
]
CodeOcelot
  • 3,403
  • 3
  • 22
  • 23
  • 1
    What have you tried? What do you mean by filtering on factor name? If you follow the [phonecat sample](https://docs.angularjs.org/tutorial/step_03) it should work, but the filter is generic. You could write your own filter easy enough. – Jason Goemaat Jul 24 '14 at 18:35
  • Need to see some actual angular code here – Mike Cheel Jul 24 '14 at 18:48
  • possible duplicate of [Angular filter on child property](http://stackoverflow.com/questions/20579177/angular-filter-on-child-property) – null Jul 24 '14 at 19:18

2 Answers2

1

There is also another place where you can iterate on a array, working along side ng-repeat, in the double bindings with a custom filter.

Working Demo

HTML

<ul ng-repeat="x in factorData">
  <li>
    {{ x.Factors | factorFilter }}
  </li>
</ul>

JS

app.filter('factorFilter', function() {
    return function(items) {
        var results = [];
        if (items) {
            for (var i = 0; i < items.length; i++) {
                results = items[i]['FactorName'];
            }
            return results;
        } else {
          return 'Doh!';
        }
   }
})    
cheekybastard
  • 5,535
  • 3
  • 22
  • 26
  • Awesome! For anyone else trying to find a solution, the following slight modification works nicely: The search box: `input( type="text" ng-model="factorFilter" )` Data presentation: `div(ng-repeat="c in data | filter:factorFilter ")` – CodeOcelot Jul 25 '14 at 23:08
0

I figured it out to make the filter on any of the elements of the array Factors, but could not figure how to select just one field of the array. Maybe it just works for you:

<input ng-model="filtervalue.Factors"/><br/><br/>
Filtered:
<p><span ng-repeat="d in data | filter:filtervalue:false">{{d}}</span></p>

And the initialization on JS:

$scope.filtervalue = {};

Sample on jsfiddle:

http://jsfiddle.net/jordiburgos/QFUu6/
jordiburgos
  • 5,964
  • 4
  • 46
  • 80