Pregunta

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

Fiddle

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. How can I solve this issue?

¿Fue útil?

Solución

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/

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top