1

How can I filter on first click and then reset filter on second click ? I want to show items having images first and then reset filter on second click. Please check [fiddle][1] [1]: http://jsfiddle.net/6L7xn/9/ Thanks

  <button class="btn btn-primary" ng-click="myFilter = {'largeImage': {'hasImage': 'Yes'}}">Images</button

2 Answers 2

1

I have forked your jsfiddle: http://jsfiddle.net/jkrielaars/n5q9xeej/2/

There were a few things I had to change:

  • your original filter was checking for hasimage:Yes (with a capital). In your objects the yes was all lowercase
  • in order to enable "deep filtering" you need to pass :true to the filter as third param
  • I made a toggle function that toggles the myFilter object between '{}' and the thing you wanted to filter on.

Controller:

    $scope.myFilter = {};
    $scope.toggleFilter = function(){
        if(angular.equals({}, $scope.myFilter)){
            $scope.myFilter = {'largeImage': {'hasImage': 'yes'}};
        }else{
            $scope.myFilter = {};
        }
    }

And the view:

    <button class="btn btn-primary" ng-click="toggleFilter()">Images</button>

    <div class="item-sc" data-ng-repeat="d in things | filter:myFilter:true">
Sign up to request clarification or add additional context in comments.

Comments

0

Simple enough to add a function to set a scope variable in your controller that you can call from ng-click. Then set your filter to equal the value in the scope variable. Like this:

Add to controller

$scope.myFilter = 'no' // or whatever you want to default to;
$scope.setMyFilter = function(){
    $scope.myFilter = $scope.myFilter === 'no' ? 'yes' : 'no';
};

HTML

<div ng-app="MyApp">
<div class="container" ng-controller="sentCtrl">
    <div class="row">
        <div><strong>Only show:</strong>&nbsp;
            <div class="btn-group btn-group-xs">
                <button class="btn btn-primary" ng-click="setMyFilter()">Images</button>
            </div>
        </div>
    </div>
    <div class="row content-sc">
        <div class="item-sc" data-ng-repeat="d in things | filter: {largeImage.hasImage: myFilter}">
             <h4>{{d.NAME}}<br/><small>ID: {{d.ID}}</small></h4>

        </div>
    </div>
</div>

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.