Hi,
I have 2 dropdown boxes that currently query the contents of specific fields in a dataset and if the values are equal then results are returned. This is all working fine but I have now added more fields in my dataset that I also need to query.
I have ‘Region1’ and ‘Region2’ as fields in my dataset. These will be 2 different regions eg. ‘London’ and ‘New York’ so if I need to filter by either ‘London’ OR ‘New York’ (depending on dropdown selection), how do I check if Region1 OR Region2 are equal to the value in my dropdown? My current code (simplified) is…
if ($w("#ddRegion").value) {
$w("#datasetISW").setFilter(wixData.filter()
.eq("region1", $w("#ddRegion").value)
.then(() => {
console.log("Dataset is now filtered by", $w("#ddRegion").value);
let count = $w("#datasetISW").getTotalCount();
countFunction(count)
})
.catch((err) => {
console.log(err);
});
}
but if i add .eq(“region2”, $w(“#ddRegion”).value) as well then it will be looking to make sure both fields have a value equal to my dropdown selection which is not what I want.
Any help would be much appreciated, thank you.
Rachel
Hi,
please see the below example:
wixData.query("myCollection")
.lt("age", 25)
.or(
wixData.query("myCollection") .gt("age", 65)
)
.find()
.then( (results) => {...
the full reference documentation can be found here:
good luck!
Shlomi
Thank you, Shlomi!
For reference, I used the following and it works perfectly…
$w("#datasetISW").setFilter(wixData.filter()
.eq("available", "1")
.eq("region", $w("#ddRegion").value)
.or(
wixData.filter()
.eq("region2", $w("#ddRegion").value)
))
.then(() => {...
Sorry, just one more thing!
I have 2 dropdowns… one of which queries 2 dataset fields and the other queries 5 fields so I am covering all eventualities! I just want to make sure this is the correct way of doing it or if there is a more efficient way?
$w("#datasetISW").setFilter(wixData.filter()
.eq("available", "1")
.eq("region", $w("#ddRegion").value)
.eq("specialism", $w("#ddSpecialism").value)
.or(
wixData.filter()
.eq("available", "1")
.eq("region2", $w("#ddRegion").value)
.eq("specialism", $w("#ddSpecialism").value)
)
.or(
wixData.filter()
.eq("available", "1")
.eq("region", $w("#ddRegion").value)
.eq("specialism2", $w("#ddSpecialism").value)
)
.or(
wixData.filter()
.eq("available", "1")
.eq("region", $w("#ddRegion").value)
.eq("specialism3", $w("#ddSpecialism").value)
)
.or(
wixData.filter()
.eq("available", "1")
.eq("region", $w("#ddRegion").value)
.eq("specialism4", $w("#ddSpecialism").value)
)
.or(
wixData.filter()
.eq("available", "1")
.eq("region", $w("#ddRegion").value)
.eq("specialism5", $w("#ddSpecialism").value)
)
.or(
wixData.filter()
.eq("available", "1")
.eq("region2", $w("#ddRegion").value)
.eq("specialism2", $w("#ddSpecialism").value)
)
.or(
wixData.filter()
.eq("available", "1")
.eq("region2", $w("#ddRegion").value)
.eq("specialism3", $w("#ddSpecialism").value)
)
.or(
wixData.filter()
.eq("available", "1")
.eq("region2", $w("#ddRegion").value)
.eq("specialism3", $w("#ddSpecialism").value)
)
.or(
wixData.filter()
.eq("available", "1")
.eq("region2", $w("#ddRegion").value)
.eq("specialism5", $w("#ddSpecialism").value)
))
.then(() => {...
Hi,
something smells wrong in your solution
i think something in the choices you have made in the page and db modeling is the cause.
can you please share the business use case / user story?
Shlomi
My dataset contains a list of people who are available by region and specialism.
I have 2 dropdowns… one that is populated with regions and the other with specialisms.
They have an ID of #ddRegion and #ddSpecialism .
Users should be able to search by region AND/OR specialism (AND available must = 1)
Search results are based on who is available in (up to) 2 regions and (up to) 5 specialisms therefore, in my dataset I have 2 fields for region ( region and region1 ) and 5 fields for specialism ( specialism , specialism2 , specialism3 , specialism4 and specialism5 )
So I need to see results for all fields equal to what is selected in the dropdown(s)
eg.
A user searches for someone available in ‘London’ with a specialism of ‘PAMS Assessment’
I need to search fields region AND r egion1 for ‘London’ and fields specialism , specialism2 , specialism3 , specialism4 and specialism5 for ‘PAMS Assessment’ because the person (in the dataset) may be available in 2 regions and up to 5 specialisms, therefore I need to check region with all the specialism fields as well as region2.
I hope that makes senes! Thank you
Hi Rachel,
it seems that currently your way is the best way to go, this is however a special use case. we are going to release a new feature called multi-ref soon in which instead of having 2 columns or 5 columns you can reference other tables multiple times.
in your case, regions will be a separate collection and so will be specialism. it will simplify the query
i will raise your use case with the relevant team in Wix,
Shlomi
Thank you for clearing that up Shlomi. I’m suprised my scenario is unique, I thought it would be quite common to want to search multiple fields? I will keep an eye out for when the new feature is released
@shlomsh For some reason I am not wrapping my head around the multi-ref fields. In particular as in case being discussed here. How would a search of a multi-ref field work? I have actor A who has four movies in multi-field. I am sorting according to that four movie field (my drop down would have move 1, movie 2, movie 3, and movie 4 ???) and selecting any one of those 4 in drop down would route search thru that multi-ref field and kick out actor A if so desired?
@jeff-haskins
Would you not just use something similar to this…
It will check if any of the movie reference fields (movie1, movie2, movie3, movie4) are equal to the value in your dropdown (ddMovies) and if so return your actor?
$w("#datasetMovies").setFilter(wixData.filter()
.eq("movie1", $w("#ddMovies").value)
.or(wixData.filter()
.eq("movie2", $w("#ddMovies").value))
.or(wixData.filter()
.eq("movie3", $w("#ddMovies").value))
.or(wixData.filter()
.eq("movie4", $w("#ddMovies").value))
)
.then(() => {...
any update on the multi-ref feature? I have a dataset to filter in a similar way, but up to 8 filters, so it is not really feasible to spell out all permutations.
thanks so much