simple show/hide button based on dataset Field Key value

Hi guys,

I have been searching and trying for a few days now but can’t seem to find the right answer to my question. Forgive me if this has been asked before, then please point me in the right direction.

I’m trying to understand how I can show or hide some page elements like an image, button or video player, based on values I get from the database, using custom code.

My database has movies in them (title, editor, year, runningtime, etc) and the only thing I want to do is hide a specific button on a Dynamic Page (the movie details page) when the Field Key (type: boolean) is TRUE.

So I would nee something in code like:

  • get all database values for this film from the database
  • if = TRUE hide
  • else show

Tried hundreds of code snippets and combinations, but can’t seem to get this to work.

Please advise and thanx in advance!

Of course you can:

  1. Query the database collection with .eq(fieldtypename, true) and get all the matched results

  2. if( results.items.length > 0){$w(“button”).hide();}else{$w(“button”).show();}
    See:
    https://www.wix.com/velo/reference/wix-data/query
    https://www.wix.com/velo/reference/$w/hiddenmixin/show
    https://www.wix.com/velo/reference/$w/hiddenmixin/hide

Hi JD, thank you for your reply. I couldn’t get it to work with your example, but managed to figure it out anyway. This is how I solved it;

$w.onReady(() => {
$w( “#mydynamicdatasetname” ).onReady(() => {
const item = $w( “#mydynamicdatasetname” ).getCurrentItem();
if (item.myfieldkeyname) {
$w( “#element1” ).expand();
$w(“#element2”).expand();
}
});
});

So now the dynamic page loads, looks if there is a value in “wyfieldkeyname”. If there is, it shows element 1 and 2 on this page.

A very important thing to keep in mind that for this to work, the elements both have to be marked as “collapsed” in the Deafult Values pannel .

Hey Stefan, thanks for figuring this out. I’ve been hunting around for a way to do this too and thanks to your method mine is working too :slight_smile:

Hi estefano, can this code be written directly in the “page code tab” or do I have to create a js file in the backend? My understanding is that Wix collections cannot be accessed from the code tab of the page.
Thank you so much