Hi there,
I am currently having the issue with the read write database displaying the first entry on a submission form that I am trying to use to collect and tabulate average rating/review. On top of that, I realised when I switch it to write only, the code on the page that is supposed to tabulate the ratings will not work at all (no entry for average rating or number of rating +1 after form submission)
I been researching and came across someone mentioning it might something to do with backend .jsw that might help but I have no idea how to do this.
I am using the codes from here: https://support.wix.com/en/article/corvid-tutorial-capturing-and-displaying-ratings and have set all my elements to the exact/proper ID stated in the example.
Can anyone please help me?
The tutorial that you are using is only using code in the frontend on the page that you are using it.
It does not use any backend file so in theory there should be no issues with any js or jsw backend files with this tutorial.
The code is simply as this on your page which you simply paste underneath the export function for your ratingsInput_change event as stated in the tutorial.
Copy the code below and paste it into the event handler function above the line that says “//Add your code for this event here:”. (You can delete that line if you want.)
$w.onReady(function() {
export function ratingsInput1_change(event){
$w("#dataset1").onReady(() => {
// get the current item from the dataset
constcurrentItem = $w("#dataset1").getCurrentItem();
// get the current average rating, number of ratings, and
//total ratings for the current dataset item
constaverage = currentItem.avg;
constcount = currentItem.numRatings;
consttotal = currentItem.totalRatings;
// get the new rating from the ratings input
constnewRating = $w('#ratingsInput1').value;
// calculate the new average rating based on the current
//average and count
constnewAverageLong = (total + newRating) / (count +1);
// Round the average rating to 1 decimal point
constnewAverageShort = 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');
});
});
});
Plus, make sure that you have setup the database for this as stated in the tutorial too with the correct field types.
In your collection
-
Make sure your collection’s permissions allow for user input.
-
Add 3 new Number fields, for the average rating, number of ratings submitted, and sum of all the ratings submitted. You can leave these fields blank or input a starting values. Remember that the average rating must be between 1 and 5.
Hi GOS,
Thanks for your help but here is the current issue:
I have 2 separate pages; 1 for Writing a Review (user to enter rating) and 1 dynamic page(s) for displaying corresponding reviews.
I will need to insert your code for the Write Review page yes? But if i set the dataset mode on this page to be ‘Write Only’. There will not be any entry in my Average Rating, Number of Rating and Sum of all Ratings columns in the dataset.
If I set the mode to Read & Write, it will display previous existing entry in the user-input fields boxes such as service selection, name, mobile, ratings etc.
On a separate note, for my Dynamic Page(s) used to display the reviews on a repeater (#repeater1), If I want it to collapse it if there are no entry for the current service, how do I code it?