Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
42
rated 0 times [  43] [ 1]  / answers: 1 / hits: 12857  / 11 Years ago, mon, january 13, 2014, 12:00:00

I'm having some trouble getting angular to properly filter my results. I'm attempting to use a custom filter that gets arguments from a minimum input and a maximum input.



/index.html



<input ng-model=minHorsepower>
<input ng-model=maxHorsepower>
...
tr(ng-repeat=plane in planes | filter:horsepowerFilter)


/controllers.js



//Horsepower filter
$scope.horsepowerFilter = function(plane) {
var ret = true;

if($scope.minHorsepower && $scope.minHorsepower > plane.horsepower) {
ret = false;
}

if($scope.maxHorsepower && $scope.maxHorsepower < plane.horsepower) {
ret = false;
}

return ret;
};

$scope.planes = [
{
'make' : 'Piper',
'model' : 'Arrow',
'modelNumber' : 'PA-28R-180',
'horsepower' : '180',
'gear' : 'retractable',
},
{
'make' : 'Piper',
'model' : 'Arrow',
'modelNumber' : 'PA-28R-200',
'horsepower' : '200',
'gear' : 'retractable',
}
];


It works INITIALLY when I set $scope.minHorsepower/$scope.maxHorsepower in controllers.js, but only initially, not when I put something else in the <input>s. Furthermore, it prefills the inputs AND filters the results. It just doesn't work properly when I change the value of the inputs.



I've referenced this Stack Overflow thread, but I can't find any material differences in our code... AngularJS multiple filter with custom filter function



Thanks for the help.


More From » angularjs

 Answers
2

Unfortunately, I can't pinpoint exactly the problem you're having with your code. However, I think I have created a plnkr that (I believe) works as intended.



Apart from parsing the inputs with parseFloat, the logic seems to be the same. Not parsing the inputs to a numeric form shouldn't break it, but will possibly make it behave strangely (9 > 10 for example).



Anyway, hopefully you can pull out the useful pieces and get it working.


[#48742] Sunday, January 12, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austenjordang

Total Points: 544
Total Questions: 112
Total Answers: 112

Location: Monaco
Member since Sun, Jan 16, 2022
2 Years ago
;