Need some help with dynamically checked checkboxes that are used to filter a database query.
The checkbox form group list used for filtering the database is a dynamic list, which is populated by a server action and a repeat on the form’s Checkbox Control element. That works fine, also the filtering works well when a checkbox is clicked. So far so good
Now, I want to dynamically check some of the checkboxes if their value is set in a query parameter. Following this doc that should be possible by setting the value of the checkbox group to query.[parameter name] in the checkbox group properties, like I did:
The parameter was also added to the content pages’ query parameters by the way as well as on the layout page (I’m on NodeJS).
When I output the value of the checkbox group however, it’s empty. That is, it is empty on page load with a query parameter set, but it is populated with value when checkboxes are clicked of course. So on page load the checkbox group value is always empty, therefore the checkboxes are also not automatically checked and the database is not filtered by this value either.
I think it probably has something to do with the checkbox group value being an array instead of a single value. Should the query.cat value (if only one) be transformed to another format possibly? Or if not, what else could be wrong here?
Did some more testing and it seems setting the value for the checkbox group settings doesn’t have any effect at all, whether it’s set to be equal to a query parameter or a static number, like I tried here:
The output (for cat_filter.cat_id.value) is empty then, always.
Except when a checkbox is clicked, then the value array is filled with one or more values.
Your checkboxes need values. They will be “checked” when the checkbox group value matches their values!
Your checkboxes have NO values at the moment:
The checkboxes are used for dynamic filtering and the repeat data is a list of categories from the database. All works fine.
Now I want to set a value to the checkbox group properties to automatically/dynamically check boxes, but that doesn’t as described in your tutorial doc work. What am I doing wrong then? Should I add static values to it as well?
I understand the static example and get now that the problem is that an array has to be set as the input for the checkbox group properties. The static one works and checks the boxes automatically.
I am sorry to say I still can’t get the dynamic repeat checkbox form to work the same way. Here’s the code (in proper code tags):
So a (for testing purposes static) array is set now with values 1 and 3 in it. In the repeated form there are checkbox included where dmx-bind:value="id" equal 1 and 3. So I would expect those boxes to be checked, but they are not.
The issue seems to be that the values of the checkboxes are strings (as all other inputs) and if your ids are numbers, they don’t match. So 1 is no the same as '1'
You can add this to your checkbox group value and it should work:
I am sorry to say that this also still doesn’t make it work unfortunately…
Just to be sure I tried changing the query type to string, number and even array (should be of type Text, right?) and tested again. Also tested with url queries in these formats: ?cat=1,3, ?cat=[1,3] and even ?cat=1
With cat query being a string type ?cat=1,3 would be the correct query format in the url, right?
Then the split function creates the array of the comma separated values and .map('$value.toString()')
should convert every single value to a string, am I correct?
Output of query.cat.split(',').map('$value.toString()') on the page returns 1,3 so that seems okay.
However, the checkboxes are still not checked…
As a last guess I even tried if it works if the checkboxes’ ids are converted to strings, since these are numbers that don’t match the string type of the checkbox group input: <input class="form-check-input" type="checkbox" id="input_category" name="inp_category" dmx-bind:value="id.toString()">
This also didn’t resolve the issue.
Any ideas left what could be wrong here? Thanks again for your effort and help!
I am facing similar issue. I am trying to generate checkboxes dynamically and trying to set the checkbox group value. I am using API Action for value binding. Seems this is not working me.
What if I get the result ?offertype=1&offertype=2&offertype=3? How to correct it so that will be correctly in the format like ?offertype=1,2,3 name="offertype[]" doesn't work