Collapse Strip if table is empty

Hi guys, can you help me out please?

I have a table “Table2” that is on a dynamic page, and filtered by the title field to show only items related to the dynamic page. The dataset connected to it is called “Albums”.
What I would like to do is hide the strip which the table is in whenever the filtered results are empty.

I have tried the below code, and a lot of other variants, but it either results in error or nothing happens

wixData.query(“table2”)
.find()
.then((results) => {
console.log(results.items.length); // check the length of results in the console
if (results.items.length === 0) {
// Table2 is empty
$w(“#StripDiscography”).collapse();
$w(“#StripDiscography”).hide();
} else {
// Table2 has data
$w(“#StripDiscography”).expand();
}
});

I would appreciate your help as I can’t for the life of me get it to work

What is a —> “table” <— from your sight of view?

  1. Some kind of thing what you can find in the kitchen, maden out of wood, which you can use to make some breakfast.

  2. Some kind of a list presented in a digital way, loaded from a database.

  3. The DATABASE itself?

First we have to make clear, that your understandment of a table is right?
I never have seen, that someone can QUERY a table.

Even when i take a look onto the VELO-API-DOCS → there is no such a function to be found!

wixData.query("table2")

So one more time → WHAT IS → “table2” <— in your code?

Is “table2” the ID of your DATABASE?
Do you really named your DATABASE as → TABLE?

CHANGE IT !!!

See here what a table is…

As per the link you shared, a table is a table for dispaying data, which is exactly what I’m using and its ID is #table2
My collection’s name is Artists

Obviously I’m doing something wrong, I’m no pro, that’s why I’m humbly asking for anyone’s help.

Yes, it was clear for me that you are using a table, but the code you have presented, won’t work on a table. This is why i wanted to make clear, that you really are talking about a TABLE.

Yes on your pic we can see the wix-element (TABLE), which either can be connected to a DATASET (and the dataset itself will be connected to your DATABASE), or it can be connected directly to databse (using some code).

In your case you already are using a DATASET.
You can try to manipulate your table with the following code…

$w.onReady(()=>{
    $w('#yourDatasetIDhere').onReady(()=>{
        $w("#table2").onDataChange((event)=>{
            let tableRows = event.target.rows; 
            console.log("RESULT: ": tableRows);
        });
    });
});

Paste that code onto your related page and open it in PREVIEW-MODE.
Take a look onto console. What do you get as → RESULT <— when you do changes on your table ???

When running the code you provided, whenever the table is empty, it results in the following error: Error: Cannot read properties of undefined (reading ‘map’).
When it is run when the table is populated, it shows the array

Can you show the 2x different original screenshots (good and bad data) ?
Also do open the (OBJECT / ARRAY).

I do not have all these informations in front of my eyes …:grin:

Anyway → we already created two different states ! ( STATUS ) → BAD & GOOD

One works, when table has items and the second state is an ERROR-STATE.

Your wish was…

Collapse Strip if table is empty

That means → if you get the error → normaly your Column-Strip should close right?
And if you get some results → your Column-Strip should open again showing your table, what also makes sense, right?

There are now surely several different options of how to manage this problem.

One of these could be…