Velo Tutorial: Capturing and Displaying Ratings

Visit the Velo by Wix website to onboard and continue learning.

This tutorial shows you how you can capture and display ratings of your items from your visitors. You will set up a Ratings Display element to display the current average rating and the total number of ratings. Then using a Ratings Input element, you'll capture your visitor's rating, calculate the new average rating and total number of ratings, and then update the collection and the Ratings Display element with the new values. 

You'll need a collection with items you want to let your visitors rate and a page with a dataset connected to that collection. 

This tutorial has 3 parts:

  • How to set up your collection and your page
  • Code you can copy and paste onto your page
  • A detailed explanation of the code

Setting Up

This section covers what you need to prepare in your collection and what you need to do in your page in the editor.

In your collection

  1. Make sure your collection's permissions allow for user input.
  2. 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.

In your page

  1. Set your dataset's mode to "Read and Write." This will let your visitors update information in the collection.
  2. Add the following elements:
    1. A Ratings Display element connected to the new Number fields in your collection.
    2. A Ratings Input element your visitors can use to pick a rating for the item. You can use the default setting for the ratings, or define your own.  Do not connect it to the dataset.

The Code

This section has 2 parts. The first part shows you how to add the event handler to the Ratings Input element. The second part has the actual code that you can copy and paste onto your page.

Adding the event handler

An event handler adds code that only runs when your visitor performs a certain action. In our code, we used the "onChange" event, so that the rating is saved when our visitor makes their selection in the Ratings Input.

To add the event handler:

  1. Select your Ratings Input.

  2. In the Properties & Events panel, hover over "onChange", click the + icon, and then press Enter. 

    The code for the event handler is added to the bottom of your page code. The image below shows what it looked like on our page.

    Copy
    1
    export function ratingsInput1_change(event) {
    2
    // Add your code for this event here:
    3
    }

Adding the code

  1. 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.)
Copy
1
$w("#dataset1").onReady(() => {
2
// get the current item from the dataset
3
const currentItem = $w("#dataset1").getCurrentItem();
4
5
// get the current average rating, number of ratings, and
6
//total ratings for the current dataset item
7
const average = currentItem.avg;
8
const count = currentItem.numRatings;
9
const total = currentItem.totalRatings;
10
11
// get the new rating from the ratings input
12
const newRating = $w('#ratingsInput1').value;
13
14
// calculate the new average rating based on the current
15
//average and count
16
const newAverageLong = (total + newRating) / (count +1);
17
// Round the average rating to 1 decimal point
18
const newAverageShort = Number.parseFloat(newAverageLong).toFixed(1);
19
20
// set the dataset fields to the new average, total
21
// ratings, and number of ratings
22
$w('#dataset1').setFieldValues({
23
'avg': newAverageShort,
24
'totalRatings': total + newRating,
25
'numRatings': (count + 1)
26
});
27
28
// save the dataset fields to the collection
29
$w('#dataset1').save()
30
.catch((err) => {
31
console.log('could not save new rating');
32
});
33
});
  1. Make sure to make these substitutions.
    • '#myDataset1': the ID of your dataset
    • avg: the field ID of the field in your collection that holds the average rating
    • numRatings: the field ID of the field in your collection that holds the total number of ratings received
    • totalRatings: the field ID of the field in your collection that holds the sum of all ratings received
    • '#ratingsInput1': the ID of your Dropdown element
  2. Preview your page and test out the Ratings Input element. Watch how the ratings display element updates when you add a rating. 
  3. Go back to the Content Management System (CMS) to see how the average rating and the total number of ratings for that item have been updated in your collection.

Understanding the Code

First we read the current item from the dataset. Then we define variables called average, count and total, and set their values to be the item's current average rating, number of ratings, and the sum of all the ratings.

Copy
1
const currentItem = $w("#myDataset").getCurrentItem();
2
const average = currentItem.averageRating;
3
const count = currentItem.numRatings;

We define a variable called newRating and set its value to be the rating selected by the user in the Ratings Input.

const newRating = Number($w('#ratingsInput1').value);

Next we calculate the updated average rating and save it to a variable called newAverageLong. We then round the results to 1 decimal place and assign it to the variable newAvergeShort.

Copy
1
const newAverageLong = (total + newRating) / (count + 1);
2
const newAverageShort = Number.parseFloat(newAverageLong).toFixed(1);

Now we save the new average rating, sum of all the ratings, and total number of ratings to the current item. We include code to deal with errors should they occur.

Copy
1
$w('#dataset1').setFieldValues({
2
'avg': newAverageShort,
3
'totalRatings': total + newRating,
4
'numRatings': (count + 1)
5
});
6
7
$w('#dataset1').save()
8
.catch((err) => {
9
console.log('could not save new rating');
10
});
Was this helpful?
Yes
No