Filtering after Switch and multiple Buttons

Hello there,

today i were trying to do some filtering but with switch and multiple buttons
the logic is as follow

  • at start shows all staff
  • if button is onClick then it will filter as per button pressed
  • if switch is been checked (selected), then filter per checked
    -if switch is checked then button pressed, then filter by both (switch and button)

Switch between 2 location (filter by location — works)

then if button is clicked filter again (does not work!)

my code

om

import wixData from 'wix-data';

$w.onReady(function () {
});
export function switch1_change(event) {
let Mawaleh = !$w('#switch1').checked;
let Bousher = $w('#switch1').checked;

if (Bousher) {
Filtering("Bousher");
}
if (Mawaleh) {
Filtering("Mawaleh")
}
}
export function Administrators_click(event) {
Filtering("Administrators");
}
export function Management_click(event) {
Filtering("Management");
}
export function Teachers_click(event) {
Filtering("Teachers");
}
function Filtering(bool, click) {
let location = bool;
let section = click;
console.log(location);
console.log(section);
if (location === "Bousher") {
$w('#teamDataset').setFilter(wixData.filter().eq("shortDescription", "Bousher").eq("longDescription", section));
}
if (location === "Mawaleh") {
$w('#teamDataset').setFilter(wixData.filter().eq("shortDescription", "Mawaleh").eq("longDescription", section));
}
}

any help or any suggestion please

You might try clearing the filter before setting a new filter:

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

@3shtar What you’re doing by clicking on the buttons is simply adding more filters on the dataset.

You can try resetting the dataset filters as @yisrael-wix mentioned above, then add the new filter.

$w('#filterButton1').onClick(() =>
    $w("#staffDataset").setFilter(wixData.filter());
    // This will reset the dataset filter when you
    // click on the button.
    
    // Then you can add the filter:
     $w("#staffDataset").setFilter(wixData.filter()
         .eq(key, value)
    );
);

If you want to add more than one filter to the dataset, use (if…else) statements or switch to apply more than one filter as followed:

$w('#filter1Button').onClick(() =>
    $w("#dataset1").setFilter(wixData.filter()                      
        .eq("role", "Administrator")
        .eq("department", "IT")
    )    
);

Hope that helped.
Regards.

@yisrael-wix & @ahmadnasriya thank you booth issue solved