Multiple Filters Sample

This page demonstrates how to use the multiple filter support for data sets.

Here is a working example that uses multiple filters.


Before starting, you must make sure that you include SpryDataExtensions.js *after* SpryData.js:

<script language="JavaScript" type="text/javascript" src="../../includes/xpath.js"></script>
<script language="JavaScript" type="text/javascript" src="../../includes/SpryData.js"></script>
<script language="JavaScript" type="text/javascript" src="../../includes/SpryDataExtensions.js"></script>

This will add multiple filter support to all data sets.

Now you can define your data set and filter functions as you normally would:

<script type="text/javascript">

var dsStates = new Spry.Data.XMLDataSet("../../data/states/states.xml", "states/state");

function ffAH(ds, row, index){ var c =; return c >= 'A' && c <= 'H' ? null : row; };
function ffIP(ds, row, index){ var c =; return c >= 'I' && c <= 'P' ? null : row; };
function ffQZ(ds, row, index){ var c =; return c >= 'Q' && c <= 'Z' ? null : row; };


The filter functions above filter out rows of data based on whether or not the first letter in the 'name' column falls within a certain range of letters. These are the same functions used to filter in the live sample above.

The important thing to remember is that if you are going to use multiple filters, DO NOT USE the filter() function on the data set. If you do, it will blow away any filters you have installed. Now, to add a filter, use the addFilterFunction:

dsStates.addFilter(ffAH, true);

The first argument to addFilter is the filter function you want to add, the 2nd argument is optional, if true, it tells the data set to apply all filters after the filter is added. If false or undefined, it simply adds the filter function to its internal list and does nothing else. This is sometimes handy if you want to add a bunch of filters and then apply them after they have all been added:

dsStates.addFilter(ffAH);       // Add.
dsStates.addFilter(ffIP);       // Add.
dsStates.addFilter(ffQZ, true); // Add and then filter.

To remove a filter, you call removeFilter(). Like addFilter, the optional 2nd argument determines whether or not the data set will apply the set of existing filters immediately or not:

dsStates.removeFilter(ffAH);       // Remove.
dsStates.removeFilter(ffIP);       // Remove.
dsStates.removeFilter(ffQZ, true); // Remove and then filter.

You can also remove all filters in a single call with removeAllFilters(). removeAllFilters() also takes an optional argument which tells the dataset whether or not it should unapply the last set of filters. This is useful if you want to remove any active filters, add some new filters and then apply the filters:

dsStates.removeAllFilters();      // Reset our filter list so its empty.
dsStates.addFilter(ffIP, true);   // Add a filter and apply it.


dsStates.removeAllFilters(true);  // Reset our filter list and restore all data.

To re-run the current set of active filters, you can use the applyFilters() function. This is sometimes useful if you want to set a bunch of filters and then apply them at a later point in time:

dsStates.addFilter(ffAH); // Add.
dsStates.addFilter(ffIP); // Add.
dsStates.addFilter(ffQZ); // Add.


dsStates.applyFilters();  // Filter the data.