Hello!
I’m creating a list of “news” for my page, and some items will connect to a PDF document and some will connect with a page. I have created two buttons that I put in a repeater - both say “view” but one is connected to the database field for URLs and one is connected to the field for PDFs. Depending on which one is used, I want the button that is not used to hide. I’ve put together this bit of code, but I can’t figure out how to make it work. Can someone help me tweak it (or completely re-write it?)
Thanks!
$w("#dynamicDataset").onReady(() => {
$w("#repeater1").forEachItem( ($w) => {
if($w("#viewPDF").link !==null){
$w("#viewPDF").hide();
}
if($w("#viewURL").link !==null){
$w("#viewURL").hide();
}
});
});
The itemData must be present and thats where the record is stored so if you will compare you must compare the itemData.fieldKey to something and then set or use hide or show. In your code you have the same conditions on both rows to check !== null which seem wrong. Always try to use the !== null or === null if you want to compare with different outcome.
$w("#myRepeater").forEachItem( ($w, itemData, index) => {
// the itemData holds the database record
if (itemData.fieldKey === "value") {
$w("#viewPDF").show();
}
} );
And also the first time the page loads it wont execute the forEachItem code for a repeater.
Then it will execute the
$w("#myRepeater").onItemReady( ($w, itemData, index) => {
});
But you can add the same code in that. So first time the repeater gets ready it will loop using onItemReady and then of you want to loop through it again when a button is clicked or something else use the forEachItem.
Hi Andreas,
I appreciate you taking the time to try to help me with this. I’m still very new to this coding.
I’ve been trying to implement your suggestion, but I must be missing something. Right now I’m not sure which elements of your code are supposed to stay that way, and which I’m supposed to change to reflect the names I’ve given my elements. I’ve tried putting your code in as is, and also tried changing things to reflect the identifiers on my page… but, not sure what I’m missing.
If it helps, I’ve also created another field in the database that is called “PDF or Link” - here I’ve just typed either “pdf” or “link” so that if I can use it somewhere in the code to just say that if it’s says “link” then show the “#viewURL” button, or if it says “pdf” then it can show the “#viewPDF” button.
I’m sure there’s lots of ways to do this, but I can’t seem to find one that works :s
Wanda
Ok… So, I have no idea why this worked… it’s a bit backwards, but seems to do the trick.
As I mentioned, I set up a field called “pdfOrLinK” and on the database I just typed in for each entry which it was. When I did the code below, the only way it would work was to say that if it says “pdf” show the URL button, and if it says “link” then show the PDF button. It’s backwards, but for some reason it works the way I want it to. Oh well, I’m happy.
import wixData from 'wix-data';
$w.onReady(function () {
$w("#dataset1").onReady(() => {
$w("#repeater1").forEachItem( ($w, dataset1, index) => {
if (dataset1.pdfOrLink !== "pdf") {
$w("#viewURL").show();
}
if(dataset1.pdfOrLink !== "link") {
$w("#viewPDF").show();
}
});
});
});