Hi, I am creating a film review blog website for a university module. A key feature of the site is the ability for users to add a star rating for each film (and for the average to be shown).
I have been following the Velo tutorial > https://support.wix.com/en/article/velo-tutorial-capturing-and-displaying-ratings < and it’s working! (Yay). I am a complete coding beginner, so this in itself is a huge achievement for me. However, the elements are the same for each dynamic page and so essentially there’s only one rating - but I need an individual rating for each page.
I don’t know if I can implement the store tutorial code successfully (as I am unsure what elements are relevant to me). Is it possible to add a qualifyer of some sort to the base code that would place the data collected from the ratings input into a specific item line in the collection? e.g. if the item was named the page ID and that related to the page the rating was submitted on - if that makes sense?
So instead of working with a whole new code I could just modify sections of the below?
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.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');
});
});
}
(Don’t know if this is how it works - again very beginner here!) But, for example, instead of “const currentItem = $w(”#dataset1").getCurrentItem();" could it be “get variable item, dependant on page ID”?
I don’t know how to write that in code, but if that is possible please could someone help me out?
Thanks so much!
Congratulations on working on your film review blog website! It’s great to hear that you’ve made progress with implementing star ratings using the Velo tutorial.
To make the star ratings unique for each film (dynamic page) on your website, you can indeed modify the code to store the ratings in a specific item line in your collection based on the page ID. Here’s a general approach to achieve this:
-
First, make sure you have a collection set up to store the film data. This collection should include fields for the film’s details, such as title, description, and the average rating.
-
In your code, when a user submits a rating for a film, you can capture the page ID (or any other identifier that uniquely identifies the film) and the rating value.
-
Modify the code to search for the item in your collection that corresponds to the page ID of the film being rated.
-
Once you have identified the specific item in the collection, update the rating field for that item with the new rating value.
-
After updating the rating, you can recalculate the average rating for the film and update the average rating field in the collection accordingly.
By implementing these modifications, you’ll be able to store and display individual ratings for each film on their respective dynamic pages.
While I can’t provide specific code modifications without knowing your current implementation, you can refer to the Velo documentation ( https://www.wix.com/velo ) and the Wix Corvid Forum ( https://www.wix.com/velo/forum ) for more detailed guidance. Additionally, the Wix Support team can assist you with specific technical questions related to your Wix website.
Good luck with your film review blog, and feel free to ask if you have any further questions!
TellPopeyes Survey
have you worked out a sollution?
I am interested in a same thing for my dynamic pages.
Hey,
One important thing in setting the code up above is to make sure you follow the guide the OP posted. You want to make sure that any ratings you capture for each user are collected into a separate collection, so that my rating doesn’t override your rating when we submit per dynamic page.
Another approach is to store all the ratings in a separate database, and then use the aggregate - Velo API Reference - Wix.com functionality to calculate the averages, total and number of ratings only when the page loads, rather than recalculating for every (movie/song/info in your DB.). It protects the DB from bad re-writes.
If you’re taking your dynamic pages beyond just ratings, also check out the Comments - Velo API Reference - Wix.com reference for how to create comments that are tied to that dynamic page.
I have zero knowledge in writing code.
So i have now set up a ratings display and input.
It is connected too a new Collection and a new dataset.
In the Collection I have fields: avg, numRatings, totalRatings, and reference field with link to dynamic pages Collection (properties) that i input manually in collection. The dataset set has filter: Field=Reference (properties), condition=same as (properties item).
This actually works, ratings display will show numbers coresponding to their item.
In editor while testing the code all works flawlessly. must test on published page, don’t have time now so in couple of hours will publish test page ands check. would like some help to check it.
EUREKA guys.
Capturing and Displaying Ratings on dynamic pages is solved.
To capture and display ratings follow this Velo tutorial
You dynamic page is stored in one collection, ratings in another with fields as in guide, add another field type being reference with Referenced Collection to one of dynamic pages.
For dynamic page you must manually input connection in reference field. Collection must have permision for user input, anyone can view, add, update.
Next on your page create new dataset connected to ratings collection, mode is read&write, add filter in dataset settings: Field - Reference (Properties), Condition - Same as, Dataset - Properties item.
That is all. Visitors can click on rating input, it gets collected in ratings collection for that dynamic page, and as per code average is calculated and shown as well as total user input number.
So glad i did something.