Hello, I have been working with the WIX code tutorial: Capture and Display ratings. I need to rate the page where I propose my affiliate program. I got stock because I don't get if I need 2 DB collections with 2 DataSets and how I connect them with the rating display and the rating input. I have search for videos but didn't find one that works for me. Maybe there is someone who has already set up both ratings (display and input)? thanks !
top of page
Important forum update
This forum is migrating to one unified Wix community forum starting July 26th, and will be read-only during the process.
Wishlist Page is the official platform for requesting new features. You can vote, comment, and track the status of the requested features.
bottom of page
Here is a very simple example of the RatingsInput and RatingsDisplay components:
View the Live demo in a browser page
Open the Example template in the Wix Editor
Note: The RatingsInput component is on the RECIPE Lightbox page.
how does it work for modalyst dropshipping products that have no productID ? I tried replacing productID with InventoryItem which Modalyst provides but doesn't work at all.
Having similar issues. I have a display and a user input ratings system. Not sure if the Code is right but the dataset/collection is there. Difference is i have and want 3 ratings on same page. So i have 3 displays and 3 inputs. Please help?
Here is my code:
$w.onReady(function () {
//TODO: write your page related code here...
});
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.averagerating;
const count = currentItem.number;
const total = currentItem.sum;
// get the new rating from the ratings input
const newRating = $w('#ratingsInput3').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({
'averagerating': newAverageShort,
'sum': total + newRating,
'number': (count + 1)
});
// save the dataset fields to the collection
$w('#dataset1').save()
.catch((err) => {
console.log('could not save new rating');
});
});
}
Now I get parsing error: unexpected token?
export function ratingsInput1_change(event) { let currentItem=$w('#dataset4').new(); // get the current average rating, number of ratings, and //total ratings for the current dataset item const average = currentItem. averageRating; const count = currentItem. ratingsSubmited; const total = currentItem. ratingsSum; // 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('#dataset4').setFieldValues({ 'averageRating': newAverageShort, 'ratingsSum': total + newRating, 'ratingsSubmited': (count + 1) }); // save the dataset fields to the collection $w('#dataset4').save() .catch((err) => { console.log('could not save new rating'); }); }); }
@Yisrael (Wix) When I try the Rating Input, i can hover over the options and select one. However, the Rating Display stays at default. In the page code I get a message "script error". When I return to the data base collection there is no input. Now just to clarify, I have created:
- 1 data base collection with 3 number fields; average, count, total
- 1 data set connects to the collection above; mode read & write (number of items to display 20)
- 1 Rating Display connects to the dataset (rating value connects to: count - Number of ratings connects to total)
- 1 Rating Input with 5 ratings : Now here is the thing that I found strange, in the tutorial https://support.wix.com/en/article/wix-code-tutorial-capturing-and-displaying-ratings it reads: 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.
So maybe I'm missing one element or missed to connect the Rating Input? My webpage is: https://www.finlandweeks.com/affiliate-program
Voilà. Looking to your comments, tks
@Yisrael (Wix) yes of course, I have followed the WIX code tutorial and make the changes accordingly to point 2 of the tutorial:
2. Make sure to make these substitutions:
- '#myDataset1': the ID of your dataset
- avg: the field key of the field in your collection that holds the average rating
- numRatings: the field key of the field in your collection that holds the total number of ratings received
- totalRatings: the field key of the field in your collection that holds the sum of all ratings received'
#ratingsInput1': the ID of your Dropdown element
These substitutions have been made:
- Dataset name: #dataset4 (connected to Data Base collection, Mode: Read & Write)
- avg: field key = averageRating
- numRating field key = ratingsSubmited
- totalRatings field key = ratingsSum
One thing that I'm wondering is: Is it necessary to add some numbers in the const newAverageLong ?
// calculate the new average rating based on the current
//average and count
const newAverageLong = (total + newRating) / (count +1);
I have added my code so maybe someone can check it and mention where it is wrong:
Export function ratingsInput1_change(event) {
//Add your code for this event here:
$w("#dataset4").onReady(() => {
// get the current item from the dataset
const currentItem = $w("#dataset4").getCurrentItem();
// get the current average rating, number of ratings, and
//total ratings for the current dataset item
const average = currentItem.avgRating;
const count = currentItem.ratingsSubmited;
const total = currentItem.ratingsSum;
// 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('#dataset4').setFieldValues({
'avgRating': newAverageShort,
'ratingsSubmited': total + newRating,
'ratingsSum': (count + 1)
});
// save the dataset fields to the collection
$w('#dataset4').save()
.catch((err) => {
console.log('could not save new rating');
});
});
}