Hi All,
Thought to share a solution to how to add a Search filter that is based on a user checking more than one checkbox (and based on that returning a filtered Dataset):
A SOLUTION
1. Add a “Search” field to the Collection you’d like to filter. (The next point will cover how to add values for each row)
2. Add an “OnAfterSave” event handler to the page where you have your form elements databound to your dataset (example: the Form where you collect information or input information pertaining to the information to be searched):
import wixData from 'wix-data';
$w.onReady(function () {
// register event
$w("#dataset1").onAfterSave( (itemBeforeSave, itemAfterSave) => {
console.log("After save - start");
let search = "";
if(itemAfterSave.qualitativeResearch) { search += "qualitativeResearch,"; }
if(itemAfterSave.quantitativeResearch) { search += "quantitativeResearch,"; }
if(itemAfterSave.userExperience) { search += "userExperience,"; }
if(itemAfterSave.designResearch) { search += "designResearch,"; }
if(itemAfterSave.neuroscience) { search += "neuroscience,"; }
if(itemAfterSave.strategicPlanner) { search += "strategicPlanner,"; }
if(itemAfterSave.brandDevelopment) { search += "brandDevelopment,"; }
if(itemAfterSave.antropology) { search += "antropology"; }
// get row thru query method first. Then update. Doing it this way
// ensures that won't lose any other data for the other fields.
wixData.query("YOUR-COLLECTION")
.eq("_id", itemAfterSave._id)
.find()
.then( (results) => {
let item = results.items[0];
item.search = search;
wixData.update("YOUR-COLLECTION", item);
console.log("After save - complete");
} )
.catch( (err) => {
let errorMsg = err;
console.log("After save - error");
} );
} );
});
- Add the filtering logic to the Search page’s code-behind. Note, the key parts to the code below are setting the search values to an empty-string if they were not checked and then note the section with multiple “.contains” clauses:
export function search_click(event) {
$w('#spinner').show();
$w('#notFound').hide();
let textQualitativeResearch = $w('#chkQualitativeResearch').checked ? 'qualitativeResearch' : '' ;
let textQuantitativeResearch = $w('#chkQuantitativeResearch').checked ? 'quantitativeResearch' : '' ;
let textUserExperience = $w('#chkUserExperience').checked ? 'userExperience' : '' ;
let textDesignResearch = $w('#chkDesignResearch').checked ? 'designResearch' : '' ;
let textNeuroscience = $w('#chkNeuroscience').checked ? 'neuroscience' : '' ;
let textStrategicPlanner = $w('#chkStrategicPlanner').checked ? 'strategicPlanner' : '' ;
let textBrandDevelopment = $w('#chkBrandDevelopment').checked ? 'brandDevelopment' : '' ;
let textAnthropology = $w('#chkAnthropology').checked ? 'antropology' : '' ;
$w("#dynamicDataset").setFilter(wixData.filter()
.contains("search", textQualitativeResearch)
.contains("search", textQuantitativeResearch)
.contains("search", textUserExperience)
.contains("search", textDesignResearch)
.contains("search", textNeuroscience)
.contains("search", textStrategicPlanner)
.contains("search", textBrandDevelopment)
.contains("search", textAnthropology)
)
.then( (results) => {
console.log("Specialists are now filtered.");
if(!results){
$w('#notFound').show();
}
$w('#spinner').hide();
} )
.catch( (err) => {
console.log("Specialists filter error: " + err);
$w('#spinner').hide();
} );
}
Hope this helps.
Nick