Hi folks. I’m a composer (and definitely not a coder) using Wix to display my works. I created repeaters tied to datasets so I can quickly type in information about my pieces and hit a button to display them. Easy-peasy. The trouble is that I don’t have recordings for all my pieces, so the audio button isn’t needed on every repeat. So, knowing next to nothing about code, I stole off a previous post and adapted a line of code to suit my needs, only I copy-pasted it 4 times and changed the “#XYZ” stuff:
import wixData from 'wix-data';
$w.onReady(function () {
$w("#dataset1").onReady(() => {
var item = $w("#dataset1").getCurrentItem();
if (item["recording"] === undefined)
$w("#audioPlayer1").hide();
else $w("#audioPlayer1").show() ;
});
});
$w.onReady(function () {
$w("#dataset2").onReady(() => {
var item = $w("#dataset2").getCurrentItem();
if (item["recording"] === undefined)
$w("#audioPlayer2").hide();
else $w("#audioPlayer2").show() ;
});
});
$w.onReady(function () {
$w("#dataset3").onReady(() => {
var item = $w("#dataset3").getCurrentItem();
if (item["recording"] === undefined)
$w("#audioPlayer3").hide();
else $w("#audioPlayer3").show() ;
});
});
$w.onReady(function () {
$w("#dataset4").onReady(() => {
var item = $w("#dataset4").getCurrentItem();
if (item["recording"] === undefined)
$w("#audioPlayer4").hide();
else $w("#audioPlayer4").show() ;
});
});
The trouble is that, while the code works like a charm for dataset 4/the last repeater (see images), it isn’t working for the other three datasets; the code hides the buttons regardless of whether audio is attached to them or not. I’ve triple checked the datasets and attached data is all going to the right places. I’m guessing I’ve done something very silly by copy-pasting the code and am suffering the effects. Would a kindly coder be so kind as to point it out for me?
Before preview:
After preview, where Ancient Future Hundredth (in #dataset3) should be displaying an audio player, like in “Others”.
Update: I deleted all the bits of code that didn’t affect dataset 1, and it didn’t work for the repeaters associated with dataset 1. So maybe it’s not the way the code is written? Going to check my datasets yet again and make sure they’re attached properly…
I think you can use the onItemReady for the repeater…
Try this bit of code →
#repeater1 - repeater
recording - field in the database where the recording is…
$w("#repeater1").onItemReady( ($item, itemData, index) => {
if(!itemData.recording) {
$item('#audioPlayer').hide();
}
else {
$item('#audioPlayer').show();
}
});
And I dont know why you use 4 datasets for the same database…
You only need one dataset and connect the dataset to the database…
Then, connect the repeater to the Dataset…
https://support.wix.com/en/article/adding-and-setting-up-a-dataset
I have 4 different sections, hence different datasets. It was the only way I knew (at the time) how to separate out the different categories.
Where about/in place of what?
Tried the code, but no dice. My field key is “recording”, and I’ve specified which audio player, as in ‘audioPlayer#1’, but it doesn’t hide when unspecified. Perhaps that’s because the audio player defaults to a Wix placeholder track?
Thanks, by the by!
Update 2:
Found some other code to try, but with the same results of hiding all the audio players regardless of their connection to datasets.
$w.onReady(() => {
$w("#repeater2").onItemReady( ($w, itemData, index) => {
const item = $w("#dataset2").getCurrentItem();
if (!item.recording) {
$w("#audioPlayer2").hide();
}
});
});
$w.onReady(() => {
$w("#repeater3").onItemReady( ($w, itemData, index) => {
const item = $w("#dataset3").getCurrentItem();
if (!item.recording) {
$w("#audioPlayer3").hide();
}
});
});
$w.onReady(() => {
$w("#repeater1").onItemReady( ($w, itemData, index) => {
const item = $w("#dataset1").getCurrentItem();
if (!item.recording) {
$w("#audioPlayer1").hide();
}
});
});
Update 3:
(I’ll leave it alone after this one, I promise).
There must be something wrong with datasets 1-3. I tested the following code with repeater #4, and it works:
$w.onReady(() => {
$w("#repeater4").onItemReady( ($w, itemData, index) => {
const item = $w("#dataset4").getCurrentItem();
if (!item.recording) {
$w("#audioPlayer4").hide();
}
});
});
But why just this dataset/repeater? It’s almost identical to the others except that the others have a scoreSample field and a button that it’s attached to. Maybe I’ll try deleting said button? I dunno. Coding hurts.
First of all, I forget about you using 3 repeaters and 3 databases !!!
Sorry…
Try this code and see if it works !!!
$w.onReady(function () {
$w("#repeater2").onItemReady( ($item, itemData, index) => {
if(!itemData.recording) {
$item('#audioPlayer2').hide();
}
else {
$item('#audioPlayer2').show();
$item('#audioPlayer2').src = itemData.recording;
}
});
$w("#repeater3").onItemReady( ($item, itemData, index) => {
if(!itemData.recording) {
$item('#audioPlayer3').hide();
}
else {
$item('#audioPlayer3').show();
$item('#audioPlayer3').src = itemData.recording;
}
});
$w("#repeater4").onItemReady( ($item, itemData, index) => {
if(!itemData.recording) {
$item('#audioPlayer4').hide();
}
else {
$item('#audioPlayer4').show();
$item('#audioPlayer4').src = itemData.recording;
}
});
});
MAKE SURE THAT your repeaters are connected to the dataset …
IT WORKS! Turns out it was my copy pasting nonsense that was doing me in. For whatever reason, having all the repeater coding under one function works, but repeating that first line that sets up a function doesn’t work. I took the above code and added a fourth set for repeater1 and audioPlayer1 and now it’s good to go! THANK YOU!