Multiple Filters Mode Sample

The multiple filter support within Spry can be switched between "and" and "or" mode. In "and" mode, the default, a row will only be added to the data set if all active filters return the row. If one filter returns a null, then the row will be filtered out. In "or" mode, if one of the active filters returns a row, that row will be added to the data set. If none of the filters return a row, then the row is filtered out.

In both modes, if there are no active filters, all rows will be added to the data set.

Here is a working example that allows you to switch between filter modes:

Show only donuts with the following toppings:

TypeBatterTopping
{name}{batters/batter}{topping}


When running the example above in "and" mode, notice how any time you click more than one checkbox, no data shows up. That is because each row of the data set only contains a single type of topping, which means that at least one of the filters installed will return a null object instead of a row. If you switch the example into "or" mode, notice how it shows only entries that have toppings that are checked. This is because in "or" mode only it is only necessary for one of the active filters to return a row for it to be added to the data set.

The filter functions used in the example above look like this:

function ffNone(ds, row, index){ return (row.topping == "None") ? row : null; };
function ffGlazed(ds, row, index){ return (row.topping == "Glazed") ? row : null; };
function ffSugar(ds, row, index){ return (row.topping == "Sugar") ? row : null; };
function ffPowdered(ds, row, index){ return (row.topping == "Powdered Sugar") ? row : null; };
function ffSprinkles(ds, row, index){ return (row.topping == "Chocolate with Sprinkles") ? row : null; };
function ffChocolate(ds, row, index){ return (row.topping == "Chocolate") ? row : null; };
function ffMaple(ds, row, index){ return (row.topping == "Maple") ? row : null; };

Checking a checkbox in the example above causes one of the functions listed above to be passed into a call to dsDonuts.addFilter(). For more info on adding/removing filters, check out the Multiple Filters Sample.

To switch filter modes, simply call the setFilterMode() function with a value of "and" or "or":

<script type="text/javascript">
<!--

var dsDonuts = new Spry.Data.XMLDataSet("../../data/donuts.xml", "/items/item", { subPaths: "topping" });

...

// Switch into "or" filtering mode.

dsDonuts.setFilterMode("or");

...

// Switch back into "and" filtering mode.

dsDonuts.setFilterMode("and");

...
-->
</script>

The call to setFilterMode() in the code

<script type="text/javascript">
<!--

var dsDonuts = new Spry.Data.XMLDataSet("../../data/donuts.xml", "/items/item", { subPaths: "topping" });

...

// Switch into "or" filtering mode and force the data set
// to re-apply all active filters.

dsDonuts.setFilterMode("or", true);

...

// Switch back into "and" filtering mode and force the
// data set to re-apply all active filters.

dsDonuts.setFilterMode("and", true);

...
-->
</script>

above, places the multi-filter support within dsDonuts in "or" mode. The next time a filter is added to the data set, the "or" mode will be used while running each filter to figure whether or not the row should be added to the data set, or filtered out.

If you would like to change the filter mode and immediately re-filter the data, simply pass a true value as the 2nd argument: