A simple algorithm, in Corvid

Hi,

This is the algorithm:

	once page loads
	check if dataset1 field1 IsNot Null
		make Button1 appear
	else
		keep Button1 hidden/make it disappear/print a message

The logic is to have a download button appear IFF the connected dataset’s field has indeed something to download and it’s not empty.

So how could I achieve something like this in Corvid/Javascript and more importantly, where would you be kind enough to redirect me for some solid and reputable/tested tutorials?

Thanks in advance!

https://www.wix.com/corvid/reference/api-overview/introduction

https://russian-dima.wixsite.com/meinewebsite

once page loads =

$w.onReady()

});

Hide or show a button…

$w("#myButton").show();
$w("#myButton").hide();

If-else-logical…

if( $w("#myElement").hidden) {$w("#myElement").show();}
else {$w("#myElement").hide();}

Print a message… (this message will be shown in the CONSOLE → Press F12, when you are using google-chrome-browser) also you will find the message in the console of “Preview-Mode”).

console.log("YourMessage")

Working with DATASET…

Thanks for the documenation. Thanks for the link to your site. Thanks for the code. Thanks for your time! I really appreciate it.

One small part left in question though: how to say, ’ if this dataset’s field is to be found empty , (hide the button, etc)'…

I know the answer/syntax is somewhere in the documentation and I’ve already started studying Javascript, but I’d appreciate a quick fix for my site, at the moment…

Perhaps you could use–>

function getCurrentItem (): Object
getCurrentItem Parameters
This function does not take any parameters.
Returns
The current item, or null if there is no current item.

Already found a solution?
If not yet, so you can find here a little example…

https://russian-dima.wixsite.com/meinewebsite/download-list

@russian-dima thank you for coming back, really. Well, I was trying to ‘digest’ all this information and to decide where/how to begin. Just visited your latest example and now browsing your source page. I guess I’m only missing the ’ if no file to download, then no download button ’ part of my code but I’m sure this is lurking somewhere in your examples.

(I come from Python. I have a good grasp on what’s going on but JS’s syntax is really all over me).

Did you already have seen the example?
Already have tested and tried it out?

Is this example similar to what you want to create?

  1. Fill out (download-name) + (url) + (press “Add Download”).
  2. Do the same again, but do not fill out the URL.

Did you see the difference?

  1. Press on one of the DELETE-Buttons…

The DOWNLOAD-BUTTON itself is still not activated —> without function in this example.

Also a DATASET is used in this example ! ! !

import wixData from 'wix-data';

$w.onReady(function () {
    xxx()
    $w('#dataset8').onReady(()=>{
        $w('#button1').onClick(()=>{console-console.log("Saving-Process started");  
 let toSave
               toSave = {
 "title":    $w('#input1').value,
 "url":      $w('#input2').value,
            };
 
            wixData.save("downloads", toSave)
            .then( (results) => {
 let item = results; //see item below
                $w('#dataset8').refresh()
                console.log("Total-Count = " + $w('#dataset8').getTotalCount())
            } )
            .catch( (err) => {
 let errorMsg = err;
            } );
            xxx()
       })


       $w("#repeater1").onItemReady(async($item, itemData, index) => {
            $item('#button3').onClick(async(event) => {
                console.log(index);
                console.log(itemData);
 //    $item("#text1").text = "deleting in process"
                $item("#button2").hide()
 
 await $w('#dataset8').setCurrentItemIndex(index)

                $w('#dataset8').setFieldValues({
 "title":  "data deleted",
 "url":   ""
                } );
 
 //$w('#dataset8').setFieldValue("url", "")

                $w('#dataset8').save()
            })
        });  
    })
})

function xxx (parameter) {
    $w('#repeater1').onItemReady(($item, itemData, index) => {
 const thereIsNoUrl = !itemData.url
        console.log(index)
        console.log(itemData)
 if (thereIsNoUrl) {$item('#button2').collapse()}
    })
}

my used dataset = “dataset8”
my used repeater = “repeater1”

field: —> title = “download-name”
field: —> url = “Download-URL”

yes, just played with deleting the entries and re-entering new values, very nice, this is exactly what I was looking for but no time to test it yet. I’ll report back, you have many beautiful things in your tutorial site and I’m keep getting new ideas…

@gemats
Tell me your solution, when you have found it :wink:
This Download-List can be improved a lot !!!
Work on it, you have all the code for it, right now.

Hey @russian-dima , you’ve been gone for a while. Glad to see you back. I hope all is well.

@yisrael-wix
Hello my friend, yes i was gone for a while, but now i try to come back.
Forgot a lot of Corvid-Programming in this time. I have to study it again a little bit :joy:
:sweat_smile::sweat_smile::sweat_smile:.

Nice to see you again!

@gemats
The most problematic part here in Corvid/JavaScript is —> “async-await” and “.then”-syntax. Here you always have to be very carefully.

@russian-dima This is what I came up with, still not good:

$w.onReady (function() {

//#text1 gets its values from a database collection

if( $w("#text1").isVisible ) { <--here, I want to say, if #text1 contains any value
  $w("#button1").disable();
  $w('#text2').text='No file to download. Button disabled!';
}
else {
  $w("#button1").enable();
  $w('#text2').text='Button functional! Proceed with download';
}

});

I’m experimenting successfully with many TextBox properties (as found here: https://www.wix.com/corvid/reference/$w/textbox) but I can’t find what I’m looking for.

What I want to express is: if #text1 isEmpty/isNull/etc, then disable the button. I CAN disable the button by defining, for example, text1 visibility, but this is not what I want. Do you know how to build the expression above? How to check for #text1 (which is a simple text box (not input, like your example)) having any value or not?

@gemats

if ($w('#text457').text==="") {$w('#mybutton.disable'}
else {$w('#mybutton.enable'}

other one could be…

if ($w('#text457').text===null) {$w('#mybutton.disable'}
else {$w('#mybutton.enable'}

other one could be…

if ($w('#text457').text===0) {$w('#mybutton.disable'}
else {$w('#mybutton.enable'}

or if you work with input-fields…

if ($w('#input1').value==="") {$w('#mybutton.disable'}
else {$w('#mybutton.enable'}

@russian-dima No luck. This is the updated code. Doesn’t work. Doesn’t recognize the empty text box in order to disable the button. Button is always enabled, no matter if #text25 box loads empty or not empty in my page.

$w.onReady (function() {


if( $w("#text25").text === '' ) { 
  $w("#button1").disable();
  $w('#text29').text='No file to download. Button disabled!';
}
else {
  $w("#button1").enable();
  $w('#text29').text='Button functional! Proceed with download';
}

});

Looks like you figured out the syntax for If statement but still need syntax to check if a specific field is empty.

Do not rely on text on the page. Check the dataset instead.

Assuming you are checking a dynamic dataset / page with a single item, it will look something like this:

let current = $w (“#myDynamicDataset”). getCurrentItem.fieldkey ();

The field key being the column from your database collection.

You can read Wixs doc to learn more: https://www.wix.com/corvid/reference/wix-dataset/dataset/getcurrentitem

Hi, indeed I’m checking a dynamic page. I did the following and I get a lot of errors.

let current = $w("#dynamicDataset").getCurrentItem.comments();
 if ( current === '' ) { 
  $w("#button1").disable();
  $w('#text29').text='No file to download. Button disabled!';
}
else {
  $w("#button1").enable();
  $w('#text29').text='Button functional! Proceed with download';
}

When you check for empty field, try this instead …

If ( !current)

(In other words, if false do something)

@code-queen Still error: TypeError: Cannot read property ‘comments’ of undefined

let current = $w("#dynamicDataset").getCurrentItem.comments();
 if (!current) { 
  $w("#button1").disable();
  $w('#text29').text='No file to download. Button disabled!';
}
else {
  $w("#button1").enable();
  $w('#text29').text='Button functional! Proceed with download';
}