Hi everyone, I've got a repeater which display Course Modules overview elements coming from a collection called "Modules" via a dynamic dataset. Each element of this repeater displays an image, a title, a description and a button wich connects to a dynamic page with the detailed Course Module choosen. Here's what it looks like;
What I would like to do is intercepting the event of clicking on the button of each item and check in another collection (called "ModulesSelectionnes" in the code below) if the current user is allowed or not to access to the dynamic page destination. If yes, this page will be opened, if not, another page will be opened aksing the user to pay to access it.
I have then triggered the button click (that seems to overpass the dataset connection for this button) and placed this code :
import wixUsers from 'wix-users';
import wixData from 'wix-data';
export function button1_click(event) {
let currentItem = $w('#dynamicDataset').getCurrentItem();
console.log(currentItem);
let userId = wixUsers.currentUser.id;
wixData.query("ModulesSelectionnes")
.eq("title", userId)
.find()
.then( (results) => {
if(results.items.length > 0) {
let i = 0;
do {
let idModule = results.items[i].idModule;
if(idModule === currentItem.title) {
//display the authorized page
}
i = i + 1;
} while (i <= results.items.length);
//not found in the results so display the payment page
} else {
//no result from the query so display the payment page
}
This code works but the problem I have is that button1, as I triggered the click seems not connected to the dynamic dataset anymore and has the same id on every item of the repeater. So if I click on the button of the first Item or on the button of the third item I get exactly the same results and the same data from $w('#dynamicDataset').getCurrentItem() :
(Same data sent to the console log while having clicked on the 3 different buttons) :
Is someone able to help me ? Maybe I'm not using the right method ?
Thanks.
I think you only missed the association with the compiler, if I understood correctly.
$ item
export function button1_click(event, $item) { let currentItem = $item('#dynamicDataset').getCurrentItem(); console.log(currentItem); let userId = wixUsers.currentUser.id;
When you use a repeater you can not just use the same code as on your normal page.
You can read about using code for repeaters here.
https://www.wix.com/corvid/reference/$w.Repeater.html
You can see this example here that will help you understand the difference between global actions (outside the repeater) and inline actions (inside the repeater).
https://www.wix.com/corvid/forum/corvid-tips-and-updates/example-input-repeaters
Plus, if you use the search function in this forum, you can find many previous posts on how to link a repeater button to a dynamic page.