1

I am repeating through a data model that contains key/value pairs. Let's say there are 30 rows, and 5 of them contain the string 'map' in the key.

I want to filter the display to show only those objects that contain the string 'map'. I know this is a filter, however, I am not having much luck with the documentation - none of it deals with ng-repeat on key/values.

Any idea on how to achieve this?

<div class="row" 
    data-ng-repeat="(textKey, textValue) in publisher.update.file">
    <div class="small-3 columns">
        <label class="text-right inline" 
            for="file"
            id="textKey">
            {{ textKey }}
        </label>
    </div>
    <div class="text-align-left small-9 columns">
        <input name="textKey"
            ng-model="publisher.update.lang[textKey]">
    </div>
</div>
2
  • 1
    can you not use ng-show? On your element, you can <span ng-show="textValue=='map'"></span> Commented Jan 22, 2015 at 22:41
  • ^ That's the one! Thanks. Commented Jan 23, 2015 at 18:54

1 Answer 1

3

I doubt you can do that with default filter

doc says:

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

But you can create your own filter to achieve this. here is the example

var app = angular.module('app', []);


app.filter('Find', function() {
  return function(input, str) {
    var tmp = {};
    angular.forEach(input, function(val, key) {
      if (val.indexOf(str) !== -1) {
        tmp[key] = val;
      }
    });
    return tmp;
  };
})
app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
  <div ng-controller="MainCtrl">
    <div ng-init="test = {test1:'map',test2:'map2',test3:'test'}"></div>

    Search:
    <input ng-model="search" type="text">
    <br>
    <table id="searchTextResults">
      <tr>
        <th>key</th>
        <th>val</th>
      </tr>
      <tr ng-repeat="(key, val) in test| Find: search||'' ">
        <td>{{key}}</td>
        <td>{{val}}</td>
      </tr>
    </table>
    <hr>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.