Search code examples
javascriptjqueryjsonangularjsrangeslider

How to filter data in ng-repeat using a jquery slider?


I am using Ion.RangeSlider that is a jquery slider. I have json data that is coming through http and i am displaying it in the page using ng-repeat directive. I want to filter the JSON data using this slider that looks like below

enter image description here

under the image you can see two values 185;500 that is the value i am getting when i bind ng-model to the slider.

so, my questions is how do i filter my data using this slider?

EDIT I changed the Slider that i was using and made the same CSS changes to make it look like the above slider. https://github.com/rzajac/angularjs-slider


Solution

  • Here is how you can do that with ng-repeat and a custom filter. Just use your binded value and replace them as min and max in myfilter like:

    <div ng-repeat="item in items | myfilter: { min: slider.min, max: slider.max }">
    

    JSFiddle

    HTML:

    <div ng-app="app" ng-controller="dummy">
        <div ng-repeat="item in items | myfilter: { min: 185, max: 500 }">
            <p>{{item.name}}</p>
        </div>
    </div>
    

    JS:

    var app = angular.module("app", []);
    app.controller('dummy', function($scope, $sce) {
        $scope.items = [{name: 'hello', cost: 100}, {name: 'world', cost: 200}]
    });
    app.filter('myfilter', function() {
       return function( items, condition) {
        var filtered = [];
    
        if(condition === undefined || condition === ''){
          return items;
        }
    
        angular.forEach(items, function(item) {          
           if(item.cost >= condition.min && item.cost <= condition.max){
             filtered.push(item);
           }
        });
    
        return filtered;
      };
    });