Question:
Hello everyone
Here is my problem: (in fact I have 2 pbs).
1/ By checking checkboxes, I gather, by concatenating them, several keywords in the same field just separated by a space (for example colors, or power, or category, etc …) it gives for example this in my field named for the example “Coloris”: Blue Red Green Yellow
I would like, if I click again on the checkbox “Yellow”, that the keyword “Yellow” disappears from my “Coloris” field: and therefore: Blue Red Green
2/ Then I perform a filter of my “Articles” database according to the colors that I have chosen (Blue Red Green).
Currently when I do my search, it takes me Blue AND Red AND green: I would like, Blue OR Red OR Green.
A big thank you for your help.
Product:
Wix Editor
What are you trying to achieve:
I want to extract data from several keywords
What have you already tried:
Here the code i use
$w(‘#MesArticles’).setFilter(wixData.filter()
.contains(“categorie”, $w(‘#Categorie’).value)
.contains(“Coloris”, $w(‘#Couleur1’).value)
.contains(“Coloris”, $w(‘#Couleur2’).value)
.contains(“Coloris”, $w(‘#Couleur3’).value)
.eq(“enStock”, true)
.ge(“prix”, PrixDep)
.le(“prix”, PrixFin))
.then(result)
1/ instead of turning the values into a string and then analyzing that string, it would be a lot simpler to keep them as an array and simply remove the value.
Something like:
// colors = ['blue', 'red', 'green']
function selectColor(color) {
const index = colors.indexOf(color) // -1 if not in array
if (index === -1) colors.push(color) // Add color
else colors.splice(index, 1) // Remove color
}
// Example, if colors were selected by buttons for instance
$w('Couleur1').onClick(selectColor('red'))
$w('Couleur2').onClick(selectColor('blue'))
$w('Couleur3').onClick(selectColor('green'))
2/ As for OR in WixDataFilters, it is a bit bothersome but can be done kinda like this:
const baseFilter = wixData.filter()
.contains('categorie', $w('#Categorie').value)
.eq('enStock', true)
.ge('prix', PrixDep).le('prix', PrixFin)
// Whatever custom functionality you use to determine which colors to filter by:
const filter = baseFilter.contains('Coloris', 'red')
.or(baseFilter.contains('Coloris', 'green'))
.or(baseFilter.contains('Coloris', 'blue'))
Thank you for your answer DeanAyalon.
I’m starting to see a solution thanks to you, but I’m still a little lost.
Sorry, but i don’t really understand 2 points :
1°/ How does he know to do the filter in my “Articles” Database? with :
const baseFilter = wixData.filter()
2°/: In fact, when i save a new article in my database, i click on “blue”, “red”, “green”, etc, in a field called 'Coloris". And then i user a filter from this field “coloris”. How do i use your function to save the field “Coloris”. (By adding or removing keywords)?
Thank you.
In my table “Articles”, the field called “Coloris” looks like this : “Rouge Bleu vert”
That is the reason why i would like to remove the color by clicking again on “Rouge” or “Bleu” or “Vert”.
On my page, the clients can choose 3 colors which will be used to do the filter.
- Field1 : Coloris1
- Field2 : Coloris2
- Field3 : Coloris3
Here the code i use to filter when i clik on the “Afficher” Button:
$w(‘#Afficher’).onClick((event) => {
let PrixDep
let PrixFin
PrixDep = Number($w('#Prixdepart').value)
PrixFin = Number($w('#PrixFin').value)
const baseFilter = wixData.filter()
.contains("categorie", $w('#NomSearch').text)
.eq("enStock", true)
.ge("prix", PrixDep)
.le("prix", PrixFin)
const filter = baseFilter.contains('Coloris', $w('#Couleur1').value)
.or(baseFilter.contains('Coloris', $w('#Couleur2').value))
.or(baseFilter.contains('Coloris',$w('#Couleur3').value))
})
1º/ I have only created the filter, I have not applied it
$w('#MesArticles').setFilter(filter)
The setFilter function takes WixDataFilter object as an argument, you simply created it right there when calling the function, while I saved it, ‘merged’ it multiple times and created a compound filter - to be applied later
2º/ Is this a CheckboxGroup element?
If so, it already tracks what’s selected, so you don’t need to save anything to a variable
$w('#checkboxGroup').value // ['red', 'green'] - only selected values
You can iterate that array to create your filter accordingly
1 Like
Nice!! It’s OK. Effectively, it was so simple to do it with a CheckboxGroup element!!
And filter is ok!!!
Thank you very much 