We have a client that runs a band rental business. They have a super large custom rental form we are building now and I am having an issue building some of the dropdowns.
We have a dropdown for the grade of a student and the options they can choose from is determined from which school they choose. In a database we have all of the schools and then the low grade and the highest grade. I built a for loop that seems to work with all numbers under 9 which is super odd. If anyone selects a highschool it does not populate.
export function populateGrade (parameter) {
wixData.query("Schools")
.eq('schoolName', $w("#iptSchool").value)
.find()
.then(results => {
let uniqueList = createUniqueListGrade(results.items);
$w("#iptGradeLevel").options = buildOptionsGrade(uniqueList);
})
function createUniqueListGrade (items) {
let lowGrade = items.map(item => item.lowGrade)
let highGrade = items.map(item => item.highGrade)
var gradesArray = [];
for (let i = lowGrade; i <= highGrade; i++) {
gradesArray.push(i.toString());
console.log(gradesArray);
}
return[...new Set(gradesArray)];
}
function buildOptionsGrade (uniqueList) {
let uniqueListFinal = uniqueList.map(curr => { return {label:curr, value:curr}; });
uniqueListFinal.push({"label": "Other", "value": "Other"});
return uniqueListFinal;
}
It’s obviously a frontend code as he has selectors in his code.
I’m not sure the foor loop is correct, the iterator should be a number where as yours is an array, also, the loop condition is the length of the high grades array, which doesn’t make any sense to me, and I don’t understand what you’re doing in the grades here, would you please elaborate more.
@ahmadnasriya it is a number not an array. But maybe the way I’m retrieving it makes it an array? What is should be doing is finding the dataset item and then finding all the numbers between low grade and high grade.
For example (low grade is 1 high grade is 6) it should return 1,2,3,4,5,6.
let lowGrade = items.map(item => item.lowGrade)
let highGrade = items.map(item => item.highGrade)
The lowGrade and highGrade type should be an array (object), as the map( ) method returns an array, not a number, therefore, the iterator in your loop is an array, not a number.
Try adding a console just before the loop and see what you’ll get.