I have a repeater that is bound to a dataset and displays items that have a price assigned to them. I want to format each price with thousands separators but when I try to do that I am only able to select the price item in the first item of the repeater.
I am using this code to try and console.log all prices:
$w.onReady(() => {
$w('#repeater1').forEachItem( ($w, itemData, index) => {
console.log($w('#text78').text);
} );
});
However, I get 5500 printed twice when I run the code, and other 20 item prices are not printed at all. What am I doing wrong?
Hi,
My guess is that forEachItem method runs before the dataset is ready.
Change forEachItem to onItemReady.
Roi .
Hey Roi,
tried that but it seems to freeze Wix totally. I am guessing the onItemReady event gets triggered way too much.
Not sure what other event to use, I checked the dataset and the repeater object but no appropriate events are available.
Hi Igor,
As Roi pointed out, the dataset might not yet be ready. You can try putting the forEachItem loop inside of the dataset’s onReady() function:
$w("#dataset").onReady( () => {
$w('#repeater1').forEachItem( ($w, itemData, index) => {
console.log($w('#text78').text);
} );
} );
The other approach would be to get the field as the repeater is being created. You can use the repeater’s onItemReady() function for that. Follow the code examples in the API for how to do this.
Yisrael
Thanks a bunch Yisrael,
for future reference, here is how I solved my problem:
$w.onReady(() => {
$w("#villaDataset").onReady(() => {
$w('#repeater1').forEachItem( ($w, itemData, index) => {
const numberWithCommas = (x) => {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
}
$w('#text76').text = numberWithCommas(parseInt($w('#text76').text));
} );
});
});
Hi Igor and @yisrael-wix , that worked for me as well! But only on the first page of the repeater.
Do you know how this code can work on multiple pages in a repeater?
It seems like pagination is loading data before that code can format it. Any idea how would I fix that?