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();
});
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 )
…problem with access to the dataset is that the $w context selector of the onClick() function refers to the repeater item and does not have access to the page context. Therefore $w does not “see” the page’s dataset. You need to change the context selector of your onClick() function, sort of like this…
As you can see, I changed the context selector to $somethingelse , and then I access the dataset with the $w global context selector: $w(#dataset1") .
Perhaps with the introduction of a new method (from August 1), there will be no problems with the repeater.
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);
});
}
Hi Steve!
I checked your code, it works!
However, it had to be corrected in two places, since my editor reported errors:
$ w (‘#repeatedElement’) …
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!
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 everyone !
I’m sorry to dig up a solved problem but this post covers exactly what I need…except your code doesn’t work properly for me.
If I use your code, when I click on the repeatedElement that acts as a button, it doesn’t update the current index as it should : it is actually updated with the previous value of newCurrentIndex.
Do any of you have an idea why ?
Thanks a lot,
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 -