Hi there,
I have a CMS called “Awards” which refers to the “Projects” CMS. On the dynamic page displaying the Projects items, I would like the section containing the Awards to collapse if the project does not have any Awards linked to it. Awards are manually filtered in the menu. This code seems to work, but could you please tell me if this is the correct method?
import wixData from 'wix-data';
const repeaterAwards = '#repeaterAwards';
const sectionAwards = '#sectionAwards';
$w.onReady(function () {
$w('#dataset1').onReady(() => {
wixData.query("Awards").find()
.then(awardsRes => {
if ($w('#dataset1').getTotalCount() === 0) {
$w(sectionAwards).collapse();
} else {
$w(sectionAwards).expand();
}
});
});
});
There’s never just one way to do anything, right? If it works, great but yes there are always better (more efficient) ways to do this.
In this case, if you’re using a repeater, you can just set that if it does not have any item data (values) to collapse. I cannot see your website or how it works so if it works then you can just leave it as is. If everything is connected to a dataset then the repeater on item ready or for each item (if changes without changing the page) would work fine. You would use $item instead of $w of course.
As @oakstreetassociates mentioned, there’s multiple ways of doing something with code that will result in the same outcome.
Looking at the code (and assuming it’s the only code on the page), I’d potentially remove the wixData.query
part. The dataset is already getting the data and the query is getting the data again, but not being used anywhere.
I’d potentially go with this, but again, it depends on the current setup you have 
const sectionAwards = '#sectionAwards';
$w.onReady(function () {
$w('#dataset1').onReady(() => {
if ($w('#dataset1').getTotalCount() === 0) {
$w(sectionAwards).collapse();
} else {
$w(sectionAwards).expand();
}
});
});
1 Like