Collapse elements when dataset returns 0 results.

Hi all,

I am using the code below to collapse 3 elements if the dataset returns no results. I expected it to work but it doesn’t. Am I overlooking something?

$w.onReady( () =>
{ $w( “#dataset4” ).onReady( () => {
let count = $w( “#dataset4” ).getTotalCount();
if (count === 0 ) {
$w( “#columnStrip162” ).collapse();
$w( “#columnStrip163” ).collapse();
$w( “#repeater15” ).collapse();
} else {
$w( “#columnStrip162” ).expand();
$w( “#columnStrip163” ).expand();
$w( “#repeater15” ).expand(); }}
);
} );

Thanks!

Hi!

I just added a repeater and two strips to a page and tested your code on my own site and it worked for me.

Could we get a link to your site and the name of the page in the Editor? Once you provide a link to the site we will be able to have a closer look.

Dara | Corvid Team

Hi Dara,

Sure! It’s misterwanderluster.com and the page is the dynamic page “CONTENT (ITEM)”.

Thanks!

Stefan

Hi Dara,

Did you manage to look into this?

Thanks!

The code seems to work just fine, can you provide the site URL where the issue is?

Hi Ahmad,

A page this occurs on is: https://www.misterwanderluster.com/dromen/roadtrippen

Thanks!

Regards, Stefan

I dont know if this is the problem, but sometimes when you have 0 results the return isn’t 0, but undefined or false. try change this

if (count === 0 ) for if (count === undefined ) or if (count === false )

getTotalCount() returns a number, so they can’t use null or undefined here.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#getTotalCount

Previous forum posts can help here too where the code looks similar :wink:
https://www.wix.com/corvid/forum/community-discussion/how-to-hide-a-strip-for-a-repeater-with-no-items

Hi Stefan,

I’ve had a chance to look at your code and I found a couple of issues.

You have a parsing error on line 41 and you have two dataset onReady calls that are outside of the page onReady function.

I recommend creating separate functions for each of the dataset on Ready calls and then calling each function within the page onReady.

The example below should work for you. You do not have to use the same function names, I just named them after the datasets they interacted with.

import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
import wixWindow from 'wix-window';
import wixData from 'wix-data';

//Onderstaande code zorgt ervoor dat BESTEMMINGEN alleen getoond worden als deze beschikbaar zijn in de dataset.
$w.onReady(() => {
    dataset4()
    dataset1()
    dynamicDataset()
});

//Onderstaande code zorgt ervoor dat ITEMS alleen getoond worden als deze beschikbaar zijn in de dataset.
function dataset4() {
    $w("#dataset4").onReady(() => {
 let count = $w("#dataset4").getTotalCount();
 if (count === 0) {
            $w("#columnStrip162").collapse();
            $w("#columnStrip163").collapse();
            $w("#repeater15").collapse();
        } else {
            $w("#columnStrip162").expand();
            $w("#columnStrip163").expand();
            $w("#repeater15").expand();
        }
    });
}

function dataset1() {
    $w("#dataset1").onReady(() => {
 let count = $w("#dataset1").getTotalCount();
 if (count === 0) {
            $w("#columnStrip93").collapse();
            $w("#columnStrip161").collapse();
            $w("#repeater14").collapse();
        } else {
            $w("#columnStrip93").expand();
            $w("#columnStrip161").expand();
            $w("#repeater14").expand();
        }
    });

}

function dynamicDataset() {
    $w("#dynamicDataset").onReady(() => {
 let description = $w("#dynamicDataset").getCurrentItem().cta; //text is the field you want to get its text
        $w("#text426").text = description + ".";
    })
} 

Hope this helps!

Dara | Corvid Team

Hi Dara, this worked! Thank you so much!

Hi Dara,

You’re a hero! Thanks, it worked :slight_smile: