10

Is it possible to filterBy multiple values in emberjs?

I am trying to filter items in a table with different filtering variables and I'm having trouble doing so with more than one of those variables.

Can anyone help out? I'm ne to emberjs and eager to learn. Thanks in advance.

FutoRicky
  • 903
  • 2
  • 9
  • 22

2 Answers2

10

You can't pass multiple properties into the same filterBy, but you can filterBy on the same array 2 times, i.e. chain your filterBy calls if that makes sense.

See the following answer I recently gave (here) for a working demo of what I am talking about

So, in short, if you have an array arr and you want to filter it by country and name properties for example, you would do:

arr.filterBy('country', countryName).filterBy('name', 'Josh')

You can also just use the filter (as opposed to filterBy) method and filter things any way you would like.

Community
  • 1
  • 1
Kalman
  • 8,001
  • 1
  • 27
  • 45
  • What if I want to filter by a state and a county selected in different dropdowns? How do I add the selected value to the method parameters? – FutoRicky Jan 23 '15 at 20:52
  • @FutoFarai use a filter function instead of filterBy as kalman mentioned. filterBy is syntactic sugar around the filter method, if you just need to filter with one property. – blessanm86 Jan 24 '15 at 02:16
  • 2
    I threw together a jsbin to filter by both country and state - http://emberjs.jsbin.com/beseqo/2/edit?html,js,console,output – Kalman Jan 25 '15 at 04:15
  • Thats awesome, exactly what I needed. One question, the view "select" helper, is there one for checkboxes? Do filtering but with values that you select by checkbox. Sorry for the load of questions. – FutoRicky Jan 26 '15 at 21:31
  • Check out the following for how to use checkboxes in Ember http://emberjs.com/api/classes/Ember.Handlebars.helpers.html#toc_use-as-checkbox – Kalman Jan 26 '15 at 21:35
  • How would the checkbox input work when using non-boolean values like a string? If its possible.. – FutoRicky Jan 26 '15 at 22:34
  • Also, how can I default that when it is back to "Pick One..." to show all of them? Sorry for all the questions, I just find that you are very knowledgeable in emberjs since you are the one that always answers my questions about emberjs here in SO. – FutoRicky Jan 26 '15 at 22:58
  • Returning to "Pick One..." now shows all of them http://emberjs.jsbin.com/beseqo/3/edit I am doing `if(country)` instead of checking for undefined... – Kalman Jan 27 '15 at 01:58
  • Checkboxes are booleans - that's what they are. If you need a string - you can always display a label next to them – Kalman Jan 27 '15 at 01:59
  • @KalmanHazins you have been the best help in SO so far, so can you check out this question to see if you can answer it? Im beginning to get frustrated http://stackoverflow.com/questions/28179679/how-to-implement-multiple-filters-with-checkboxes-in-emberjs – FutoRicky Jan 28 '15 at 20:48
  • I get the message `TypeError: Ember.default.computed.filterBy(...).filterBy is not a function` –  Sep 06 '15 at 06:39
2

I blogged a solution at http://www.emberdaily.com/2019/02/25/filter-by-multiple-values/

Essentially,

this.get('myProjects').filter(project => project.get('status') === 'done' || project.get('status') === 'active');
Emad
  • 4,110
  • 5
  • 30
  • 35