If database field contains X than hide section

Hi there!

On a Dynamic page I want to hide a section on the page when a database field contains a specific value. I tried to achieve this with the below code. To be more specific, I want to achieve that if in “Dynamicdataset” field “type” contains value “TRANSPORTATION / FLIGHT”, section 274 is hidden.

The code is not working, could somebody point me in the right direction? I would really appreciate it!

Best regards,

Stefan

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

/**

  • Adds an event handler that runs when the dataset is ready.
    */
    export function dynamicDataset_ready ( ) {
    $w ( “#dynamicDataset” ). onReady ( () => {
    let itemObj = $w ( “#dynamicDataset” ). getCurrentItem ();
    if ( itemObj . type === “TRANSPORTATION / FLIGHT” ) {
    $w ( “#section274” ). hide ();
    }
    } );

}

You have 2 dataset.onReady nesting in each other. That’s wrong.
You should only have 1 dataset,onready.

Thanks for your very quick answer! I tried deleting either one but still the code is not working.

Try:

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

$w.onReady(() => {
   $w("#dynamicDataset").onReady(() => {
    let itemObj = $w("#dynamicDataset").getCurrentItem();
    if(itemObj.type === "TRANSPORTATION / FLIGHT") {
       $w("#section274").hide();
    }
} );
})
  • make sure the string is exactly RANSPORTATION / FLIGHT with the whitespaces around the slash and nothing more in the beginning and end of the string.
    If you wish the string to include this substring but not to be equal to, do:
if(itemObj.type.includes?.("TRANSPORTATION / FLIGHT")){ 

It worked, thanks very much J.D.!

Dear J.D.,

Would such a code as the one you provided (thanks so much!) also work within a repeater? I have a dynamic page for a travel itinerary that contains a repeater connected database with segments that belong to the itinerary on the dynamic page.

Segments vary from transportation to hotel and each item in the repeater has to show different fields. For example, transportation should show “departure location” and a hotel should show the field “roomtype”. Is it possible to write a code that determines what element is expanded/shown within the repeater item?

Thanks so much!

For repeaters you’ll have to use the onItemReady or forEachItem methods (depends on how you bind the data to the repeater.

Basically it’s something like:

$w('#repeater').onItemReady(($i, iData) => {
 if(iData.type.includes?.("TRANSPORTATION / FLIGHT")){ 
  $i('#someElement').hide();
 }
})

Hi JD,

I would like to use a similar situation, but in a slideshow repeater. The above code you wrote doesn’t work, do you have an idea on how to get it working?

Thanks so much!