Hello!
I need help trying to get a way to detect if a photo item in a dataset is empty so I know whether or not a expand button should show.
Here is my code:
if(!$w('#dynamicdataset1').getCurrentItem.optionalphoto {
$w('expandbutton').hide()
}
This doesn't work when I am on a dynamic page and the optionalphoto item is empty. It does stay showing if I do have something in the optional photo item.
Help PLEASE. I AM NEW. BUT I LOVE WIX CODE.
Any confusion? Just ask!
Hi There
OK I made a minor, but important, mistake with the example I provided (which I have corrected in the post above)
The test I proposed:
if (!currentItem.optionalphoto || currentItem.optionalphoto.length > 0)
should have been
if (!currentItem.optionalphoto || currentItem.optionalphoto.length === 0)
Essentially testing to see if
or
OK now there are a couple of things to look at.
1. We need to understand more what you are trying to accomplish and
2. a little education regarding images and how they are used.
Let's start with the second
When you download an image it goes into a special storage area that is referenced by the Wix code framework using a URL that is a string and looks like this:
"wix:image://v1/df0e14_5d4967c2f63e4ffe82a80eb7e52f39dc~mv2_d_1667_1667_s_2.png/df0e14_5d4967c2f63e4ffe82a80eb7e52f39dc~mv2_d_1667_1667_s_2.png#originWidth=1667&originHeight=1667"
When you access the property for optionalphoto in the dataset you are really looking for a string that looks like this.
The Wix API does the hard work for you in terms of determining where your image is and how to reference it using the image type property and creating the string describing the URL.
So if the image string is malformed (not easy to figure out unless you try to use it) or either doesn't exist or has a length of zero (e.g. the string looks like "") then you don't have an image.
You can test this out in your code by adding a log statement that will show up at the bottom of your page when running in preview mode. Try inserting this call before the if test:
console.log('Dumping dataset currentItem...\n'+ JSON.stringify(currentItem)); console.log('Optional Photo: '+currentItem.optionalphoto);
If you continue to have problems then helping you will be easier if you share the page you are having problems with and describe what you are expecting to see on the page.
Hope this helps
Cheers
Steve
You need to use the correct syntax on your test.
The if conditional statement looks like this:
if ( condition ) { // Do something } else { // Do something else }
If you look at your code snippet you will see you are missing a closing parenthesis. Additionally the getCurrentItem is a function not a property so it needs to have parenthesis appended. If you add that your code may work.
// -------------- add closing parenthesis here ------------ + // add parenthesis here ---------- v v if ( !$w('#dynamicdataset1').getCurrentItem().optionalphoto ) { $w('expandbutton').hide(); }
One extra test you could perform is to verify that the photo link name is longer than 0.
let currentItem = $w('#dynamicdataset1').getCurrentItem(); if (!currentItem.optionalphoto || currentItem.optionalphoto.length === 0) { $w('expandbutton').hide(); }
Cheers
Steve