Dynamically create checkboxes and check based on query

I know there are a million threads about this, but sifting through most of them I haven’t been able to find an answer.

This tutorial does a great job of explaining how to dynamically check checkboxes. Which is the function I’m really after here. However, the number of checkboxes are based on a db query.

I can easily generate a bunch of checkboxes based on a query using a repeat children. But that won’t allow me to use the nice feature of dynamically checking them as we can do the multi checkbox form data as explained in the tutorial .

I cheekily tried to place a repeat children inside of the form group :face_with_hand_over_mouth: which wasn’t allowed.

Any ideas on how to generate a bunch of checkboxes based on a DB query (as you do with a repeat children) but dynamically check them based on if the checkbox’s value exist in a table?

I tried using a dynamic attribute “checked” on the checkbox input itself, but couldn’t figure out how to check if the value existed in a given table. After having tried every single possible combination of data formats on the data binding I was about to flip my desk but found the tutorial linked above.

Perhaps the correct answer here is to NOT bother with the form and use a repeat children as I currently am, but figure out how to use the dynamic attribute if value exist in a given table?

Add a flex box or container and make that repeat children. Place the checkbox to be repeated inside it.

haha thanks and sorry @bpj I was writing my second post above there while you were replying.

For whether it is checked, use a condition that evaluates to true for it to be checked, usually if the checkbox value matches the data field

E.g.
dmx-bind:checked="value == somedata "

Yes, therein lies my problem.

I have 2 tables.

Table 1: talent_tags

This is what’s used to dynamically create the list of checkboxes.

Screenshot 2023-07-21 at 9.04.39 AM

Table 2: tags

I need the corresponding checkbox in the second image to be checked if its value exists in table 2.

To test that the values and everything is correct. I did a simple condition

(value == conn_layout_single_talent.data.query.tags[0].talent_tag_id)

Which will tick the first box “Rockstar” which is has the value of 7. Splendid, so everything seems to be set up correctly as far as getting the basics going. The condition used here will of course only return the first entry in the table.

And this is where I get stuck, I can’t figure out how to compare it against the full list from Table 2. I’ve tried returning a full list using “values” conn_layout_single_talent.data.query.tags.values(talent_tag_id) and a condition checking if that returned list contains the value of the checkbox, and a myriad of other weird combinations in my desperate attempts. But embarrassingly enough I can’t figure out the logic of it.

Try just:
value == talent_tag_id
It’s in a repeat so you just need to point to the data field in that repeat

Unfortunately I don’t think that will work, since the repeat is using Table 1 as data source, in generating the dynamic list of checkboxes.

So it will always return TRUE, since the checkbox’s value is of course the repeated talent_tag_id.

It’s basically just saying “This checkbox with a value of 7 == the value of the data that generated it, which is 7” right? So it ends up comparing against itself.

conn_layout_single_talent as referenced in the snippet you quoted is returning table 2.

Ok , I think I get you,
you could try this:

conn_layout_single_talent.data.query.tags.where('user_id',user_id,'==').where('talent_tag_id',value,'==').hasItems()

(you might not ned the where formatter for user_id if you have already filtered for this in the server action)

1 Like

It looks like a similar problem I had with select dmx-bind:disabled and here is @Teodor’s solution

Try:
dmx-bind:checked= "conn_layout_single_talent.data.query.tags.values(talent_tag_id).contains(talent_tag_id)"

*I’m not sure if conn_layout_single_talent.data.query.tags query is the right but you’ll understand it

1 Like

Unbelievable. I had tried that before and it didn’t work, tried it again and seeing your example, it turns out that my script had simply somehow got messed up, and looked like this.

conn_layout_single_talent.data.query.tags.values(talent_tag_id.contains(talent_tag_id))

I’m guessing I must have accidentally applied the “contains” to the property itself? Like this;

instead of your formatting which is the equivalent of this

conn_layout_single_talent.data.query.tags.values(talent_tag_id).contains(talent_tag_id)

At first I wasn’t able to reproduce the first image. Turns out that if you create the “values” and then right click #property, it will give you the contains option. But if you apply it correctly to begin with, like the second image, you’re no longer given the contains option when you right-click on #property.

I’m sure that makes perfect sense… somehow, but it sure befuddled me :smile:

Thank you so much @famousmag and @bpj for all your help with this!

1 Like

Is this finally working? Checked?

Sure is! thank you!

1 Like

Glad it works!!
The Formatter panel is a bit tricky… You have to check after assigning the values that the result is the desired one