I have an onClick() event handler on a repeater element. When I get this event I am setting up the selector scope using the new $w.at() method so:
$('#repeatedElement').onClick((event) => {
let scopeSelector = $w.at(event.context);
});
When I load the dataset itemData using this scopeSelector I get the expected result. However when I try to get the index of the item data in the dataset I get zero.
Is this a feature or a bug?
So:
$('#repeatedElement').onClick((event) => {
let scopeSelector = $w.at(event.context);
// This works...
let itemData = scopeSelector('#dataset1').getCurrentItem();
// This returns an index of 0 no matter which item I clicked on
// the repeater. Is this a side effect of the next at() implementation?
// Note: I get the same effect if I re-instate the scoped selector
// using (event, $w) => ...
let itemIndex = scopeSelector('#dataset1').getCurrentItemIndex();
});
Cheers
Hi Pauline !
Sorry, I didn’t answer earlier, because I read your post just today.
The getCurrentItemIndex() function returns the index of current item within the items of the dataset. The indices of the items in a dataset are zero-based. For example, if the third item is the current item, then getCurrentItemIndex() returns 2.
See -
https://www.wix.com/code/reference/wix-dataset.Dataset.html#getCurrentItemIndex
Good luck!
Hi Александр Ч:
Thanks for the feedback.
The $w('#repeatedElement') would need to be replaced by the specific element on the repeater that was being clicked so yes this would be the right thing to do.
Good catch on the $w('#dataset1') vs $w('#repeater1'). As you say the repeater is the element that knows what items it is displaying and the dataset knows what data the repeater has been given to display!
I have updated the code snippet above to reflect your catch. It should now be useable - although anyone using this will need to replace the element names shown with ones that are being used on their page.
Hi Steve!
I checked your code, it works!
However, it had to be corrected in two places, since my editor reported errors:
1) $w('#repeatedElement') ...
2) The use of the function forItems() is possible only for repeated elements to which $w('#dataset1') does not apply. I changed it to $w("#repeater1"), that in the context of the question - the same thing.
Many thanks!
Hi Steve,
thanks for your post. i will forward this question to the relevant team in Wix
Shlomi
Hi Александр Ч
Yes I was aware of the difference between global scope and repeater scope. The difference between this problem and the one you referenced is that I need to adjust the repeater item based on the clicked repeater element.
My work around for now looks like this:
$('#repeatedElement').onClick((event) => { let scopeSelector = $w.at(event.context); // This works... let itemData = scopeSelector('#dataset1').getCurrentItem(); // Reset dataset currentItem updateDataSetItemIndex(itemData); }); function updateDataSetItemIndex(item) { // Use the wix-dataset forItems() function to grab the necessary // index info based on the item $w('#repeater1').forItems([item._id], ($item, itemData, index) => { let dataset = $w('#dataset1'); // Simplify the code // The index of the selected item is a function of the // repeater page, page size and current repeater index. // We get this info from the dataset because the dataset // controls what data the repeater displays. let page = dataset.getCurrentPageIndex() - 1; // Page index starts at 1 we need zero based let pageSize = dataset.getPageSize(); // Calculate the index of the item let newCurrentIndex = (page * pageSize) + index; dataset.setCurrentItemIndex(newCurrentIndex); }); }
Perhaps others will find this helpful!
Steve
I'm not sure I can help you, but in May I had a similar problem with getting the index of the base element from the repeater. Yisrael (Wix) helped me then. He explained ( Need help: Repeater-dataset )
Perhaps with the introduction of a new method (from August 1), there will be no problems with the repeater.