Repeater not filtering with code

Question:
I’m new to Wix and trying to code my repeater based on 2 dropdowns added to the list page and cannot get it to filter the results when clicking the Search button

Product:
Wix Editor

Both the state and lakename are pulling from 2 databases (StateDB & LakeDB1) - this is working perfect. The repeater is linked to my CMS database called “import791” - Here is my code:

import wixData from 'wix-data';


$w.onReady(function () {
    initPage();
});

async function initPage() {
    const StateLakes = {};

    wixData.query('StateDB')
        .ascending('title')
        .find()
        .then(results => {
            const stateOptions = results.items.map(state => ({ label: state.title, value: state._id }));
            $w('#stateDropdown').options = stateOptions;
        });

    $w('#stateDropdown').onChange(async () => {
        let hasLakes = false;
        const selectedState = $w('#stateDropdown').value;

        if (StateLakes[selectedState]) {
            hasLakes = true;
            $w('#lakeDropdown').options = StateLakes[selectedState];
        } else {
            const results = await wixData.query('LakeDB1')
                .eq('state', selectedState)
                .ascending('title')
                .find()

            if (results.length > 0) {
                hasLakes = true;
                const lakeOptions = results.items.map(lake => ({ label: lake.title, value: lake._id }));
                $w('#lakeDropdown').options = lakeOptions;
                StateLakes[selectedState] = lakeOptions;
            }
        }

        if (hasLakes) {
            $w('#lakeDropdown').selectedIndex = undefined;
            $w('#lakeDropdown').expand();
            $w('#searchButton').disable();
        } else {
            $w('#lakeDropdown').collapse();
            $w('#searchButton').enable();
        }

    });
    let dDown = $w('#lakeDropdown');
    $w('#lakeDropdown').onChange(() => {
        $w('#searchButton').enable();
        let index = $w('#lakeDropdown').selectedIndex; // Get the selected index
        let itemOptions = $w('#lakeDropdown').options[index] // get the selected item
        let itemLabel = itemOptions.label; // grab the label of selected object
        let itemValue = itemOptions.value; // grab the value of selected object

        console.log("Label = " + itemLabel) // the label of the selected dropdown item 
        console.log("Value = " + itemValue) // the value of the selected dropdown item

    });

}


export function searchButton_click(event) {
	search();
}

function search() {
	 const selectedState = $w('#stateDropdown').value;
	 	let index = $w('#stateDropdown').selectedIndex; // Get the selected index
	    let itemOptions = $w('#stateDropdown').options[index] // get the selected item
        let itemLabel = itemOptions.label; // grab the label of selected object
        let itemValue = itemOptions.value; // grab the value of selected object

        console.log("Label = " + itemLabel) // the label of the selected dropdown item 
        console.log("Value = " + itemValue) // the value of the selected dropdown item
	
	 wixData.query("import791")
	.contains("state", String($w("#stateDropdown").value))
	.and(wixData.query("import791").contains("lakeName", String($w("#lakeDropdown").value)))
 
	.find()
	.then(results => {
		$w("#listRepeater").data = results.items;
			
	});

	 
}

export function resetButton_click(event) {
	$w("#dynamicDataset").setFilter(wixData.filter())
	
	$w("#stateDropdown").value = undefined;
	$w("#lakeDropdown").value = undefined;
	
 
}

Welcome to Wix :wave:

Let’s see where we can get :slight_smile:

First up, I see you mentioned that the repeater is connected to the database import791 although I don’t see the connection made via code - is this currently done via a dataset?

It’s normally best practice to go complete code or complete dataset, but it can be mixed and matched in certain scenarios.

I’d also recommend going with event handlers using the dot method (can’t remember the actual name) vs exported event handlers.


Couple of extra notes:

  • Although the lakeDropdown is changing value visually, it’s not altering data
  • Not sure on the specifics of the project, but it’s likely best to load all the data up front and then work from it there on, rather than querying when stateDropdown changes for example.

Hope this helps as a starter :muscle:

Also going to share a link about creating filters with datasets - CMS: Adding Filters and Sorts to Datasets to Control Live Site Content | Help Center | Wix.com

I’m new at coding in Wix - thank you for your help. The repeater is connected to the database through the Connect to CMS link on the repeater box itself:

screenshot_195

I could probably write the first portion against the same database no problem. I can look into event handlers but not so sure I can catch on. Not so sure about what you mean on the 2nd bullet point.

If I follow correctly:

You have 3 collections:

  1. StateDB - lists states
  2. LakeDB1 - lists lakes
  3. import791 - lists information, and contains a column with information about states, and another columns about that item’s lakes

If the above is correct, it might even be possible without the need for code.

You’d likely end up with one collection, with all the information. It would have a column for your Lake and State.

For example:

Title Lake State
Location 1 Lake 1 State 1
Location 2 Lake 1 State 2
Location 3 Lake 2 State 1
Location 4 Lake 2 State 1
Location 5 Lake 2 State 2
Location 6 Lake 1 State 1

Then, you can connect your dropdown to the relevant column using the “Filter content” which will allow you to filter the content for the items

Screenshot 2024-08-06 at 15.29.55

I thought about that but didn’t know how to filter the 2nd dropdown (Lake) based on the State selected in the first dropdown.

If there is a way to filter the lake based on the state selected in the first dropdown, let me know. In the meantime, I’m going to recode to pull all from the same database (import791) - I also have 2 more questions to make sure I have this set up right:

#1 All of my products come from Printful and a database is automatically created by Wix Apps Collections under Stores/Products. I was unable to create another field in this database called states and/or lakes. That’s why I had to create a separate database (import791) that contains the info along with an image, etc. - Just seemed like a lot of work to do what I’m trying to do.

On the homepage, the user selects the State and Lake - is there a way to force their entry to flow to this new page with the repeater? See laketoads.com

I’m having to create this repeater because all my lakes were contained in categories (the way it’s working now) and now I’m at 200 categories and can’t create anymore.

Thank you - learning here:)

Ahhh okay - yeah, this’ll be a coded solution if this is a must.


I don’t personally have experience with Printful, so not sure on this one :thinking:

Interested to hear how you get on :muscle: