# Repeater not filtering with code

**URL:** <https://forum.wixstudio.com/t/repeater-not-filtering-with-code/61674>\
**Category:** Ask the community\
**Tags:** code, cms, question, wix\
**Created:** [August 6, 2024, 1:54pm UTC](https://forum.wixstudio.com/t/repeater-not-filtering-with-code/61674 "2024-08-06T13:54:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amy\_Jones](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/amy_jones/32/81451_2.png) [@Amy\_Jones](https://forum.wixstudio.com/u/Amy_Jones)\
**Post date:** [August 6, 2024, 1:54pm UTC](https://forum.wixstudio.com/t/repeater-not-filtering-with-code/61674/1 "2024-08-06T13:54:43Z")

</div>

**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:

```javascript
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;
	
 
}

```

---

<div class="post-metadata">

**Author:** ![noahlovell](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/noahlovell/32/118920_2.png) [@noahlovell](https://forum.wixstudio.com/u/noahlovell)\
**Post date:** [August 6, 2024, 2:13pm UTC](https://forum.wixstudio.com/t/repeater-not-filtering-with-code/61674/2 "2024-08-06T14:13:35Z")

</div>

Welcome to Wix 👋

Let’s see where we can get 🙂

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 💪

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](https://support.wix.com/en/article/cms-formerly-content-manager-adding-filters-and-sorts-to-datasets-to-control-live-site-content)

---

<div class="post-metadata">

**Author:** ![Amy\_Jones](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/amy_jones/32/81451_2.png) [@Amy\_Jones](https://forum.wixstudio.com/u/Amy_Jones)\
**Post date:** [August 6, 2024, 2:22pm UTC](https://forum.wixstudio.com/t/repeater-not-filtering-with-code/61674/3 "2024-08-06T14:22:38Z")

</div>

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](https://us1.discourse-cdn.com/wix/original/3X/5/0/50a15a7ae793e11ba580047b52770e8fb58be817.png)

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.

---

<div class="post-metadata">

**Author:** ![noahlovell](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/noahlovell/32/118920_2.png) [@noahlovell](https://forum.wixstudio.com/u/noahlovell)\
**Post date:** [August 6, 2024, 2:30pm UTC](https://forum.wixstudio.com/t/repeater-not-filtering-with-code/61674/4 "2024-08-06T14:30:42Z")

</div>

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](https://us1.discourse-cdn.com/wix/original/3X/e/2/e2e322fe16ca9a9338319edc9d4489fcc8f055f0.png)

---

<div class="post-metadata">

**Author:** ![Amy\_Jones](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/amy_jones/32/81451_2.png) [@Amy\_Jones](https://forum.wixstudio.com/u/Amy_Jones)\
**Post date:** [August 6, 2024, 2:36pm UTC](https://forum.wixstudio.com/t/repeater-not-filtering-with-code/61674/5 "2024-08-06T14:36:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Amy\_Jones](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/amy_jones/32/81451_2.png) [@Amy\_Jones](https://forum.wixstudio.com/u/Amy_Jones)\
**Post date:** [August 6, 2024, 2:49pm UTC](https://forum.wixstudio.com/t/repeater-not-filtering-with-code/61674/6 "2024-08-06T14:49:59Z")

</div>

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](http://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:)

---

<div class="post-metadata">

**Author:** ![noahlovell](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/noahlovell/32/118920_2.png) [@noahlovell](https://forum.wixstudio.com/u/noahlovell)\
**Post date:** [August 6, 2024, 4:37pm UTC](https://forum.wixstudio.com/t/repeater-not-filtering-with-code/61674/7 "2024-08-06T16:37:00Z")

</div>

> [@Amy\_Jones](#):
>
> I thought about that but didn’t know how to filter the 2nd dropdown (Lake) based on the State selected in the first dropdown.

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 🤔

Interested to hear how you get on 💪
