0

I'm using Angular and I've this object:

$scope.items = {
    'abcdhx3': {name:'file1.jpg', type:'.jpg', size:30000},
    'sxcdhb2': {name:'file2.jpg', type:'.png', size:30000},
    'k4cdhx5': {name:'file3.jpg', type:'.jpg', size:30000},
    '23cdhd3': {name:'file4.jpg', type:'.png', size:30000},
    'ascdhx3': {name:'file45.jpg', type:'.png', size:30000}
};

I want to filter this object based on "name" and "type" values that I can get from input texts. So how can I do that using a filter in ng-repeat, for example: I want to show files that contains "file4" with type ".png".

<div data-ng-repeat="(key, item) in items">
    <div>{{ item.name }}</div>
</div>

2 Answers 2

1

I've solved the problem using a transformation filter like this:

angular.module('test').filter('itemsFilter', [
    function() {
        return function(items) {
            var list = [];
            for (var i in items) {
                list.push(items[i]);
            }
            return list;
        };
    }
]);

And filtering items before apply the search filter

Name: <input type="text" ng-model="search.name" />
Type: <input type="text" ng-model="search.type" />
<div ng-repeat="item in items | itemsFilter | filter:search">
    <li>{{item.name}}</li>
    <li>{{item.type}}</li>
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this by using a simple Filter:

HTML

Name: <input type="text" ng-model="search.name" />
Type: <input type="text" ng-model="search.type" />
Whatever: <input type="text" ng-model="search.$" />
<ul ng-repeat="item in items | filter:search">
    <li>{{item.name}}</li>
    <li>{{item.type}}</li>
</ul>

3 Comments

It doesn't works, I've tried that way without success, when "items" is an array it works, but "items" is an object and all filter properties are inside each item value.
@Ragnar You cannot filter an object. You would need to convert it to an array. Object is a mere kvp. You would need to implement your own filter for that
I'm trying using a filter function but it doesn't works "ng-repeat="item in items | filter:filterFunction" with filterFunction(item, index) {..}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.