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.
- and: See note below.
- ascending
- between
- contains
- descending
- endsWith
- eq
- ge
- gt
- hasAll
- hasSome
- isEmpty
- isNotEmpty
- le
- lt
- ne
- not: See note below.
- or: See note below.
- startsWith
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 thefilter()
function and not aWixDataQuery
object.- When using a read-write dataset and linked input elements, calling any of the following functions will save any changes made in the linked input elements.
Syntax
function setFilter(filter: WixDataFilter): Promise<void>
setFilter Parameters
NAME
TYPE
DESCRIPTION
Returns
Fulfilled - When the filter has been set. Rejected - An error object.
Return Type:
Related Content:
Was this helpful?
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.
1import wixData from 'wix-data';23// ...45$w("#myDataset").setFilter( wixData.filter()6 .startsWith("lastName", "D")7 .ge("age", "21")8);9
This example builds a filter based on certain conditions. It then uses the built filter to filter a dataset.
1import wixData from 'wix-data';23// ...45function filter(low, high) {6 let filter = wixData.filter();78 if(low) {9 filter = filter.ge("price", low);10 }1112 if(high) {13 filter = filter.le("length", high);14 }1516 $w("#myDataset").setFilter(filter);17}18
1import wixData from 'wix-data';23// ...45$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} );
1import wixData from 'wix-data';23// ...45$w("#myDataset").setFilter( wixData.filter() );
1import wixData from 'wix-data';23// ...45$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} );