I have a collection called “locationAvailability” with dates and a number of different identifier (here in this example 3, but the code should be flexible enough to run for any number of columns.
Based on the date selected by the user, I want to return the ID for which there is a “1” in the cells
I tried the following code, however I didn’t manage to solve it. Here is the code I am using
var myDate = $w(“#datePicker1”).value; var date = myDate.getDate(); var month = myDate.getMonth() + 1; var year = myDate.getFullYear(); var dateStr = date + “/” + month + “/” + year;
This is the code if you want to get the date identifier (as you said at the end of your question). But if you want to return the entire row, as you stated in the post title, then it’s a little bit different.
I have one last thing where I am struggling. Based on the array of relevant IDs, I want to filter a table (dataset1 below). If I enter manually an ID, it filters it. However if I pass the array, the filter does not work. Any idea what I am missing?
Thanks a lot for the help!
export function datePicker1_change(event) {
var myDate = $w(“#datePicker1”).value; var date = myDate.getDate(); var month = myDate.getMonth() + 1; var year = myDate.getFullYear(); var dateStr = date + “/” + month + “/” + year;
wixData.query(“locationAvailability”)
.eq(“availability”, dateStr)
.find()
.then( (results) => { let items = results.items; if (items.length > 0){ let item = items[0]; let dateIDs = Object.keys(item);
dateIDs = dateIDs.filter(e => item[e] === 1);
console.log(dateIDs);//an array with the relevant dateIDs
doesn’t work unfortunately. The console.log works, the array has the required elements in, but instead of showing the element in the table, the table is blank. Any idea why that could be?
var myDate = $w(“#datePicker1”).value; var date = myDate.getDate(); var month = myDate.getMonth() + 1; var year = myDate.getFullYear(); var dateStr = date + “/” + month + “/” + year;
console.log (dateStr)
// find all the locations that are available and save them in an array
wixData.query(“locationAvailability”)
.eq(“availability”, dateStr)
.find()
.then( (results) => { let items = results.items; if (items.length > 0){ let item = items[0]; let dateIDs = Object.keys(item);
dateIDs = dateIDs.filter(e => item[e] === 1);
console.log(dateIDs);//an array with the relevant dateIDs
this part here works:
//$w(“#dataset1”).setFilter(wixData.filter() //.contains(“identifier”, “ID00000001”));
→ If I manually define ID00000001 is the relevant one, only this one shows up. But for the code above, the table is blank.
This one is the database where the ffilter is applied. This is also a table shown on the website with about the same categories, only the identifier is not part of the table.
This is the database where the relevant identifiers are taken from (based on 0 and 1).
I am a bit confused. You are right, the elements in the array are id instead of ID, however in the collection both are with capital letters. How come this is changed in the query?
I tried the query, however it is not working
export function datePicker1_change(event) {
var myDate = $w(“#datePicker1”).value; var date = myDate.getDate(); var month = myDate.getMonth() + 1; var year = myDate.getFullYear(); var dateStr = date + “/” + month + “/” + year;
console.log (dateStr)
// find all the locations that are available and save them in an array
wixData.query(“locationAvailability”)
.eq(“availability”, dateStr)
.find()
.then( (results) => { let items = results.items; if (items.length > 0){ let item = items[0]; let dateIDs = Object.keys(item);
dateIDs = dateIDs.filter(e => item[e] === 1);
dateIDs = dateIDs.map(e => e.toUpperCase);
console.log(dateIDs);//an array with the relevant dateIDs
In the collection there’s field name and field key. The field key always starts with a lower case letter no matter what. The query retrieves the field key only and that’s the reason.
Add () like this:
dateIDs = dateIDs.map(e => e.toUpperCase () );