Hi!
I want to have several checkboxes that work together that is they show the result that is a combination of them. So I have a category for clothes, size and colour and according to selection they will show the ones that fit exactly the categories I have selected.
I haven’t been able to reach this with the code I found. When I hit preview it doesn’t even let me select anything.
Do I need to create another dataset?
Can you provide some code?
First, you could copy and paste the code here, so we can take a look.
It is quite feasible, I guess you need a filter with checkboxes…
Thanks! So below the code I have used. I have managed to make it work with one checkbox. My issue is that I need at least twoccheckboxes working independently but also together , and it does not work with just copying this code once again.
P.S.: When I say together I mean from the same Dataset
import wixData from 'wix-data';
const databaseName = 'Items1';
const databaseField = 'instrument';
$w.onReady(function () {
$w('#checkboxGroup1').onChange((event) => {
const selectedBox = $w('#checkboxGroup1').value;
addItemstoRepeater(selectedBox);
})
});
function addItemstoRepeater(selectedOption = []) {
let dataQuery = wixData.query(databaseName);
if (selectedOption.length > 0) {
dataQuery = dataQuery.hasSome(databaseField, selectedOption);
}
dataQuery
.find()
.then(results => {
const filtereditemsReady = results.items;
$w('#listRepeater').data = filtereditemsReady;
})
}
Try this one…
import wixData from 'wix-data';
const databaseName = 'Items1';
const databaseField = 'instrument';
$w.onReady(function () {
$w('#checkboxGroup1').onChange((event) => {
let myValues = $w("#checkboxGroup1").value
addItemstoRepeater(myValues);
})
});
function addItemstoRepeater(myValues) {
let dataQuery = wixData.query(databaseName);
if (myValues.length > 0) {dataQuery = dataQuery.hasSome(databaseField, myValues);}
dataQuery.find().then(results => {$w('#listRepeater').data = results.items;})
}
Why your code did not work? → Because searching for VALUES in an empty ARRAY do not make any sense.
@russian-dima Thanks! So I made one checkbox work, but not two and definitely not together.
Shall I copy this code twice and change the name of the field and checkbox?
@musicforyourlessons
You will need to use a checkbox-group instead of using several single ones.
And if you want to combine VALUES of several CheckboxGroups, you will first have to generate/fill your ARRAY with all selected values, and then push it to your function.