Search.../

setFilter( )

Sets the dataset filter.

Description

The setFilter() function filters out items that don't match the filter criteria.

The WixDataFilter object is created by calling the filter() function on wixData and chaining one or more of the following Wix Data filter functions.

Setting a dataset's filter overrides existing filters that were previously set using the setFilter() function or using the Dataset Settings panel in the Editor.

To clear a dataset's current filter, call setFilter() and pass it an empty filter. You create an empty filter by calling the filter() function without chaining any of the additional filter functions mentioned above.

Calling setFilter() on a write-only dataset causes an error.

You will need to import wix-data in order to create a WixDataFilter object.

Notes:

  • When using the and, not, or or functions, be sure to pass them a WixDataFilter object created by calling the filter() function and not a WixDataQuery object.
  • When using a read-write dataset and linked input elements, calling setFilter() will save any changes made in the linked input elements.

Syntax

function setFilter(filter: WixDataFilter): Promise<void>

setFilter Parameters

NAME
TYPE
DESCRIPTION
filter

A wix-data filter object.

Returns

Fulfilled - When the filter has been set. Rejected - An error object.

Return Type:

Promise<void>

Related Content:

Was this helpful?

Set a dataset's filter

This example filters a dataset to only have items where the lastname field starts with "D" and the age field is greater or equal to 21.

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5$w("#myDataset").setFilter( wixData.filter()
6 .startsWith("lastName", "D")
7 .ge("age", "21")
8);
9
Build and set a dataset's filter

This example builds a filter based on certain conditions. It then uses the built filter to filter a dataset.

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5function filter(low, high) {
6 let filter = wixData.filter();
7
8 if(low) {
9 filter = filter.ge("price", low);
10 }
11
12 if(high) {
13 filter = filter.le("length", high);
14 }
15
16 $w("#myDataset").setFilter(filter);
17}
18
Set a dataset's filter

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5$w("#myDataset").setFilter( wixData.filter()
6 .startsWith("lastName", "D")
7 .ge("age", "21")
8)
9.then( () => {
10 console.log("Dataset is now filtered");
11} )
12.catch( (err) => {
13 console.log(err);
14} );
Clear a dataset's filters

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5$w("#myDataset").setFilter( wixData.filter() );
Set a dataset's filter with an or condition

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5$w("#myDataset").setFilter( wixData.filter()
6 .startsWith("lastName", "D")
7 .or(
8 wixData.filter()
9 .ge("age", "21")
10 )
11)
12.then( () => {
13 console.log("Dataset is now filtered");
14} )
15.catch( (err) => {
16 console.log(err);
17} );