Antony
April 2, 2020, 8:37am
1
When creating sub-arrays, you can want them with an and
or an or
function.
I can do the and
function with multiple .where() conditions
my_array.where(`field1`, 1, "==").where(`field2`, 1, "==")
So now I want to do the or
function - so basically to merge two arrays:
my_array.where(`field1`, 1, "==") MERGED WITH my_array.where(`field2`, 1, "==")
How do I do that?
While this kind of thing can be done during database access, I need it for Data Store access.
Teodor
April 6, 2020, 9:43am
3
Hello Antony,
What is your idea? What are you trying to achieve exactly?
Antony
April 6, 2020, 10:20am
4
I want a sub array of my_array where field1==1
OR field2==1
.
So if I have:
Name field1 field2
==================
Fred 1 1
John 1 0
Jane 0 1
Mary 0 0
I want an array with Fred, John and Jane.
The .where(field1==1).where(field2==1)
structure will only give me Fred.
It is the basis of some very complex search functions I need to carry out on my contacts
Data Store.
Teodor
April 6, 2020, 10:36am
5
So why not try doing:
my_array.where(`field1`, 1, "==") || my_array.where(`field2`, 1, "==")
1 Like
Antony
April 6, 2020, 11:26am
6
oh fab, so || works for arrays too?
Teodor
April 6, 2020, 11:28am
7
Well, not sure what you mean it works for arrays, but of you want to return the values you mentioned in your previous post, the expression should be:
{{my_array.where(condition1) || myarray.where(condition2)}}
and as your explanation is not really clear, and as i don’t understand what are you trying to achieve exactly and where your data comes from and how - please try this and let me know if it works.
I’m sorry, but {{my_array.where(condition1) || myarray.where(condition2)}}
will not work. It will always return the first filtered array. At this moment it is not possible to do an OR
unless you write your own custom formatter.
1 Like
Antony
April 6, 2020, 2:19pm
9
Thanks for your help in this folks!
@patrick , is there any documentation on writing your own custom formatter?
TomD
April 6, 2020, 3:12pm
10
There are a few threads with examples of using custom formatters.
There are a couple of alternative methods to achieve what you want (I think). You could create a custom query using CASE WHEN, or you could use Query Builder and add a virtual column to your table (if you’re using MySQL anyway).
Is on the server-side or client-side that you need it?
For client-side formatters:
You could try my_array.filter('field1 == 1 || field2 == 1')
, it is a formatter to filter an array that isn’t available in the UI.
A custom formmatter looks like:
dmx.Formatters('array', {
my_filter: function(arr) {
return arr.filter(function(row) {
return row.field1 == 1 || row.field2 == 1;
});
}
});
usage: my_array.my_filter()
.
3 Likes
Antony
April 6, 2020, 3:28pm
13
Thanks for your help folks!
It is all totally on the client side, using data from a Data Store, so no MySQL involved.
I’ll give that a go Patrick!