0

I have an application where I want to filter a long list of products based on value from "select" containing product types. The filter works, but only after I select something. It initially sets the "Show All" option, but filters out everything. If I select something else, it works, and if I re-select "Show All" it works. But why doesn't the filter work initially?

The model (looks something like this):

$scope.products = {[
    {name: 'productA',Type: 1},
    {name: 'productB',Type: 1},
    {name: 'productC',Type: 2},
    {name: 'productD',Type: 2},
]};

$scope.productTypes = {[
    {Name: 'typeAlpha',Type: 1},
    {Name: 'typeBravo',Type: 2},
]};

The HTML:

<select id="productFilter" data-ng-model="productFilter">
    <option value="" selected="selected">Show all</option>
    <option data-ng-repeat="type in productTypes" value="{{type.Type}}">{{type.Name}}</option>          
</select>
<p data-ng-repeat="product in products | filter:{Type:productFilter} ">{{product.Name}}</p>
1
  • What is that supposed to mean $scope.products = {[...]}; ? It should be either array $scope.products = [{...}, ...]; or object $scope.products = {}; This example is not working. Commented Mar 11, 2016 at 10:28

4 Answers 4

2

I recommend using ng-options instead of ng-repeat over the options:

<select id="productFilter" ng-model="productFilter" 
  data-ng-options="type.type as type.name for type in productTypes">
    <option value selected="selected">Show all</option>
</select>
<p data-ng-repeat="product in products | filter:(!!productFilter || undefined) && {type: productFilter}">
    {{product.name}}
</p>

For "show all", the filter must return undefined (ng-repeat filter "show all" items if no filter selected)

Also removed the {..} around the array and better use lower case for properties:

  $scope.products = [
    {name: 'productA', type: 1},
    {name: 'productB', type: 1},
    {name: 'productC', type: 2},
    {name: 'productD', type: 2}
  ];

  $scope.productTypes = [
    {name: 'typeAlpha', type: 1},
    {name: 'typeBravo', type: 2}
  ];

Here is a jsbin (based on Hiskinds) http://jsbin.com/yepaqikodo/1/edit?html,js,output

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

Comments

0

This is a working example based on code above: http://jsbin.com/buzafisuko/edit?html,js,output

The Slava.N's comment is correct and you should not wrap productTypes and product in {}

Also, JavaScript is a case-sensitive language, product.Name is always undefined, you should use product.name in your HTML instead.

Comments

0

Use product.Type instead ofType inside 2nd ng-repeat filter

2 Comments

Hm... that seems to always filter out everything
what it is doing exactly?
0

you set $scope.productFilter = ''. so its return by default value blank at filter.

Comments

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.