Dataset filter parameters

I have a Dataset that I want to allow users to filter based on whether they enter filtering criteria or not. There are 4 types of filtering criteria: ’ color ', ’ height ', ’ width ’ and ’ length '. I therefore need to build a query based on which fields the user fills in, IF he fills them in.

This is my code:

    $w ( "#datSearch" ). setFilter (  wixData . filter () 
        . eq ( "color" ,  $w ( "#txtColor" ). value ) 
        .eq( "height" ,  $w ( "#txtHeight" ). value ) 
        . eq ( "width" ,  $w ( "#txtWidth" ). value ) 
        .eq( "length" ,  $w ( "#txtLength" ). value )) 
        . then ( () => { 
console.log("FILTERED!"); 
    } ) 

If the user fills in only one field it works fine. When a user fills in 2 or more fields, it doesn’t work anymore.

Example: If the user enters filtering criteria for color (say, ‘blue’) and length (say, ‘59’), I’d expect the dataset to be filtered for ‘blue’ AND ‘59’ .

@internationalcovidsu There is a good example of how to do this in the documentation for the dataset setFilter function . With your code, chaining the filter statements together based on whether the filter criteria is filled in, would go something like this:

let filter = wixData.filter();
if ($w("#txtColor").value){
    filter = filter.eq("color",$w("#txtColor").value)
}
if ($w("#txtHeight").value){
    filter = filter.eq("height",$w("#txtHeight").value)
}
if ($w("#txtWidth").value){
    filter = filter.eq("width",$w("#txtWidth").value)
}
if ($w("#txtLength").value){
    filter = filter.eq("length",$w("#txtLength").value)
}
$w("#datSearch").setFilter(filter);

Works! Thanks! :slight_smile: