Make a filter with contains and IF

Hello, I was trying to make a filter with dropdowns. The main idea is after select dropdown with the selector’s necessaries, filter database, and have the result. I have 3 filters, but 2 are working because they are union and use contains form but the other is an if and when I use deactivates the other filters even if it is in the first position or in last.

My questions are how can we do to use this 3 filter together using first the contains and then if conditional.

The format of values that of third filter is a boolean type. Filter that starts working and makes the other stop working is if statement
can you help me please to make 3 filters works simultaneously, alone or together using the button
thanks!!

//search button of clicking
export function searchButton_onClick(event, Sw) {
let searchIndustry = $w('#dropdown1').value;
let searchRegion= $w('#region').value;
let searchOds= $w('#sdg').value;
let filestore = $w('#sdg').value;
$w('#dataset1').setFilter(wixData.filter().contains("sector", searchIndustry).contains("region", searchRegion)).contains("ods", searchOds)
$w('#dataset1').onReady( () => {		
		if (filestore === '1') {
		$w('#dataset1').setFilter(wixData.filter().eq('ods1', true));
		}
		if (filestore === '2') {
		$w('#dataset1').setFilter(wixData.filter().eq('ods2', true));
		}
		if (filestore === '3') {
		$w('#dataset1').setFilter(wixData.filter().eq('ods3', true));
		}

		if (filestore === '4') {
		$w('#dataset1').setFilter(wixData.filter().eq('ods4', true));
		}

		if (filestore === '5') {
		$w('#dataset1').setFilter(wixData.filter().eq('ods5', true));
		}

		if (filestore === '6') {
		$w('#dataset1').setFilter(wixData.filter().eq('ods6', true));
		}

		if (filestore === '7') {
		$w('#dataset1').setFilter(wixData.filter().eq('ods7', true));
		}

		if (filestore === '8') {
		$w('#dataset1').setFilter(wixData.filter().eq('ods8', true));
		}

		if (filestore === '9') {
		$w('#dataset1').setFilter(wixData.filter().eq('ods9', true));
		}

		if (filestore === '10') {
		$w('#dataset1').setFilter(wixData.filter().eq('ods10', true));
		}

		if (filestore === '11') {
		$w('#dataset1').setFilter(wixData.filter().eq('ods11', true));
		}

		if (filestore === '12') {
		$w('#dataset1').setFilter(wixData.filter().eq('ods12', true));
		}

		if (filestore === '13') {
		$w('#dataset1').setFilter(wixData.filter().eq('ods13', true));
		}

		if (filestore === '14') {
		$w('#dataset1').setFilter(wixData.filter().eq('ods14', true));
		}

		if (filestore === '15') {
		$w('#dataset1').setFilter(wixData.filter().eq('ods15', true));
		}

		if (filestore === '16') {
		$w('#dataset1').setFilter(wixData.filter().eq('ods16', true));
		}

		if (filestore === '17') {
		$w('#dataset1').setFilter(wixData.filter().eq('ods17', true));
		}

		}
		)

 
.then((results) => {
        console.log("Dataset is now filtered");
 //$w('#repeater1').data = results.items;
        resultsChecker();
    });
 

//$w('#repeater1').expand();
$w("#dropdown1").value = "Select Type:"; 
$w("#region").value = "Select Category:"; 

}

// resultsChecker function displays a message if no results are returned
function resultsChecker(event){

    $w("#dataset1").onReady( () => {

 let count = $w("#dataset1").getTotalCount(); 

 // if repeater has results, show them
 if(count > 0){
            $w("#text108").hide(); $w("#text108").collapse();
            $w("#repeater1").expand(); $w("#repeater1").show();
        }

 // if repeater returns no results, then show message
 if(count === 0){
            $w("#text108").show(); $w("#text108").expand();
            $w("#repeater1").collapse(); $w("#repeater1").hide();
        }
 
    }); 

}

Hello Luis,

perhaps this one could work for you, but not tested.
Perhaps you have still to optimize it.

export function searchButton_onClick(event, Sw) {
 let filter =  wixData.filter()  
 
 let filestore = $w('#sdg').value;
 //------------------------------------------------------------
  filter = filter.contains("sector", searchIndustry)
  filter = filter.contains("region", searchRegion)
  filter = filter.contains("ods", searchOds)
 //-----------------------------------------------------------
 if (filestore === '1') {filter = filter.eq('ods1', true);}
 if (filestore === '2') {filter = filter.eq('ods2', true);}
 if (filestore === '3') {filter = filter.eq('ods3', true);}
 if (filestore === '4') {filter = filter.eq('ods4', true);}
 if (filestore === '5') {filter = filter.eq('ods5', true);}
 if (filestore === '6') {filter = filter.eq('ods6', true);}
 if (filestore === '7') {filter = filter.eq('ods7', true);}
 if (filestore === '8') {filter = filter.eq('ods8', true);}
 if (filestore === '9') {filter = filter.eq('ods9', true);}
 if (filestore === '10') {filter = filter.eq('ods10', true);}
 if (filestore === '11') {filter = filter.eq('ods11', true);}
 if (filestore === '12') {filter = filter.eq('ods12', true);}
  console.log(filter)
  $w(DATASET).setFilter(filter)
}

Further you can use a for-loop to reduce the size of your code.

Hi Luis :raised_hand_with_fingers_splayed:

You can’t create two filters and assume that both of them will work, when you create a filter, it’ll replace whatever filter you have chosen before.

Also, a small tip for you, instead of using 12 if statements, you can write the filter in a way that accepts the value whatever it was like this:

let filter = wixData.filter().eq(`ods${filestore}`, true);

Another note for you russian-dima, you must include the wixData.filter(), read more about wixData.filter API here.

As for the combination filter, use an if statement like this:

if (filestore !== undefined && typeof filestore === 'number') {
    if (searchIndustry !== undefined && searchRegion === undefined && searchOds === undefined) {
        let mainFilter = filter
            .and(
                wixData.filter()
                    .contains("sector", searchIndustry)
            )
    
    } else if (// More 5 options here ...) {
        // run code here
    }
    
    
}

Hope that helped!
Ahmad

That means my version would not work? (really curious if that works or not).
Have to try it out, that interessts me.

you build a filter using the filter() (creating-filter)

let filter =  wixData.filter() 

refine the filter withWixDataFilterfunctions

{filter = filter.eq('ods1', true);}

and then apply the filter to the dataset using thesetFilter()function.

$w(DATASET).setFilter(filter)

Sorry don’t understand → “… you must include the wixData.filter()…”
Can you explain a little bit?

I don’t use often a filter, not so much experience with it.

OK, let’s assume that the filter is like what you wrote:

filter = filter.eq('ods1', true);

Then when setting that filter on the dataset, it’ll be like this:

 $w('#dataset1').setFilter(filter)
 
 // Which means:
 $w('#dataset1').setFilter(filter.eq('ods1',true))

And that’s totally incorrect, saving the filter must be with wixData.filter(), that means you’re telling the code that the value is a filter() from the wixData module.

The code should be like this:

let filter = wixData.filter().eq('keyValue', value);

$w('#dataset1').setFilter(filter);
// Which is the same as:
$w('#dataset1').setFilter(wixData.filter().eq('keyValue', value));

Take a look at the wixData API module.

Thanks so much, both solutions works!!! I am working programming with python this is new for me even filter, because is simple use a isin or contains, but here you need to put the pieces really organize for something works. Thanks for your time and your quick answer , for teach and I will try to improve my skill to helps to others. Regards!!

You’re welcome Luis, :wink:

Please edit the title of this post and add a [Solved!] tag to it to indicate that it has been solved.