Hi I have a dataset that is used by a dynamic page set and the below code for it which works perfectly well. If there is no data in each element, it is collapsed.
However, I have a need to use it with a new dynamic page set with one difference. I need to put all teh elements referenced in the code into a repeater now whereas before they were loose on the page. I’m not sure how as to modify it to make it work as the whitespace has returned. I have VERY limited experience but always want to be enabled to learn! Help modifying the code would be very appreciated!
$w.onReady(function () {
// Assign the data set column and the matched element's id on the page
let datasetMap = {
greenBodyTitle1: "#text137",
ctaSentance: "#text135",
greenBodyTitle2: "#text152",
bodyAnswerCta1: "#text139",
greenBodyTitle3: "#text140",
bodyAnswerCta3: "#text153",
greenBodyTitle4: "#text154",
bodyAnswerCta4: "#text141",
greenBodyTitle5: "#text138",
bodyAnswerCta5: "#text155",
greenBodyTitle6: "#text142",
bodyAnswerCta6: "#text143",
greenBodyTitle7: "#text144",
bodyAnswerCta7: "#text145",
greenBodyTitle8: "#text146",
bodyAnswerCta8: "#text147",
greenBodyTitle9: "#text148",
bodyAnswerCta9: "#text149",
greenBodyTitle10: "#text150",
bodyAnswerCta10: "#text151",
};
// Get the current data set on the page
let currentItem = $w('#dataset1').getCurrentItem();
// Iterate over the map, make the item collapsed to hide it
for (var column in datasetMap) {
if (currentItem.hasOwnProperty(column) && currentItem[column]) {
// Ignore when the data item available
} else {
$w(datasetMap[column]).collapse();
}
}
});
If you want to use code to display database collection items on a repeater you can use the query() function from the wix-data API as well as the repeaters onItemReady() function.
Thanks for replying Dara. Unfortunately, it doesn’t as I’m not too versed in this. I need it to hide text elements on my repeater when the columns they are linked to in my database are empty. Could you please offer me a line or two of code that I could then copy/paste to cover all of the other elements in the repeater? That would be a MAJOR help
Thank you kindly for the reply! When I set the code as:
import wixData from 'wix-data';
function myFUNCTION() {
wixData.query("#dataset1")
.isNotEmpty("#text139")
.find()
.then( (results) => {
if(results.items.length > 0) {
let firstItem = results.items[0]; //see item below
} else {
// handle case where no matching items found
}
} )
.catch( (err) => {
let errorMsg = err;
} );
}
$w("#repeater1").data = results
to test if #text139 gets hidden or not, I get an error on the last line, the #repeater1 line - it says #repeater1 is not defined. Sadly, I don’t know how to define it! Also when I implement this for the 24 fields in the repeater, do I repeat the lines from wix.Data - >Let errorMsg for all of the fields? I’m sure you can tell my ignorance by my questions - thanks so much!