Hi all,
I have 3 pages that have 3 separate star rating inputs. I want all of the inputs to live in one dataset called #dataset1.
I’ve created a line item in my dataset for each page, each with four columns:
- Page name
- avgRating
- sumofRatings
- numofRatings
I want an onChange on the rating inputs to update columns 2-4 of only one item, corresponding to the page it was inputted on. For example, I want you to input your rating on page 1 and have it update columns 2-4 for only page 1. This is the code I currently have on each page with rating inputs. The code is currently updating the top item in the dataset, regardless of page. How do I fix this?
//RATINGS//
export function ratingsInput1_change(event) {
$w("#dataset1").onReady(() => {
// get the current item from the dataset
const currentItem = $w("#dataset1").getCurrentItem();
// get the current average rating, number of ratings, and
//total ratings for the current dataset item
const average = currentItem.avgRating;
const count = currentItem.numofRatings;
const total = currentItem.sumofRatings;
// get the new rating from the ratings input
const newRating = $w('#ratingsInput1').value;
// calculate the new average rating based on the current
//average and count
const newAverageLong = (total + newRating) / (count +1);
// Round the average rating to 1 decimal point
const newAverageShort = Number.parseFloat(newAverageLong).toFixed(1);
// set the dataset fields to the new average, total
// ratings, and number of ratings
$w('#dataset1').setFieldValues({
'avgRating': newAverageShort,
'sumofRatings': total + newRating,
'numofRatings': (count + 1)
});
// save the dataset fields to the collection
$w('#dataset1').save()
.catch((err) => {
console.log('could not save new rating');
});
});
}
Thanks so much!!