top of page

Forum Comments

How to create a checkbox user input filter?
In Coding with Velo
Pav Slyzyak
Jun 28, 2020
Hi Ahmad! Firstly thank you for your help! I am working with a group checkbox rather than individual ones as there is a lot of values to enter, will this still be working the same or will this be a tad different? Are you able to just explain on a few things at all? So i've added the code to the page and tried to make it work, but getting a bit stuck on certain parts. From the first code snippet it keeps coming up with an error that a checkbox is not a valid sector but on the page the group checkbox is called AlcoholCheckboxGroup. I tried to search it up but the results were just saying that the element is not on the page so it cant be referenced. The same issue on the second part of the code along with a slight confusion on what string to attach to it to make it work. Please see code snippet below: async function AlcoholFilter() { // Check which checkbox is checked and add the checked ones into an array let checkedBoxes = []; for (let i = 0; i < numberOfCheckBoxes; i++) { if ($w('#AlcoholCheckboxGroup${String(i+1)}').checked) { let item = { id: '#AlcoholCheckboxGroup${String(i+1)}', boxIndex: i } checkedBoxes.push(item); } } // Now set set the filter let totalChecked = checkedBoxes.length; if (totalChecked > 0) { let filter; switch (totalChecked) { case 1: let firstBox = { id: checkedBoxes[0].id, index: checkedBoxes[0].boxIndex } filter = wixData.filter( .eq(`field${firstBox.index}`, true) ) break; case 2: filter = wixData.filter( .eq(`field${checkedBoxes[0].boxIndex}`, true) .eq(`field${checkedBoxes[1].boxIndex}`, true) ) break; case 3: filter = wixData.filter( .eq(`field${checkedBoxes[0].boxIndex}`, true) .eq(`field${checkedBoxes[1].boxIndex}`, true) .eq(`field${checkedBoxes[2].boxIndex}`, true) } break; /* You can scale to as much as you want and add as much cases as you want */ } $w('#dataset1').setFilter(filter); } else { // Reset the filter if no checkbox is checked $w('#dataset1').setFilter(wixData.filter()); } } On the case 1 the .eq the error code keeps coming up as unexpected token. Hope to hear from you soon!
Content media

Pav Slyzyak

More actions
bottom of page