Filtering dataset with checkbox groups into repeater

Hey, my team and I have been struggling on code to filter a dataset for a couple weeks now. I’ve stumbled across this article https://www.wix.com/corvid/forum/community-discussion/solved-multiple-group-check-boxes-to-filter-a-dataset-and-filter-update-a-drop-down which is essentially what we want, but when trying the code for our purposes, the dataset still won’t filter. We’re considering using a text value and filtering manually, but I wan’t to see if there’s any way this can work with check boxes directly first.

In summary: we want users to use checkboxes to filter out programs.

Our code:

import wixData from 'wix-data';
export function FilterBySeason(){
 let Seasons = [];
 // Get the indexes of all the checkboxes checked in that group
 let selectedSeasons = $w("#checkboxGroup17").selectedIndices;
 // Now, loop through the checked items and add each city to the Cities array
 if (selectedSeasons.length > 0){
 for (var i = 0; i < selectedSeasons.length; i++) {
         Seasons.push($w('#checkboxGroup17').options[selectedSeasons[i]].value);
      }
      $w("#dataset1").setFilter(wixData.filter()
        .hasSome("date", Seasons)
       );
    } else {
        $w("#dataset1").setFilter(wixData.filter()
        );      
    }
}

export function checkboxGroup17_change(event) {
    FilterBySeason();
}
$w.onReady(function () {
checkboxGroup17_change($w("#checkboxGroup17").onClick())
});

Wix is saying that checkboxGroup17 is undefined, and nothing filters when interacting with the checkbox. Also, the checkbox, dataset, and field is stated in the code. Should I also have the collection name somewhere? I’m a serious beginner.

Hi,

Can you send us a URL to your site?

Thanks,
Sapir

Hey,

Unfortunately I haven’t published that part of the site yet (please let me know if that’s necessary for the help). Here’s an image:

Hi,

You don’t need to publish , you can save and send the URL , its difficult to understand the problem without debugging the code.

Best.,
Sapir

https://editor.wix.com/html/editor/web/renderer/edit/0e3e9114-8356-4bd0-9406-6c3ac9b4cc27?metaSiteId=63fc6ab9-ef9a-4ed2-9534-eb3ed77b9c29&editorSessionId=2EA7AC5E-5DCF-483A-98CA-617898501D9F

Gotcha!

Hey Sapir!

Just making sure: does that link work for you?

Thanks,
Daysia

Hi,

After checking your site I have done some changes in your code and elements:

  1. Disconnect all the check groups to the same dataset that the repeater connected to ( I don’t understand why you need to do this connection from the first place)

  2. View this code:

import wixData from 'wix-data';

$w.onReady(function () {
    $w("#dataset1").onReady(async () => {
 let data = await $w("#dataset1").getItems(0, 4);
        console.log("data", data);
    })
 //checkboxGroup17_change($w("#checkboxGroup17").onClick())
});

export function checkboxGroup17_click(event) {
    FilterBySeason();
}

async function FilterBySeason() {
 let Seasons = [];
 // Get the indexes of all the checkboxes checked in that group
 let selectedSeasons = $w("#checkboxGroup17").selectedIndices;
 // Now, loop through the checked items and add each city to the Cities array
 if (selectedSeasons.length > 0) {
 for (var i = 0; i < selectedSeasons.length; i++) {
            Seasons.push($w('#checkboxGroup17').options[selectedSeasons[i]].value);
        }
    } else {
        $w("#dataset1").setFilter(wixData.filter());
    }

    console.log("Seasons", Seasons);
    $w("#dataset1").setFilter(wixData.filter()
        .hasSome("date", Seasons)
    ).then(async () => {
 let data = await $w("#dataset1").getItems(0, 4);
        console.log("data 1 ", data);

    })

}
  • I have done the setFilter() function outside the for loop

  • I added some console.log() in order to see the data in the dataset before the filer and after ( of courts its not necessary)

Best,
Sapir

Here is an example that might help:

Multiple Filter Options
Filter results based on multiple selections in multiple filter groups.

Hi Sapir,

Thank you so much for that tip!!! I automatically thought the checkboxes had to be connected for some reason. However, unfortunately, the boxes still arent filtering. Is this because I have to add an .onChange function?

I’m not sure I understand, when I checked the code I sent you I sew that each click on the check group that data in the dataset was filtered and as a result the repeater present different items.

Hi Yisrael,

This was extremely helpful!! Thank you so much. I have a couple questions. Is there any way I can make it not as case-sensitive? For instance, if someone selects “fall” in the checkboxes, how can I make a program with “fall” and “winter” tags show in the repeater? At the moment, this can only happen if both “fall” and “winter” are selected.

Also, is there any way I can invert the way programs are shown? So when no boxes are selected, all programs will show, then it will begin filtering out as people select. This question is less significant than the first.

Regarding case-sensitive, why not just use Fall/Winter/etc for the filters. Or, you can do the filtering based on fall/winter/etc, and then use the Repeater’s onItemReady() function to change the way the Repeater is displayed.

For your second question, you can check in code if checkboxes are checked or not, and then reset the filter, like this:

$w("#dataset1").setFilter( wixData.filter() );

This line of code simply sets the filter to an “empty” filter so that the dataset is not filtered.

Is using fall/winter/etc for the filters different from setting the checkbox choices as these options, as shown in the image above?

@sapirh
I’m sorry, I’ve tried a few times and haven’t gotten anything. I’ve also had a colleague try from his end, but with no filtration… Might there be a system error on our end?

Hi Yisrael, I think I found the complication. It looks like the .hasSome function will want to match with all of the filters instead of only a portion. For instance, if one of my programs has “fall, winter” but someone only selects “fall,” this program won’t appear. Do you know how I can remedy this?

Hi @yisrael-wix Sorry if this isn’t the best place to jump in with a new request (it relates to Multiple Filter Options). Is it possible to filter a repeater using multiple groups of Selection Tags? The goal is to be able to separate groups of Selection Tags e.g. #selectionTags1 and #selectionTags2 from where multiple selections can be made in each group to filter a dataset by a ‘tags’ field ( fieldToFilterByInCollection).

I can do this without a hitch for just one set of Selection Tags, following this example: https://www.wix.com/corvid/example/filter-with-multiple-options

My query, in a nutshell, is whether it’s possible to filter by two groups of selection tags, on the same field within a dataset. Example:

“Producers” dataset has multiple tags within a ‘tags’ field e.g. Bordeaux, Burgundy, Red, White, Rosé. I’d like to be able to separate the Selection Tags so #selectionTag1 enables filtering by Region (Bordeaux, Burgundy) and #selectionTag2 by Colour (Red, White, Rose). Then, visitors to the site can filter by Producers from Burgundy that offer white wine etc.

I could throw them all into just one #selectionTag1, and use hasAll(… but visually it would be great if the tags can be separated on the page (like you have with ‘type’ and ‘lighting’ in the checkbox groups).

Thanks so much in advance if you can help at all.