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?
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,
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
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,
My guess is that forEachItem method runs before the dataset is ready.
Change forEachItem to onItemReady.
Roi.