Now despite the fact that the items are ordered in the reversed order as expected, their index inside the onItemReady() function is of the original data (data1).
So the first item to display (id: b) has index 1 while the last item (id: a) has index 0.
Let’s continue:
data2.pop();
$w("#repeater1").data = data2;
Now there’s only 1 item in the repeater (as expected) but… its index === 1 and there’s no item with index 0 at all!
a problem…
Is it a bug? an an annoying feature? Am I doing something wrong?
Please post the editor URL of your site. Only authorized Wix personnel can get access to your site in the editor.Please include the name of the page involved.
Page name: repeater (which is the homepage).
The question is:
Why doesn’t the index of the repeater items change when I change the source of the repeater data.
The callback is triggered when you add new items by setting the data property. It is not triggered for existing items that are updated when you set the data property.
What you have here is that you’re updating existing items, so the onItemReady() function isn’t being triggered again. You need to force it. What you can do is clear the data, and then set it again. Your onClick() function then would look like this:
$w("#button1").onClick((event) => {
$w("#repeater1").data = []; // force onItemReady() to run
if (option === 1) {
$w("#repeater1").data = data2;
option = 2;
} else {
$w("#repeater1").data = data1;
option = 1;
}
})
I commented the additional line of code that I added. This additional line of code clears the repeater data so that when you set it again, the items aren’t “updated”, rather they are “new”.
You can let go now. Or perhaps you might need to hold on for just a few moments more.
@yisrael-wix , I see. Thank you.
This solution is problematic because I want to have my cake and eat it too :
In my real page (not the simple example I linked earlier),
I have a user input field inside the repeater, and I want to keep the input inside an existing item even when I change the order (and that won’t work with your solution)
So, I guess I’ll have to keep the input aside and push it to the relevant item after updating the data source.
Well, if you insist on a certain caloric intake, you could use the forEachItem() function. This way you can keep your repeater items intact. However, I’m not sure how that affects the order.