0

I see a couple of other questions like this, but their answers aren't working for me.

I have the code below which is displaying data in a table using AngularJS. I want to filter the query to only display the SecurityID numbers of 8, 9, and 10. When I add the filter as shown below I get nothing. When I remove the filter all of the securities appear. What is the easiest way to write this filter?

    <div ng-app="myApp">
        <div ng-controller="SecuritiesCtrl">
            Select A Forex Pair:
            <select ng-model="SecuritiesModel" ng-options="x.SecurityID as x.TrueSymbol for x in Securities | filter:{ SecurityID: 8,9,10 }" ng-change="GetQuotes();" ng-bind="date | date:'MM/dd/yyyy'"></select><br />

         <table id="QuotesTable" class="MyTable" style="display:none;">
              <tr>
                <th class="MyTable">Time</th>
                <th class="MyTable">Open</th>
                <th class="MyTable">Hi</th>
                <th class="MyTable">Lo</th>
                <th class="MyTable">Close</th>
              </tr>
              <tr ng-repeat="x in Quotes">
                <td class="MyTable">{{ x.QuoteTime }}</td>
                <td class="MyTable">{{ x.Open }}</td>
                <td class="MyTable">{{ x.Hi }}</td>
                <td class="MyTable">{{ x.Lo }}</td>
                <td class="MyTable">{{ x.Close }}</td>
              </tr>
            </table>
        </div>
    </div>

1
  • u should use custom filter for this. Commented Apr 15, 2016 at 13:33

1 Answer 1

1

For a filter on several values, I recommend you to create a controller function to filter.

$scope.interestingSecurityIds = [8, 9, 10];

$scope.filterBySecurityId = function(security) {
    return ($scope.interestingSecurityIds.indexOf(security.SecurityID) !== -1);
};

With this filter in your view :

<select ng-model="SecuritiesModel" ng-options="x.SecurityID as x.TrueSymbol for x in Securities | filter: filterBySecurityId" ng-change="GetQuotes();" ng-bind="date | date:'MM/dd/yyyy'"></select>

JSFiddle mockup here.

You can also refer to this answer.

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

9 Comments

Thanks for the reply, but I can't get this to work either. I don't know why. I tried putting a breakpoint in the filterBySecurityId function, but it never hits in Developer Tools.
@SteveGaines There is no error in the console logs ?
I think he missed calling function name. Use like this filter: filterBySecurityId()
@bivale - It looks like there is an error in console logs. I'm pretty new to using Developer Tools. Here's what it says (in part): Error: options is undefined writeNgOptionsValue@stevegaines.us/Scripts/angular.js:27783:15
@ArunShinde Nope you should not use paranthesis : docs.angularjs.org/api/ng/filter/filter
|

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.