Hi guys,
I have been trying for a couple of days and I would really appreciate some help with passing variables across pages when it comes to dates. Here is what I am trying to achieve:
1) On the 1st page: a date picker lets the user pick up a date, then a "Search" button directs the user to a 2nd page
2) On the 2nd page: the variable from 1st page is passed to page 2, assigned to another date picker (which shows the value initially entered by the user on the 1st page), and a collection from a dataset is retrieved and filtered by date (according to the date value entered on page 1) and showed on page 2 through repeaters
The code that I am using is:
On 1st page:
import {session} from 'wix-storage'; // activates wix-storage
export function button_click(event, $w) { // on click...
let start = $w("#datePicker1").value; // assigns datepicker value to start variable
session.setItem('startdate', start); // stores start variable using startdate key
}
On 2nd page:
import wixData from "wix-data"; // activates wix-data
import {session} from 'wix-storage'; // activates wix-storage
$w.onReady(function () {
let start = session.getItem('startdate'); // retrieves start variable from page 1 (WORKS)
$w('#datePicker2').value = start; // assignes start value to datepicker on page 2 (DOESN'T WORK, I would like to have the value from page 1 passed to and showed in the new datepicker)
let e = $w("#datePicker2").value; // assigns datePicker2 value to variable e
e.setHours(0,0,0,0); // clears out the time from e
$w("#RoomsDS").setFilter( wixData.filter() // filters the dataset RoomsDS by date
.lt("availability", e));
session.clear(); // clears the session
});
To be noted: All the filtering code works well when it is run from page 2, with a date selected by the user directly into datePicker2. It is when I try to get the stored data from page 1 and assign it to datepicker2 (page 2) that somethings is not working...Anything I should do to set up datepicker2 in order to be able to "accept" data instead of running with today's date or blank?
Any help is appreciated!
Alessandro
See here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON
Basically, call the Date constructor with the string value, e.g.
new Date(theStringValue)
Hi Ofer, many thanks for your feedback it makes sense.
It looks like the date is automatically stored in a variable as a string e.g. "Sun Apr 29 2018 00:00:00 GMT+0100 (BST)" which I can display in a text object but can't indeed use as a date object. Would you be able to suggest code I should use to convert the above string back to a date object?
Thanks
Alessandro
Hi Alessandro,
I think the problem is that the date picker works with `Date` objects, while storage works with strings.
You might need to save a string representation of the date to storage, and parse it back to a `date` object when reading from storage before using it to set the value of the date picker.
Hope this helps