Retrieve the items data that is clicked in a repeater

So when i click the container in needs to get that containers data , but it only gets the data from the first container my goal is to create a cart, later i’ll be using a add to cart button that will change accordingly but only if i can get the basic function right.

this is the code:

 export function container1_click() {
	let userId = wixUsers.currentUser.id;
	let currentItem = $w("#dynamicDataset").getCurrentItem().title;
	//gets the title
	let toSave = {
					"title": currentItem,
					"userId": userId
				};
				wixData.save("Saveditem", toSave);
	console.log("Clicked data are: " + JSON.stringify(currentItem));
}
1 Like

Hi,
Check out this example:

export function container1_click(event, $w) {
 const idOfItem = event.context.itemId;
 let rowData;
 $w('#repeater1').data.forEach((item) => {
  if (item._id === idOfItem) {
            rowData = item;
        }
     });
    console.log(rowData); // your item
}

Good luck!
Roi.

thanks a lot. it works.

and when i use this it’s saving all the items details in the database not just the one i clicked on, others too.

but console logs just the item i clicked on.

,created this many rows with just one click

this is the code:

export function container1_click(event, $w) {
 const idOfItem = event.context.itemId;
 let rowData;
 let userId = wixUsers.currentUser.id;
 
 $w('#repeater1').data.forEach((item) => {
  if (item._id === idOfItem) {
            rowData = item;
        }
        let toSave = {
					"title": item,
					"userId": userId
				};
				wixData.save("Saveditem", toSave);
     });
    console.log(rowData); // your item
}

to get specific field

export function container1_click(event) {
	let user = wixUsers.currentUser; 
	let userId = user.id; 
	const itemId = event.context.itemId; // this is the item in the repeater assuming that the button is in the repeater.
	wixData.query("TRIPOLI") // get the item from the database collection.
	.eq("_id", itemId)
	.find()
	.then((results) => {
		let saveit = results.items[0].title; //get the title, (results.items[0]) to get every field.
		let toSave = {
					"title": saveit,
					"userId": userId // adding a field of the current user to the database in order to distinguish between other users wishlist. 
				};
				wixData.save("Saveditem", toSave);
			console.log(saveit);
	
});
}

Simple mistake in your code. This should work correctly:

export function container1_click(event, $w) {
    const idOfItem = event.context.itemId;
    let rowData;
    let userId = wixUsers.currentUser.id;

    $w('#repeater1').data.forEach((item) => {
        if (item._id === idOfItem) {
            rowData = item;
            let toSave = {
                "title": item,
                "userId": userId
            };
            wixData.save("Saveditem", toSave);
        }
    });
    console.log(rowData); // your item
}