selectattr

SOLVE
ben-duchy
Top Contributor

I'm creating a filter that needs to cover an 'or' option, but I can't get it to work beyond the first option. Any ideas what I'm doing wrong?

 

 

{% set table = hubdb_table_rows(xxxxxxx, queryparam) %}

{% set performance = table | selectattr ("plot_status.name","equalto","Exchanged") or ("plot_status.name","equalto","Completed")

{% for row in performance %}
 /* Only seems to display plots that are 'Exchanged' */
{% endfor %}

 

 

0 Upvotes
1 Accepted solution

Accepted Solutions
amwilie
Solution
Key Advisor | Elite Partner

I'm don't think OR is really possibly with selectattr. Instead you'll want to create variables for each filter and then use |union to combine them (union will deduplicate any common items so if there's an item in both lists it'll only show up once in the for loop).

 

{% set table = hubdb_table_rows(XXXXXX) %}

{% set performance = table|selectattr("plot_status.name","equalto","Exchanged") %}
{% set performance_2 = table|selectattr("plot_status.name","equalto","Completed") %}

{% for row in performance|union(performance_2) %}

{% endfor %}

 

 

border

Alyssa Wilie

Web Developer at LyntonWeb

If this answer solved your question, please mark it as the solution!

View solution in original post

2 Replies 2
amwilie
Solution
Key Advisor | Elite Partner

I'm don't think OR is really possibly with selectattr. Instead you'll want to create variables for each filter and then use |union to combine them (union will deduplicate any common items so if there's an item in both lists it'll only show up once in the for loop).

 

{% set table = hubdb_table_rows(XXXXXX) %}

{% set performance = table|selectattr("plot_status.name","equalto","Exchanged") %}
{% set performance_2 = table|selectattr("plot_status.name","equalto","Completed") %}

{% for row in performance|union(performance_2) %}

{% endfor %}

 

 

border

Alyssa Wilie

Web Developer at LyntonWeb

If this answer solved your question, please mark it as the solution!

View solution in original post

ben-duchy
Top Contributor

Hi @amwilie ,

 

Thats a good trick, thank you. I wasn't aware of this filter type.

 

Just to add to this, is it possible to combine even more lists using 'union'? I tried the below but it only returns 1 & 2

{% set performance_1 = table | selectattr("plot_status.name","equalto","Exchanged") %}
{% set performance_2 = table | selectattr("plot_status.name","equalto","Completed") %}
{% set performance_3 = table | rejectattr("sale_type.name","equalto","Affordable") %}
{% set performance_4 = table | selectattr("forecast_year.name","equalto","20/21") %}
{% set performance_5 = table | selectattr("forecast_year.name","equalto","21/22") %}

{% for row in performance_1|union(performance_2,performance_3,performance_4,performance_5) %}

 

Having tried different combinations, I feel that I'm close, but just can't figure out the last part 🤔

0 Upvotes