- SOLVED - Button disable after a date passes from database

I was wondering if it was possible to disable a button after a date has passed in the database?
I have some trips that when the date displayed for the trip has passed (the date is pulled from a date field in the database) then the ‘book here’ button would be disabled? Is this possible?

yes. for the example I took the date of the first row of my collection, but it is the same if you retrieve the date via a query for instance.
This goes in the page code.

$w.onReady(function () {
    
$w("#dataset1").onReady(() => {
// Retrieve first row
var itemDate = $w("#dataset1").getCurrentItem();
// Retrieve date column value
 var myDate= itemDate["uneDate"];
// Today's date
 var currentDate= new Date();
// Check date in collection is not empty
 if (myDate !== undefined) {
     // Hide if date in collection is prior to current date
     if (myDate<currentDate) $w("#text19").hide();
     // Otherwise display
     else $w("#text19").show();
  }
  });

Thank you for your response jeromechauveau. The code you have posted makes some sense but I’m struggling to implement this…

I have four dates on the dynamic page, all of which are connected to different fields in the database. Field key ‘dateone’ ‘dateTwo’ etc, so I know that I will need to query this field then check if it is valid to today’s date. Then disable the relevant button to that date.

I’m happy with querying the database but not sure how to use the code above to disable the button, therefor I’m finding it hard to test as I can’t see any change… Would you be bale to point me in the right direction?

Hi jeromechauveau, I have been trying to implement this but seem to be struggling a little, would you be able to help? My code is below

$w.onReady(function () {
    $w("#dynamicDataset").onReady(() => {
 let itemObj = $w("#dynamicDataset").getCurrentItem();
        $w("#date1").text = itemObj.dateone.toDateString();
        $w("#date2").text = itemObj.dateTwo.toDateString();
        $w("#date3").text = itemObj.datethree.toDateString();
        $w("#date4").text = itemObj.dateFour.toDateString();
 var itemDate = $w("#dynamicDataset").getCurrentItem();
 var myDate= itemDate["uneDate"];
 var currentDate= new Date();
 if (myDate !== undefined) {
 if (myDate<currentDate) $w("#bookhere1").disable();
 else $w("#bookhere1").enable();
        }
    });
});

Many thanks!

Hi,
Notice you’ve copied the name of the field “uneDate” from the code above, is that also the name of the field in your database?

That makes so much more sense now! It’s works, thank you for your help!
Would you know how to change the $w(“#bookhere1”).label?

 
$w.onReady(function () {
    $w("#dynamicDataset").onReady(() => {
 let itemObj = $w("#dynamicDataset").getCurrentItem();
        $w("#date1").text = itemObj.dateone.toDateString();
        $w("#date2").text = itemObj.dateTwo.toDateString();
        $w("#date3").text = itemObj.datethree.toDateString();
        $w("#date4").text = itemObj.dateFour.toDateString();
 var itemDate = $w("#dynamicDataset").getCurrentItem();
 var myDate= itemDate["dateone"];
 var currentDate= new Date();
 if (myDate !== undefined) {
 if (myDate<currentDate) $w("#bookhere1").disable, $w('#bookhere1').label = "Trip Expired"();
 else $w("#bookhere1").enable();
        }
    });
});

Thank you so much for your help :slight_smile:

Try this code:

if (myDate<currentDate){
 $w("#bookhere1").disable;
 $w('#bookhere1').label = "Trip Expired";
 }

The code above throws up an error on the else statement? I have come up with the code below and it seems to work well, but I’m sure you must be able to tidy this up a little…

if (DateOne<currentDate) $w("#bookhere1").disable();
 if (DateTwo<currentDate) $w("#bookhere1").label = "More Dates Soon";
 else $w("#bookhere1").enable();

Hi,
You are right, I forgot to wrap the code with brackets, edited the code now.

Hi Or,

That still doesn’t work, it removes the error, but it doesn’t disable the button? It does display the new text on the button “More Dates Soon”
The only code that is working is the last one I posted above. It seems to work fine, but just wondered if it could be a little tidier. Thank you for all of your help!