7

I use the ngOptions directive in the HTML given below:

<select ng-model="selectedGroupToShow.Id" ng-options="g.Id as g.Name for g in myGroups">
    <option value>-- All --</option>
</select>

The filter I am using in ng-repeat to show my data goes like this:

filter:{GroupId:selectedGroupToShow.Id}"

Even if I use

filter:selectedGroupToShow

the issue stays the same.Resetting the filter..

This works perfectly, but when I want to clear the filter (select default option - 'All') it will only show data that doesn't have GroupId parameter set at all.

How can I show ALL the data (clear the filter) when choosing default option element within select?

2 Answers 2

24

I think I understand your problem and the issue you are facing is when dropdown selected to --ALL-- its value is null and not undefined. So to reset it you have to set it for undefined.

<div ng-repeat="item in items| filter:{itemId:selectedGroupToShow.Id||undefined}">
         {{item.itemId}}
</div>

Please refer the below fiddle I have created for your problem.

fiddle

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

5 Comments

You understood correctly:) Unfortunately If I use || undefined as you've shown above I get an error. If I try to put bracers () around it stays null when All is selected. I use the second filter in my question -> ng-repeat="item in items | filter:searchItemsText | filter:selectedGroupToShow || undefined". I probably use wrong structure for || ?
I just used your syntax with parameter:value and it worked, I'll just leave it so. Thank you!
still if you know how to accomplish the same thing with the filter I wrote in the first comment above I'd be glad to know how
I guess item is a json object you are using, if so then I'm afraid to say how will your filter:searchItemsText will work since on what basis should it filter the value. If you can create a fiddle for your problem with the exact object structure then we might be able to help you.
searchItemsText is not the issue, you can leave that one out (it works). I only had problems converting your answer to work with the second filter above
1

I did it this way, just with an empty String value.

Hope it could help anyone.

JADE code:

select.form-control(ng-model="productType", ng-init="productType='free'")
        option(value="free") FREE
        option(value="interest") INTEREST
        option(value="hybrid") HYBRID
        option(value="") ALL

.col-sm-6.col-md-4.col-lg-3(ng-repeat="product in productList | filter:{ type :productType }") {{product.name}} 

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.