Search code examples
javascriptember.jsember-cli

Filtering between two values in emberjs


I am trying to filter items that are between two values that are inputed as text. For example if I have this values:

people=[
{
   name: 'Ricky',
   age: 21
},
{
   name: 'Mike',
   age: 32
},
{
  name: 'Arianna',
  age: 23
}
];

And in the template I have something like this:

{{input type='number'}}min Age
{{input type='number'}}max Age

How would I only display the people who are between the age of 20 and 25? Having the values 20 and 25 acquired via the input. jsbin: http://emberjs.jsbin.com/kosoyo/1/edit?html,js


Solution

  • You didn’t give much context, but assuming that those values are stored in people:

    people.filter(function(person) {
      return person.age >= 20 && person.age <= 25;
    });
    

    Given your update, still lacking context:

    var lowerBound = parseInt(this.get('lowerBound'));
    var upperBound = parseInt(this.get('upperBound'));
    
    people.filter(function(person) {
      return person.age >= lowerBound && person.age <= upperBound;
    });
    

    Here is a working version of your JS Bin. You were trying to use lowerBound and upperBound without geting their values from the form. You were also trying to use filterBy improperly: filterBy only works for simple equality, you need filter for a more complex comparison.