I am trying to filter a dataset to a collection and a price range. I have added a filter for the collection I want as well as a separate filter for the price range using a drop down but the filters are acting in a OR scenario instead of a AND scenario. I need it to remain in the same collection and find products in that price range. right now it puts in everything in the collection and all products within the price range.
With code you can do whatever you want:
https://www.wix.com/corvid/reference/wix-dataset/dataset/setfilter
Hi J.D.,
Iv been trying to do it with code but I cant get it to work. I am new to corvid so my java skills arent great but this is my code right now.
//-------------Imports-------------//
import wixData from ‘wix-data’ ;
import wixLocation from ‘wix-location’ ;
//-------------Page Setup-------------//
$w( "#dataset1" ).setFilter( wixData.filter()
.eq( "collections" , "Sofa" )
.le( "price" , $w( '#dropdown1' ).value)
);
I am getting an error saying "$w(…).setFilter is not a function. Can you please advise how to fix this.
You need to wrap it like this:
//the import lines come here....
$w.onReady(() => {
$w("#dataset1").onReady(() => {
//the code for the filter should be here
})
})
But since you’re filtering based on a dropdown value, you should run the filter when the dropdown value changes.
Put it inside an onChange event handler.
Thanks for the reply J.D. I just have two more quick questions that will solve all my issues that if you can answer would be great. First question is that I am trying to filter all products in a specific collection, in this case the collection “Sofa”. I’m not sure what the right way to reference the collection is. My second question is that once I added this filter, it stopped my forEachItem function from working. If I take the filter out it works again as intended but the filter is messing with it for some reason. Why is that?
Thanks for the help in advance!
//-------------Imports-------------//
import wixData from ‘wix-data’ ;
import wixLocation from ‘wix-location’ ;
//-------------Page Setup-------------//
$w.onReady( function () {
// TODO: write your page related code here…
$w( “#productImage” ).fitMode = “fit” ;
$w( "#dataset1" ).onReady( () => {
$w( '#dataset1' ).setFilter( wixData.filter()
.eq( "collecions" , "Sofa" )
.le( "price" , 1000 )
)
$w( '#relatedItemsRepeater' ).forEachItem( ($item, itemData, index) => {
console.log( "Here" )
$item( '#productImage' ).onClick(() => {
wixLocation.to(itemData.productPageUrl);
});
$item( '#productTitle' ).onClick(() => {
wixLocation.to(itemData.productPageUrl);
});
if ($item( “#productPrice” ).text === $item( “#productDPrice1” ).text)
{
$item( “#productDPrice1” ).collapse();
$item( “#productPrice” ).html = <span style= 'font-size: 20px;' > ${$item( "#productPrice" ).text}</span>
;$w( “#productPrice” )
}
else
{
$item( “#productPrice” ).html = <span style= 'font-size: 20px;text-decoration: line-through' > ${$item( "#productPrice" ).text}</span>
;
$item( “#productDPrice1” ).html = <span style= 'color:#AA0000;font-size:20px;font-weight:bold;' > ${$item( "#productDPrice1" ).text}</span>
;
}
});
});
});
-
I’m not sure I got you. If you a have a field “collections” in this Products collection then filter by it using .eq (if it only contains one collection) or .contains() if it contains an array of collections (or include() if it a reference field, but then you’ll have to use the reference id).
-
In your case, I think it makes more sense to put the repeater.forEachItem in the .then() part of the filter as you want it to run after filtering:
$w('#dataset1').setFilter( wixData.filter()
.eq("collecions", "Sofa")
.le("price", 1000) )
.then(() => {
//put the forEach here
})
The .then statement worked perfectly! thanks for that. Unfortunately, I tried both the contains function and the include function but it still isn’t working. The picture below shows the field that I am trying to reference. It is directly out of the default products database. The issue if that since its a multi-reference field it doesn’t filter as a usual text or numerical field. Also .include only works on a query. Do you think this would be more effective as a query instead of a filter. I have noticed that filtering has long loading times.
this is my current updated code:
//-------------Imports-------------//
import wixData from ‘wix-data’ ;
import wixLocation from ‘wix-location’ ;
//-------------Page Setup-------------//
$w.onReady( function () {
// TODO: write your page related code here…
$w( “#productImage” ).fitMode = “fit” ;
$w( "#dataset1" ).onReady( () => {
$w( '#dataset1' ).setFilter( wixData.filter()
.contains( "collections" , "Sofa" )
// .le("price", 1000)
)
.then(() => {
$w( '#relatedItemsRepeater' ).forEachItem( ($item, itemData, index) => {
console.log( "Here" )
$item( '#productImage' ).onClick(() => {
wixLocation.to(itemData.productPageUrl);
});
$item( '#productTitle' ).onClick(() => {
wixLocation.to(itemData.productPageUrl);
});
if ($item( “#productPrice” ).text === $item( “#productDPrice1” ).text)
{
$item( “#productDPrice1” ).collapse();
$item( “#productPrice” ).html = <span style= 'font-size: 20px;' > ${$item( "#productPrice" ).text}</span>
;$w( “#productPrice” )
}
else
{
$item( “#productPrice” ).html = <span style= 'font-size: 20px;text-decoration: line-through' > ${$item( "#productPrice" ).text}</span>
;
$item( “#productDPrice1” ).html = <span style= 'color:#AA0000;font-size:20px;font-weight:bold;' > ${$item( "#productDPrice1" ).text}</span>
;
}
});
})
});
});
I don’t think you can use dataset.setFilter() on referenced fields.
But I might be wrong (as I almost never use datasets and prefer direct queries).
There were lots of questions regarding filters and referenced field. Try searching the forum. Or have a look here:
https://www.google.com/search?rlz=1C1GCEA_enIL903IL903&ei=oFFyX8e8DcLMgwfzoougDQ&q=wix+reference+field+filter&oq=wix+reference+field+filter&gs_lcp=CgZwc3ktYWIQAzIICCEQFhAdEB4yCAghEBYQHRAeMggIIRAWEB0QHjIICCEQFhAdEB46AggAOgYIABAWEB46BAghEApQ5RFY-DJgpTRoAXAAeACAAYIBiAGbCZIBBDAuMTCYAQCgAQGqAQdnd3Mtd2l6wAEB&sclient=psy-ab&ved=0ahUKEwjHjIzw4YzsAhVC5uAKHXPRAtQQ4dUDCA0&uact=5