Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I created a custom filter, but its giving me an error I created a fiddle here:

fidddler

I have this user data:

    data: [{
        profile: {
            firstName: 'John',
            lastName: 'OConner'
        }
    }, {
        profile: {
            firstName: 'Smith',
            lastName: 'OConner'
        }

    }, {
        profile: {
            firstName: 'James',
            lastName: 'Bond'
        }
    }]

And I need to filter by the nested obj - profile by this

data: [{
        column: {
            label: 'firstName',
        }
    }, {
        column: {
            label: 'lastName',
        }

    }]

I can filter but is giving me this error:

this is my filter:

myApp.filter('testFilter', ['$filter',
function($filter) {

    return function(items, selectedFilter) {

        var returnArray = items;
        var filtered = [];

        var process = {};
        process.filtered = [];

        process.loop = function(obj, key) {
            var filtered = [];
            this.obj = obj;
            this.key = key;
            // console.log('obj--> ', obj);
            // console.log('key--> ', key);

            filtered = filtered.concat($filter('filter')(items, process.superFilter));

            if (filtered.length > 0) {
                process.filtered = filtered;
            }

        };

        process.superFilter = function(value) {
            var returnMe;
            var originalValue = value.profile[process.key];

            if (typeof(value) === 'String') {
                originalValue = originalValue.toLowerCase();
            }

            if (originalValue === process.obj) {
                console.log('found');
                returnMe = value;
                return returnMe;
            }

        };



        if (Object.getOwnPropertyNames(selectedFilter).length !== 0) {
            angular.forEach(selectedFilter, function(obj) {
                filtered = filtered.concat($filter('filter')(items, obj));
            });

            returnArray = filtered;

            // console.log('selectedFilter ', selectedFilter);
        }


        return returnArray;
    };
}

]);

Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys.

Thanks

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You need to use track by as the error suggests. If you don't have a unique key to use you can use $index.

ng-repeat='talent in talents.data | testFilter:filterInput track by $index'

Here is a working example with your code: http://jsfiddle.net/hwT4P/

share|improve this answer
    
sorry my fiddler was wrong, the object that I wanted to filter was wrong. I updated the questions. I have a custom filter, and I want to filter by the nested object profile. Thanks –  BangoTango Jul 31 at 8:49
    
I've updated my answer. Let me know if it's not what you're looking for otherwise please mark as answer. –  snowman4415 Jul 31 at 19:03
    
Thank you for coming back to me again @snowman4415, but theres something not right, because as soon you use the two fields, it duplicates the items array. :/ –  BangoTango Aug 1 at 9:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.