Hide element if database is empty - doesn't reappear when I fill database.

I want a text box and a button to hide if the database is empty. Currently, the elements dissapear when I use this code:

$w.onReady(() => {
$w(“#dataset1”).onReady(() => {
const item = $w(“#dataset1”).getCurrentItem();
if (!item.button1) if (!item.text18) {
$w(“#button1”).collapse(); $w(“#text18”).collapse();
}
else {
console.log(“Showing”);
$w(“#button1”).show(); $w(“#text18”).show();
}
});
});

The problem is when I “fill” the database the elements still doesn’t show - they are still collapsed even though I write in the database.

What’s the meaning of:
if (!item.button1)

?

Also you the opposite of collapse() is exapnd() not show().
So if you use collapse() in case1, you have to use expand() in case2.
if you use hide() on case1, only then you can use show() in case2.

@jonatandor35
Hi J.D. Thank you for replying The " if (!item.button1)" is from this thread: https://www.wix.com/corvid/forum/community-discussion/hide-collapse-items-that-are-not-present-in-the-database I altered “show” to “expand”, so now it’s:

$w.onReady(() => {
$w(" #dataset1 “).onReady(() => {
const item = $w(” #dataset1 “).getCurrentItem();
if (!item.button1) if (!item.text18) {
$w(” #button1 “).collapse(); $w(” #text18 “).collapse();
}
else
{
console.log(“Expand”);
$w(” #button1 “).show(); $w(” #text18 ").show();
}
});
});

My elements are still missing even though the database is filled.

@emilkrarup please don’t refer me to other long threads. Just explain to me in your own words, what it means. Do you have a “button1” field in you collection? or what?

@jonatandor35
The #button1 is set to: click action connects to field “Datablad” in the dataset called “Referencer-erhverv dataset”. The field Datablad contains PDF-documents.

The #text18 is to connect to field “Faktaboks” in the dataset called “Referencer-erhverv dataset”- The field “Faktaboks” contains rich text.

if (!item.button1) if (!item.text18) { checks to see if the item variable from the previous line includes the document/text field, which is the field in the collection that stores the document/text.

@emilkrarup I don’t think I understood your explanation.
But I know to read code, and I know that if (!item.button1) means: if a collection field of field key “button1” is not true, then…
and if (!item.text18) means that if (!item.text18) means: if a collection field of field key “text18” is not true, then…

I doubt that what you really meant to write.

@jonatandor35 Do you think I should write if (!item.Datablad) if (!item.Faktablad) as they are the fields in my #dataset I am linking to?

@emilkrarup let me know what exactly you’d like to happen:
Do you want to collapse the elements if the fields datablad and faktablad are both not true?

@jonatandor35 Thank you for your patience - I am new to this. I want to collapse the elements separately. I want button1 to collapse if datablad is not true. The same with text18 when faktablad is not true, but I want the elements to collapse separately.

@emilkrarup Now I got you, so first, remove this code:

//Keep the begging of the code as is
//THIS CODE SHOULD BE DELETED:
if (!item.button1) if (!item.text18) {            
$w("#button1").collapse(); $w("#text18").collapse();        
}  
else 
{             
console.log("Expand");             
$w("#button1").show(); $w("#text18").show();          
}   
//keep the rest of the code  

And instead of this chunk of code, use the following code (in the same rows):

!item.datablad ? $w("#button1").collapse() : $w("#button1").expand();
!item.faktablad ? $w("#text18").collapse() : $w("#text18").expand();

@jonatandor35 it works! Thank you very much!

@emilkrarup you’re welcome :slight_smile: