Filter dataset for 'month'

Hello,

I’ve successfully created a page to filter a dataset by several fields, including date range. However, the data includes records going back around 150 years, and for this particular dataset it would be really useful to search by month. Currently I can search by year using the date range filter, but I can’t work out how to pull out all of the records with a date in May, for example. I’ve tried using getMonth, but anything I’ve tried so far either does nothing or says that ‘date’ is undefined. Any advice would be very much appreciated.

My filter code is below.

Thanks,

Rory

export function filter_click(event, $w) {

let filter = wixData.filter();
if ($w(“#iSpecies”).value) {
filter = filter.contains(“species”, $w(“#iSpecies”).value);
}
if ($w(“#iSite”).value) {
filter = filter.contains(“site”, $w(“#iSite”).value);
}
if ($w(“#iRegion”).value) {
filter = filter.eq(“region”, $w(“#iRegion”).value);
}
if ($w(“#iMonth”).value) { //filter needed here!
}
if ($w(“#iDate”).value) {
const value1 = $w(“#iDate”).value;
const value2 = $w(“#iDate2”).value;
value2.setDate(value2.getDate()+1);
filter = filter.between(‘date’, value1, value2);
}
$w(“#dataset1”).setFilter(filter);
$w(‘#table1’).expand();
}

Hey Rory,

If you want to fetch all records of a certain month in a certain year, you can do that with the ‘between’ filter by creating two dates, one at the very beginning of that month and one at the very end.
However, if you want all records of a certain month in all years, I don’t think that’s possible at the moment. Wix Data doesn’t have a filter that enables you to do that.

If, instead of using a dataset you will handle the data yourself (query Wix Data directly and put the results on the table manually), you can write custom logic to filter the results in the client side with any custom logic you want.

Good Luck,
Itai

Thanks Itai,

It’s a filter for records from a certain month (and eventually also a certain day) in all years that I’m after unfortunately. It’s a collection of wildlife sightings, so it would be really useful for people to see what had been reported on this date in previous years.

I couldn’t find anyone else looking asking the same question, but I found several posts about date formatting (e.g. https://www.wix.com/code/home/forum/questions-answers/date-display-formating) that seem to pull out the month from a date with .getMonth. Is there not a way that this could be applied to a dataset and compared to the value in a form input? Sorry, I’m still very much a beginner at this, so apologies if that’s a stupid question!

Thanks

That’s not possible at the moment.
You can try working around it, for example by adding a column to your database called “month” and storing there only the number of that record’s month. Then you can filter it using .equals(“month”, 7) or something of that matter.

Brilliant, thank you. I can’t believe I hadn’t thought of that! It works perfectly now.

On a related note, I have another page which filters a dataset in a very similar way, but is linked to a gallery rather than a table. I used the same code to create it, but it uses checkboxes instead of dropdowns and text input. My collection has a ‘tags’ field (basically just a list of keywords), and what I want is for the user to be able to filter the gallery using checkboxes and .contains(). However, clicking one checkbox works perfectly, but clicking a second searches for images with both tags, rather than all images with either tag. I can’t seem to find a solution which will find all images that relate to any ticked checkbox.

I hope that makes sense. In short, I want the number of images in the dataset to go up with every ticked checkbox, rather than down.

Thanks,

Rory

Hey Rory,

I’m glad it worked!
Regarding your second issue, you have 2 options.
Either try to use Wix Data’s hasSome function: https://www.wix.com/code/reference/wix-data.WixDataFilter.html#hasSome (and make sure the tags field is an array)
Or try Wix Data’s or function: WixDataFilter - Velo API Reference - Wix.com to define multiple filters and combine the results.

Good Luck,
Itai

Thanks again for your help. Unfortunately, I’m a bit lost with it. I’ve tried numerous different ways to use the hasSome function, but it doesn’t seem to find any images from the database, whereas .contains does.

My latest attempt looks like this, but I don’t know if I’m going the right way about it, and I can’t seem to work out how to get from here to actually populating my gallery with the results.

export function filter_click(event, $w) {

let galleryQuery = wixData.query(“MyCollection”);
var tagFilter = ;
if ( $w(“#checkbox1”).checked ) {
tagFilter.push(‘bird’);}
if ( $w(“#checkbox2”).checked ) {
tagFilter.push(‘mammal’);}
if ( $w(“#checkbox3”).checked ) {
tagFilter.push(‘plant’);}
if ( $w(“#checkbox4”).checked ) {
tagFilter.push(‘invertebrate’);}
if (tagFilter.length > 0) {galleryQuery = galleryQuery.hasSome(“tags”, tagFilter);}

You also said in your last message that I should make sure the field is an array. I wondered whether this might be my problem, so I tried many ways of presenting the data in the field, but none worked with .hasSome. How do I make sure I’m presenting the data as an array?!

Thanks,

Rory