I have been trying to achieve this for the past week but I am on the verge of giving up! So in need of help PLEASE! Thank you
I have a repeater on my page that I connected to a dataset by code.
The container in the repeater has three elements:
a text for the name, a text for an email, and a button.
The email and the name texts come from the dataset.
On the page, I have an empty user input element, a text box.
I added a function button in the repeater so that every time I click on the corresponding button of a container in the repeater the email of that container gets added to the text box so I can send a mass email to all the recipients in that text box.
I was successful in connecting the elements of the repeater to the dataset.
And I was also semi-successful in adding the email address from the repeater to the text box by creating an onClick function that adds the email to the text box. BUT (and here where the problem is), it is only doing so for the FIRST container in the repeater whereby if you click any of the buttons in the repeater, the value of the text box will be equal to the value of the first email of the first container.
I know this might be complicated, so THANK YOU in advance for any help or advice.
import wixData from 'wix-data';
$w.onReady(function () {
});
$w.onReady(function () {
$w("#repeater1").onItemReady( ($w, itemData, index) => {
$w("#text113").text = itemData.email;
$w("#text111").text = itemData.givenName;
$w("#text112").text = itemData.practiceAreas;
} );
wixData.query("CommunityMembers")
.find()
.then( (results) => {
$w("#repeater1").data = results.items;
} );
} );
function emailCombine () {
$w("#textBox1").value = $w("#text113").text;
}
export function button1_click(event, $w) {
emailCombine ();
}
You need to add an event handler to each button in the repeater so that when the button is clicked it gets the correct item. You're onItemReady() function should look something like this:
$w("#repeater1").onItemReady( ($w, itemData, index) => { $w("#text113").text = itemData.email; $w("#text111").text = itemData.givenName; $w("#text112").text = itemData.practiceAreas; $w("#button1").onClick((event, $w) => { $w("#textBox1").value = $w("#text113").text; }); } );
See Repeater and Repeater.onItemReady for more information.
I hope this helps,
Yisrael
Old post, but just in case, use the event action of any items integrated to a repeater :
export function whateverElement_onClick(event) { let $item = $w.at(event.context) const data = $w("#repeaterWhatever").data; let clickedItemData = data.find(item => item._id === event.context.itemId) //console.log(clickedItemData._id) == "MY165ID"}
I tried your trick but it didn't work. I tried replacing $w with $item. It didn't work either.
I could accessed to every item of the repeater only setting a handler like this in the normal funcion onClick.
the normal function is: // This only load the first item from the collection
export function button11_click(event) { console.log($w("#dataset1").getCurrentItem()); }
with the handler $w
//This choose whatever item from the colleption
export function button11_click(event,$w) { console.log($w("#dataset1").getCurrentItem()); } The trick is just put ----- $w ........ as the second parameter of the normal onClick funtion.
Hi @Yisrael (Wix)
Thank you for sharing the above code for chajj, I was able to apply a part of it for a similar task I am trying on my website. I feel like I'm ALMOST able to accomplish what I hope to do, I just need help with a couple more things and am very much hoping you can help me :)
Here is my scenario:
I have a regular page where I've built a custom form and a repeater to display dynamic content from my "Venues" database. The custom form button has been placed within the repeater. The custom form is meant to act as a "universal message" that can be sent to every venue a site visitor chooses to contact (through an on click event). I've been successful in connecting my custom form elements to my "Form" database wherein I can click a button within a particular repeater container and only that button is activated (using repeater selector type of code).
My dilemma now is figuring out how to input the venue name as a field value within my form database. Your code above has allowed me to set a user input text box element into my repeater and custom indicate that the text will show the custom name of the venue for the container in which it resides (i.e. if the input box is in the repeater container for "Venue X," then the input box will show Venue X's name). I've then connected this input box to be sent to the form database upon my button click event. All other form input elements, which reside outside of my repeater, are sent to the form database upon button click - however - my venue name (which resides within my repeater) is not being sent as well. Besides not being sent, it is also resetting after I click the button - all the venue names for every repeater is being reset (not just the name in the container I happen to be clicking on).
Do you know what I can do to also have the venue name sent to the form database, along with the other form input elements? I've been trying to figure this out for a really long time and I feel like I've ALMOST got it, I just can't figure out how to send the venue name as well. Yesrael (or anyone who has an answer), I would be tremendously grateful for any assistance, thank you!!!
Hey chajj,
There might be a case where itemData.practiceAreas is null or empty and you are receiving a warning when this happens.
It worked perfectly!!!!!!!!
I just have one more question tho. I am getting this error:
Wix Code SDK warning: the text parameter of text 112 that is passed to the text method cannot be set to null or undefined.
Not sure what that means. Text112 is displaying in preview mode the data from the dataset correctly.