Add data from a repeat into an array

yes

Basicaly I want to retun some Ids from a database query. For the returned Ids I want to activate (that are not disabled I mean) some checkboxes. I have managed to create the functionality of activating specific checkboxes based on array values. I just need to find the way to fill the array with the values returned from the database query.

So you can use the dynamic events of server connect > on Done > add unique to the array. Select your query, then add collections > values formatter and select the column which values you want to return.
In the UI it looks like:

In the code it looks like:

 dmx-on:done="arr1.addUniq(serverconnect1.data.query1.values(`mycolumn`))"
1 Like

Sounds nice. Will try and let you know. Thanks!

1 Like

The ‘issue’ is that I am trying to get the values from a repeat expression in the query.
This is a demo page (I am trying to get the nested values 1,10,12,4,7,12)
https://bike.nikosbeligiannis.com/_delete_to_array.php

I am trying something like this

<dmx-serverconnect id="serverconnect1" url="/dmxConnect/api/front/shop/filters/get_all_filters_that_exist_with_main_filter.php" dmx-param:filter_clicked="12" dmx-on:done="arr1.addUniq(serverconnect1.data.repeat1.query_related_filters.values('fts_filter_prop_fgn'))"></dmx-serverconnect>
1 Like

Nested repeats make this a bit more complex but you can still return an array to the front end. A few things need to be added to the server action.

  • Add a setvalue step before your repeat, add a name and a global name to it. Set the value to null, no need to enable Output

  • Add the same setvalue step (same name/global name) in the repeat. It’s value should be set to:
{{myarr + query_related_filters.join(',', 'fts_filter_prop_fgn')}}

output should not be enabled.

  • Add another setvalue step at the end of your server action, after the repeat. Call it result and set the myarr variable as a value, then apply the split formatter, split by ,:
{{myarr.split(',')}}

Output should be enabled.
On the front end you will have an array with all the values from the nested repeat, returned by the result variable :slight_smile:

2 Likes

First of all thank you for your help.
There are seem to be some small issues.
If you check the smaple page
https://bike.nikosbeligiannis.com/_delete_to_array.php

1.you will notice that between two distinct categories a ‘,’ is not added.
2. I need the values passed into the array to be considered as numbers. I am telling you this because when I was trying this to make it work (to activate checkboxes that only exist in the array) I had an issue when I was adding the values through the ‘input’ box. Although the values were added in the array the checkbox was not updated. I had to add the ‘.toNumber()’ to the value I was adding. Since the checkboxes are not updated when getting the values from the ‘result’ array I might have to do the same process again (convert values from the result array to number).

Thank you!

Ah yes, i see the issue. You can do this a in a bit hacky way, changing:

{{myarr + query_related_filters.join(',', 'fts_filter_prop_fgn')}}

to

{{myarr + query_related_filters.join(',', 'fts_filter_prop_fgn') + ','}}

This way there will always be an , at the end, but i believe it should be working fine even with it.

Yeah nice, it is just that it leaves an empty ‘,’ at the end but this does not really bother since all the values are in the array. The issue now is how to make these values considered as numbers so the checkboxes are actually updated.

Are the values in your database stored as numbers actually?

Yes they are…

Just checked the Network tab. The ‘result’ array is like this
result":[“1”,“10”,“12”,“4”,“7”,“12”,""]}

I will try to use a flow and change the values to integer with javascript. Thank you!

Ok so some more changes will be required.
Please remove the split from the results setvalue step so it becomes just {{myarr}}
Then on the page, where you use the values use this code:

{{serverconnect1.data.result.split(',').map('$value.toNumber()')}}

@Teodor I am very close to achieving the final result. With the array we are great. The issue now is that I want to disable the checkboxes that do not contain any value of the array. But when the page loads the array will be NULL and this results to all checkboxes to be unchecked.

WHAT I WANT
I need to initially all checkboxes to be active. And when clicking to any of the checkboxes to load some relevant values which will deactivate some of the checkboxes (this functionality is already done), so my issue is how to write the condional right. I am trying something like the below

<li dmx-repeat:repeat2="query_related_filters">
    <input type="checkbox" name="input1" dmx-bind:value="fts_filter_prop_fgn" dmx-bind:disabled="!arrRel.items.contains(fts_filter_prop_fgn) && arrRel.items.hasItems()" dmx-on:change="arrRel.items.contains(fts_filter_prop_fgn) ? arrRel.remove(fts_filter_prop_fgn) : arr1.addUniq(fts_filter_prop_fgn)">{{fts_filter_prop_fgn}}
</li>

which means that if the value does not exist in the array and the array has some items the disable. But this for some reason disables all the checkboxes.

Thank you!!

Try something like:

dmx-bind:disabled="arrRel.items.hasItems() ? !arrRel.items.contains(fts_filter_prop_fgn) : false"

It does the same

I don’t see this on your page?

Try this page

So what should happen on this page exactly? How to test it?