2

I have an object that maps IDs to their objects. I would like to display the list of these object and use a filter, but I cannot get it to work. Specifically, I'm trying to prevent object with ID 2 from appearing in the options Here's what I've got: http://jsfiddle.net/9d2Za/

<div ng-app ng-controller="ctrl">
    Unfiltered:
    <select ng-model="selectedBidType"
        ng-options="bidType.id as bidType.label for (bidTypeID, bidType) in bidTypes">

    </select>
    <br>
    Filtered:
    <select ng-model="selectedBidType"
        ng-options="bidType.id as bidType.label for (bidTypeID, bidType) in bidTypes | filter:{id: '!2'}">

    </select>
</div>

Please note: I cannot change the structure of the bidTypes object in whatever fix we come up with. Here's my AngularJS:

function ctrl($scope)
{
    $scope.selectedBidType = 1;
    // Bid type objects are indexed by ID for fast lookup (this cannot be changed in solution)
    $scope.bidTypes = {
        1: {id: 1, label: "Buy"},
        2: {id: 2, label: "Sell"},
        3: {id: 3, label: "Missing"}
    };
}
Dave
  • 439
  • 1
  • 6
  • 18

1 Answers1

2

As described by the documentation, the filter filter only accepts an array as first parameter, not an object. In such cases, I personally use a custom filter to make the transformation:

myModule.filter(
        'objectToArray',
        [
            function ()
            {
                return function (object)
                {
                    var array = [];
                    angular.forEach(object, function (element)
                    {
                        array.push(element);
                    });

                    return array;
                };
            }
        ]
    )
;

And then, in the template:

 <select ng-options="… in bidTypes | objectToArray | filter:{id:'!2'}">
Blackhole
  • 20,129
  • 7
  • 70
  • 68
  • If I want to remove two elements How can I specify in the filter??I tried |filter:{id:'!2' '!3'} but not working – forgottofly Jan 14 '15 at 04:15
  • @MANOJ [This question may help you](http://stackoverflow.com/questions/15868248/how-to-filter-multiple-values-or-operation-in-angularjs). – Blackhole Jan 14 '15 at 20:46