I set a range slider that ranges from 0 to 2 hours, times are calculated in minutes and then converted to hh: mm, like this: 10min, 20min, 1hr 20min, 2hr.
But now I'm trying to filter out a bunch of elements inside ng-repeat using the range given by the range slider, and it's hard for me to handle it.
Here is what I did http://cdpn.io/LDusa
I am using http://danielcrisp.imtqy.com/angular-rangeslider/demo/ for the range slider.
And here is the code:
var app = angular.module('myApp', ['ui-rangeSlider']); app.controller('myCtrl', function ($scope) { $scope.sliderConfig = { min: 0, max: 120, step: 10, userMin: 0, userMax: 30 }; $scope.times = [ {"time": "20"}, {"time": "50"}, {"time": "30"}, {"time": "10"}, {"time": "85"}, {"time": "75"}, {"time": "95"}, {"time": "100"}, {"time": "80"}, {"time": "200"}, {"time": "260"}, {"time": "120"}, {"time": "62"}, {"time": "68"}, {"time": "5"}, {"time": "116"} ]; }); app.filter('customFilter', function () { return function (value) { var h = parseInt(value / 60); var m = parseInt(value % 60); var hStr = (h > 0) ? h + 'hr' : ''; var mStr = (m > 0) ? m + 'min' : ''; var glue = (hStr && mStr) ? ' ' : ''; return hStr + glue + mStr; }; }); app.filter('range', function() { return function(input, min, max) { min = parseInt(min); max = parseInt(max); for (var i=min; i<=max; i++) input.push(i); return input; }; });
JADE (If you prefer HTML, you can convert it back to html using http://html2jade.org/ ):
div(ng-app="myApp") div(ng-controller='myCtrl') div(range-slider='', pin-handle='min', attach-handle-values='', prevent-equal-min-max='', step='{{sliderConfig.step}}', min='sliderConfig.min', max='sliderConfig.max', model-min='sliderConfig.userMin', model-max='sliderConfig.userMax', filter='customFilter') div(range-slider, prevent-equal-min-max='', attach-handle-values='', min='sliderConfig.min', max='sliderConfig.max', model-min='sliderConfig.userMin', model-max='sliderConfig.userMax', filter='customFilter') div(ng-repeat="time in times | range:sliderConfig.userMin:sliderConfig.userMax") | {{ item.time | customFilter }}
How can I get this work to display only those elements when time is within the range specified in the range slider?