Empty checkbox in database is not "false"

Hello all!

I’m trying to display and hide elements on an item page depending on the statement of a checkbox in the item database. Basically if the submitted content has been approved by the admin (me), some items will show on the page, else, some items will be hidden.

Here’s an exemple of a validated item : https://www.wehaveadeal.net/deals/1567425530
Comments show.

Here’s an exemple of a pending for validation item : https://www.wehaveadeal.net/deals/1569100108
Comments will hide and a message (in a blue box under the item) will show.

The thing is, it doesn’t work. Any posted item will be considered validated (and hence will show comments etc.) even though the said checkbox in the database is unchecked (and hence its statement is considered as “false”) unless I manually check the checkbox, and then uncheck it, which not what I want here.

Here is my code :

let items = $w(“#dynamicDataset”).getCurrentItem();
if (items[“valide”] === false ) {
$w(“#box42”).show();
$w(“#text111”).hide();
$w(“#box33”).hide();
$w(“#box32”).hide();
$w(“#button15”).disable();
}
else {
$w(“#box33”).show();
}

I think the “false” statement must be replaced by something else, but what ?

Thanks for your help!

Try looking at checked here.
https://www.wix.com/corvid/reference/$w.Checkbox.html

Also check out Nayeli (Code Queen) tutorial here.
https://support.totallycodable.com/en/article/combine-checkbox-values-using-wix-code

Plus also this previous post too.
https://www.wix.com/corvid/forum/community-discussion/uncheck-a-checkbox-if-another-checbox-is-checked

The thing is the checkbox I’m talking about is within the database, and not on the page. If I got this correctly, the page you linked only deals with checkboxes on pages.

Here you can see what I’m talking about : Item 1 has not been validated yet and the “validé ?” box is not checked
Item 2 has been validated and the “validé ?” checkbox has been checked.

Try just using the getcurrentitem call on the checkbox boolean field only

Retrieve the values from a dataset.
$w(“#dataset1”).getCurrentItem().fieldName;

Also try adding the datasets onReady function into the pages onReady function too.

As for checking the boolean field, you’ll still need to use checked in your code and get the value of true of false.

Have a read of the getcurrentitem reference too.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#getCurrentItem

Although to be honest, if you are just checking the boolean field, then you could just simply do a Wix data query on that particular field in your dataset and run the code from that result.

https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html

https://www.wix.com/corvid/reference/wix-data.html#query

@givemeawhisky Alright thanks ! I’m not a code master unfortunately, could you show me how to do based on my existing code ?

Hello Tristan!

Your code is very close. Let me see if I can teach you how it can be fixed to perform the action you are looking for.

The code is similar to yours. Let me explain my code:

  • I added the field key at the end of the .getCurrentItem() instead of creating another variable.

  • The field key for my checkbox is activeListing.

  • Variable for my current item is the word: current

  • Variable is the only word in the if statement parenthesis

What the code says is: When the dynamic dataset is ready, get the the value of this specific field from the current item. If the value returns true then do the following.


$w.onReady(function () {
    $w("#dynamicDataset").onReady(() => {
 let current = $w("#dynamicDataset").getCurrentItem().activeListing;

 if (current) {
            $w('#freeListingMessage').show();
        } else {
            $w('#freeListingMessage').hide();
        }

    });

});

Here is where I used it / what the editor looks like:

This is what my database collection looks like:

These are the live links to test each page:

https://codequeen.wixsite.com/multilevel-paid-list/company/Test-1-Active-Free
https://codequeen.wixsite.com/multilevel-paid-list/company/Test-2-Active-Free
https://codequeen.wixsite.com/multilevel-paid-list/company/Test-3-Not

Might I try to shed some light onto this subject: the basic problem Tristan is running into is truthyness or, in this case, a special case, the age old problem of a boolean not being binary (true/false), but tri-valued (true/false/unspecified), relating in real world terms to checked, unchecked, not filled out (empty). Hence the tri-value (3 values).
So instead of testing for “false”, as Tristan does here:

 if (items["valide"] === false) 

one should test for “(not) true”. So here comes truthy: truthy in Javascript is everything that is not Empty, not Null, Nan, etc.
So Nayeli is right: her code does NOT test for “false”, but she test for “truthyness” in her code here:

if (current)

meaning, for a boolean: if it is checked, but not unchecked or empty or Null or whatever (“falsy”)… etc.

BTW Good to hear from you again, Nayeli, long time no hear.

So as it turns out — I have accidentally checked for ‘truthyness’ in my codes because i KNEW that the results were ‘tri-value’ and I was never able to get the code to work. It wasn’t until you explained it right now that I actually now understand WHY it has to be that way! Thank you @giri-zano !

P.s. Well to make a long story short … I got married … moved from Texas to New Orleans … and it wasn’t until recently that things have ‘slowed’ down enough for me to get back in the ‘forum / youtube / tutorials groove’ again.

@code-queen Congrats on getting married :clinking_glasses::cupid::balloon::gift::ring:

@givemeawhisky Thank you!!!

Thanks Nayeli, it works! :slight_smile:

@code-queen So, now there’s a Code King? Congrats Nayeli! I love hearing good news. :beers::tada: