I have a ratings input element(ratingsInput1) on my blog posts page. For each blog I want to capture a rating. How do I link the blog post ID to my ratings #Dataset1.
Here’s my code.
$w.onReady( () => {
$w(“#dataset1”).onReady( () => {
let num= $w(“#dataset1”).getCurrentItemIndex();
console.log(“OnReady---------- Done setting Ratings current item " + num + " .”);
} );
$w("#dataset1").setCurrentItemIndex(3)
.then( () => {
console.log("OnReady---------- Done setting Ratings current item " + 3 + " .");
} );
} );
export function ratingsInput1_change_1(event) {
$w(“#dataset1”).onReady(() => {
// get the current item from the dataset
const currentItem = $w(“#dataset1”).getCurrentItem();
let num=5;
$w(“#dataset1”).setCurrentItemIndex(num);
const currentItemidx = $w(“#dataset1”).getCurrentItemIndex();
console.log(“==========On change Current Item index = " + currentItemidx + " .”);
// get the current average rating, number of ratings, and
//total ratings for the current dataset item
const average = currentItem.avg;
const count = currentItem.numRatings;
const total = currentItem.totalRatings;
// 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({
‘avg’: newAverageShort,
‘totalRatings’: total + newRating,
‘numRatings’: (count + 1)
});
// save the dataset fields to the collection
$w(‘#dataset1’).save()
. catch ((err) => {
console.log(‘could not save new rating’);
});
});
}