OK lets see if we can get you sorted then :-).
- You need to make sure that all drop downs have been set. There are several ways to do this but I’ll show you a simple basic way. First we will create a list of the elements that you have on the page which we will use to calculate the answer and validate that all drop downs were set.
const buildPointElements = [
$w("#value1"),
$w("#value2"),
$w("#value3"),
$w("#value4"),
$w("#value5"),
$w("#value6"),
$w("#value7"),
$w("#value8"),
$w("#value9"),
$w("#value10"),
$w("#value11"),
$w("#value12"),
$w("#value13"),
$w("#value14"),
$w("#value15"),
$w("#value16"),
$w("#value17"),
$w("#value18"),
$w("#value19"),
$w("#value20"),
$w("#value21"),
$w("#value22"),
$w("#value23"),
$w("#value24"),
$w("#value25")
];
// Initialize the counters
let totalAnswerCount = 0;
let answerTotalValue = 0;
$w.onReady(() => {
// Initialize drop down onChange handlers
initOnChangeHandlers();
});
// initOnChangeHandlers loops through the build points elements list
// creating onChange handler instances for each element
function initOnChangeHandlers() {
// Use the array forEach function to process each element
buildPointElements.forEach((element) => {
// Create onChange() event handler for the selected element
element.onChange((event) => {
// When the value of the drop down changes...
// Re calculate the answer
updateAnswer();
});
});
}
// updateAnswer loops through each element doing two things
// 1. It calculates the cumulative value of the element dropdowns.
// 2. It checks how many elements have been set
function updateAnswer() {
// Loop through the elements and checking the values
buildPointElements.forEach((element) => {
// If the dropdown has a selected item then we add its value to
// the total value and increment our answer count
if (element.selectedIndex >= 0) {
// A value has been set
// Add the drop down value to our cumulative answer
answerTotalValue += parseInt(element.value, 10);
// Increment the Answer Count - we need all buildPoint
// elements to be set
totalAnswerCount++;
}
});
// Update the answer element
$w('#answer').value = answerTotalValue;
// If we have answered all questions we can save the answer
if (totalAnswerCount === buildPointElements.length) {
$w('#submitPoints').enable();
}
}
Now its not clear if you are saving each individual drop down value into the database or not. If you are then you really don’t need to save the calculated total as well. You can calculate it from the saved drop doan values now simply by calling the updateAnswer() function in the $w.onReady() , and of course as the values are updated, like this.
$w.onReady(() => {
// Initialize drop down onChange handlers
initOnChangeHandlers();
updateAnswer();
});
If you aren’t saving the dropdown settings then you need to remove the Submit binding from your $w(‘#submitPoints’) button and create a mouse click handler
$w('#submitPoints').onClick ((event) => {
// Save the answerTotalValue in the dataset current item
// Note
$w('#dataset1').setFieldValue('answer', answerTotalValue);
$w('#dataset1').save()
.then((savedObject) => {
// if we get here the answer was saved
console.log("Answer saved saved object = "+JSON.stringify(savedObject));
})
.catch((error) => {
// If we get here then the save failed and we show the error in the console
// If this happens it might be because the dataset item hasn't been
// set due to an empty data collection. So you may need to call the dataset new() function immediately before the setFieldValue() function call above
console.log('Error! '+ (error instanceof Error ? error.message + (error['stack'] ? ' '+error.stack : '') : error));
});
});
Hope this helps or at least inspires a solution. Please NOTE: I have made best guesses at some of your element names. So this code may not work as is and may need some adjustment if the dataset element does not have the id ‘#dataset1’ or if the submitPoints button doesn’t have the id of ‘#submitPoints’ or if the data collection you are using doesn’t have an answer column called ‘answer’.
Hope this helps.