dataset Filter code issues

Hi Everyone, please, I need hep wit this piece of code. The filter should run through dataset and give the result of the product matching category, name, and capacity(Numbers). When I go on find product the repeater disappears but it works when I delete this line from the code .contains( “capacity” , $w( ’ #dropdown3 ’ ).value)) !

  • CODE
    export function findProduct_click(event, $w) {
    $w( “#dataset1” ).setFilter(wixData.filter()
    .contains( “category” , $w( ‘#dropdown1’ ).value)
    .contains( “displayName” , $w( ‘#dropdown2’ ).value)
    .contains( “capacity” , $w( ‘#dropdown3’ ).value))
    .then( () => {
    getItems()
    .then( (results) => {
    $w( ‘#repeater3’ ).data = results.items;

      }); 
    

    });
    }

function getItems() {
return $w( “#dataset1” ).getItems( 0 , 1000 )
.then( (result) => {
return result;
});
}

Hi AMINE,
instead of using .contains().contains().contains()
look here to work with .and()

kind regards
Kristof.

Sorry Kristof but I am new to coding and I don’t understand what to replace exactly to make this work. Can you please help me more ? Thanks

Oke i’ll correct it how i think it should work.

exportfunction findProduct_click(event, $w) {    $w("#dataset1").setFilter(wixData.filter()            .contains("category", $w('#dropdown1').value)            .and(wixdata.filter().contains("displayName", $w('#dropdown2').value))
.and(wixData.filter()
.contains("capacity", $w('#dropdown3').value)))
.then( () => {
getItems()
.then( (results) => {
$w('#repeater3').data = results.items;
});    
});}
function getItems() {
return $w("#dataset1").getItems(0, 1000)
then( (result) => {
return result;
    });
    }

i think this should work.

kind regards
Kristof.

Thank you very much Kristof, unfortunately the new code did not fix it. I believe there is a problem with the code being able to extract/read the info from “capacity” dropdown (maybe because it’s numbers) ? I don’t get any error message on debug, the repeater just disappears. Please bare with me :slight_smile:

Hi Amine,
if the dropdown items are manualy added you might need to take a look at the value field of your items.
the label can be capital letters, the value will start with a lowercase letter
and the dropdown value takes the value instead of the label

kind regards
Kristof.

Hi Kristof, the field value from the dataset is totally correct and matching with the respective code part. I think the problem goes beyond that also.

I thought maybe I show you the entire code. How I want it to work : there are 3 dependant dropdown menus, 2 are disabled onReady. When first dropdown gets a value second is enabled. Same goes for third which is dependant on second dropdown. Once all values are set, user clicks on Find and the respective filtered product will be extracted from dataset and displayed on the repeater. Now everything works fine, until the third dropdown which contains numbers on my dataset seems to “refuse” giving its value to the filter code (my own assumption). FYI : When I take out the piece of code for the third dropdown everything works fine and products are found. Code :

import wixData from ‘wix-data’ ;

$w.onReady( function () {

uniqueDropDown1(); 

});

function uniqueDropDown1() {

wixData.query( "PackagingSolutions" ) 

    .limit( 1000 ) 

    .find() 

    .then(results => { 

const uniqueTitles = getUniqueTitles(results.items);

        $w( "#dropdown1" ).options = buildOptions(uniqueTitles); 

    }); 

function getUniqueTitles(items) {

const titlesOnly = items.map(item => item.category);

return [… new Set(titlesOnly)];

} 

function buildOptions(uniqueList) {

return uniqueList.map(curr => {

return { label: curr, value: curr };

    }); 

} 

}

export function dropdown1_change(event) {

uniqueDropDown2(); 

$w( "#dropdown2" ).enable(); 

}

function uniqueDropDown2() {

wixData.query( "PackagingSolutions" ) 

    .contains( "category" , $w( "#dropdown1" ).value) 

    .limit( 1000 ) 

    .find() 

    .then(results => { 

const uniqueTitles = getUniqueTitles(results.items);

        $w( "#dropdown2" ).options = buildOptions(uniqueTitles); 

    }); 

function getUniqueTitles(items) {

const titlesOnly = items.map(item => item.displayName);

return [… new Set(titlesOnly)];

} 

function buildOptions(uniqueList) {

return uniqueList.map(curr => {

return { label: curr, value: curr };

    }); 

} 

}

export function dropdown2_change(event) {

uniqueDropDown3(); 

$w( "#dropdown3" ).enable(); 

}

function uniqueDropDown3() {

wixData.query( "PackagingSolutions" ) 

    .contains( "category" , $w( "#dropdown1" ).value) 

    .limit( 1000 ) 

    .find() 

    .then(results => { 

const uniqueTitles = getUniqueTitles(results.items);

        $w( "#dropdown3" ).options = buildOptions(uniqueTitles); 

    }); 

function getUniqueTitles(items) {

const titlesOnly = items.map(item => String(item.capacity));

return [… new Set(titlesOnly)];

} 

function buildOptions(uniqueList) {

return uniqueList.map(curr => {

return { label: curr, value: curr };

    }); 

} 

}

export function findProduct_click(event, $w) {
$w( “#dataset1” ).setFilter(wixData.filter().contains( “category” , $w( ‘#dropdown1’ ).value).and(wixData.filter().contains( “displayName” , $w( ‘#dropdown2’ ).value)).and(wixData.filter()
.contains( “capacity” , $w( ‘#dropdown3’ ).value)))

    .then(() => { 
        getItems() 
            .then((results) => { 
                $w( '#repeater3' ).data = results.items; 
            }); 
    }); 

}

function getItems() {
return $w( “#dataset1” ).getItems( 0 , 1000 )
.then((result) => {
return result;
});
}

Oow oke i think i see the problem then, since the value is a number in the dataset you have to give it a number instead of a string.
This part :
.contains( “capacity” , $w( ’ #dropdown3 ’ ).value)))

Should be:
.contains( “capacity” , parseInt($w( ’ #dropdown3 ’ ).value))))
It will give a yellow error since it asks for a second “radix” value (but it shoukd work without it)

Still nothing. Only difference the repeater does not disappear but it does not load any product. Filter did not pass through.

If you want i can take a look at it if you make me a contributor of your website (you can make a copy and make me contributor of that if you want to be sure nothing happends to the main website) since i am no moderator i cannot look at it with just a link :slight_smile:

kristof.print3d@gmail.com

kind regards
Kristof

Thank you very much!! That’s really kind of you. Invite is on the way.

No problem, on what page do i need to look? there are alot of pages :smiley:

@volkaertskristof On Packaging Solutions :')

@ayadiamne
hi mate, i think i found it (or it works for me anyway)
if you go to your click function where you filter, instead of using the .contains, use the .eq for the 3rd dropdown.

i didn’t save since you where in it, didn’t want to ruin your work :slight_smile:

kind regards
Kristof

So .contains( “capacity” , parseInt($w( ‘#dropdown3’ ).value))))
will be:
.eq( “capacity” , parseInt($w( ‘#dropdown3’ ).value))))

Hi Kristof, strangely does not work for me. I did exactly what you said. Would you mind saving it if it worked for you ? Thank you very much for your help :slight_smile:

I’m at work atm but i will later today :slight_smile:

Anime, to add here whilst Kristof is away and can’t help at the moment.

Note that the eq() function won’t match if you are using two different types.
https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html#eq

It only matches values of the same type. For example, a number value stored as a String type does not match the same number stored as a Number type.

As you are using parseInt here, check what you are using with your dropdown and dataset fields otherwise you are trying to match a number to a string or a string to a number for example.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

With Kristof code sample try removing the parseInt from the eq line and see if that works.

.eq("capacity", parseInt($w('#dropdown3').value))))
//will be//
.eq("capacity", ($w('#dropdown3').value))))

If you are getting the radix error in the yellow triangle on this line, you can look at trying using Number() instead of parseInt()

See here for more info on the differences between them in this old Stack post here.

If none of the above works for you, then please wait until Kristof can get back onto your site to help you with it further. :+1:

It worked!!! I changed parseInt with Number and it worked like a charm! I cannot thank you enough guys! I love this forum.