Help for hiding/showing button linked to referenced dataset [Problem solved]

Hi, I’m searching for a way to hide a button in a repeater when there’s no data for the specific container in the referenced dataset.

A little bit more explanation: I have a page with a main dataset (dataset1). To this dataset a repeater (repeater1) and it’s objects are linked. On the same page I have a second dataset (dataset2) who is filtered with a reference field to dataset1 (field xxx same as dataset1).
I now want a button in my repeater (for every container) to only be shown if there’s data to be found in the referenced dataset 2 for the data of that container.
Hope you understand what I mean…

Until now I have this, but it only works for the first container in the repeater even if there’s a forEachItem in the code:

$w.onReady( function () {
$w(“#dataset2”).onReady( () => {
$w(“#repeater1”).forEachItem( ($item, itemData, index) => {
let count = $w(‘#dataset2’).getTotalCount()
if (count === 0) {
$w(‘#buttonAntwToev’).show()
$w(‘#buttonAntwoorden’).hide()
} else {
$w(‘#buttonAntwToev’).hide()
$w(‘#buttonAntwoorden’).show()
}
})
})
})

Thanks for your help

This line of code $w(’ #dataset2 ').getTotalCount() applies to the whole database and I don’t think it’s a good idea to run it inside the forEachItem method.

You use one of these conditions; if (itemData) or if(index >= 0) instead of if (count === 0) to check for the presence of referenced data item.

Problem solved

This is the code if ever someone needs it:

$w.onReady(() => {
$w(‘#dataset2’).onReady(() => {
$w(‘#repeater1’).onItemReady(($w, itemData) => {
let count = $w(‘#dataset2’).getTotalCount()
if (count === 0) {
$w(‘#buttonToev’).show()
$w(‘#buttonAntw’).hide()
} else {
$w(‘#buttonToev’).hide()
$w(‘#buttonAntw’).show()
}
})
})
})