Hi everyone,
I am creating a dosha quiz, basically, it is a set of 33 questions with single choice answers. So I did that using radio buttons and its working fine. I’m population the dataset and its working. Each Radio button value has 3 possible values (vata, pitta, kapha). this is what you see in the dataset
Now I need to get a total count of each value, how can I count the answers in each row to be able to know how many times the vatta was selected, the pitta and the kapha individually?
I tried query but i dont even know if this is possible, if i can only search in individual rows.
Thank you,
The first example below uses a query. The second uses getCurrentItem() on the dataset and then similarly loops through the object with the for-in loop to get the count for potential answer.
wixData.query("dosha")
.find()
.then((results) => {
let pittaCount = 0, kapchaCount = 0, vataCount = 0;
console.log(results);
results.items.forEach((row) => {
pittaCount = 0, kapchaCount = 0, vataCount = 0;
for (var field in row) {
if (row[field] === "pitta"){
pittaCount++;
}
if (row[field] === "kapcha"){
kapchaCount++;
}
if (row[field] === "vata"){
vataCount++;
}
}
console.log("pittaCount: "+ pittaCount.toString());
console.log("kapchaCount: " + kapchaCount.toString());
console.log("vataCount: " + vataCount.toString());
})
})
let pittaCount = 0, kapchaCount = 0, vataCount = 0;
let row = $w("#dataset1").getCurrentItem();
pittaCount = 0, kapchaCount = 0, vataCount = 0;
for (var field in row) {
if (row[field] === "pitta"){
pittaCount++;
}
if (row[field] === "kapcha"){
kapchaCount++;
}
if (row[field] === "vata"){
vataCount++;
}
}
console.log("pittaCount: " + pittaCount.toString());
console.log("kapchaCount: " + kapchaCount.toString());
console.log("vataCount: " + vataCount.toString());
Thank you for your reply…
I’ve tried both codes and it couts but i cant get it to be saved on the dataset
i have an automation that sends an email and i need the totals to be added.
Also, how should it be done? I should give the save command and then the query? Or should I do the query before saving? and, can both actions be done under the same event? like onClick and un that that event have save and then query.
thank you,