# Unique List on Multiple Dropdown that are Linked Directly to Your Data Collection

**URL:** <https://forum.wixstudio.com/t/unique-list-on-multiple-dropdown-that-are-linked-directly-to-your-data-collection/9118>\
**Category:** Show and tell\
**Tags:** tips, wix-hotels, code, question\
**Created:** [February 18, 2021, 5:33pm UTC](https://forum.wixstudio.com/t/unique-list-on-multiple-dropdown-that-are-linked-directly-to-your-data-collection/9118 "2021-02-18T17:33:38Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![angirene7](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/angirene7/32/20920_2.png) [@angirene7](https://forum.wixstudio.com/u/angirene7)\
**Post date:** [February 18, 2021, 5:33pm UTC](https://forum.wixstudio.com/t/unique-list-on-multiple-dropdown-that-are-linked-directly-to-your-data-collection/9118/1 "2021-02-18T17:33:38Z")

</div>

Reading through the discussion posted by @Yisrael (wix) here: [https://www.wix.com/velo/forum/tips-tutorials-examples/example-remove-duplicates-from-connected-dropdown-options-using-distinct-query,](https://www.wix.com/velo/forum/tips-tutorials-examples/example-remove-duplicates-from-connected-dropdown-options-using-distinct-query,) his very helpful tips on using **distinct()** to build a unique dropdown list has helped to lean up my code tremendously. However, this function is only applicable when you do a **wixData.query** on information from your collection and not the dataset. So if you have set a “filter” function in your dataset that is connected to your dropdown, you won’t be able to use this function to generate another unique dropdown list for a second dropdown you’re building for your site.

So from the question of some of the comments from the original forum, I thought I’ll try out integrating multiple different codes to allow multiple dropdown that filter out the unique list of each other which I found difficult finding the answers from other examples given. So here you go:

**Example of Elements on the website**  
Dropdown A: List of Country; $w(‘#ListofCountryDropdown’)  
Dropdown B: States/Region; $w(‘#StatesRegionDropdown’)  
Dropdown C: Name of Hotels; $w(‘#NameofHotelDropdown’)

Collection Name (“Listing”) - _please click on the 3 dots next to the database created of your choice and select “Edit Setting” to find the Collection name. Please ensure that you copy the exact name in the setting else it won’t work._

Collection Layout - _if you open up your database, every column in that database, when you look into the properties of that column, will have a field name and field key. The Column Name that I’ve included here would be the field key._  
Column Name 1 = **listOfCountry**  
Column Name 2 = **statesRegion**  
Column Name 3 = **nameOfHotels**

**Sample Code**

import wixData from ‘wix-data’ ;

$w.onReady( function () {

function DropdownListUpdate() {  
let ListofCountry = $w( '# ListofCountryDropdown ’ ).value  
let StatesRegion = $w( '# StatesRegionDropdown ’ ).value  
let NameofHotel = $w( '# NameofHotelDropdown ’ ).value

let ListUpdate= wixData.query( " Listing " )  
if (ListofCountry) {ListUpdate= ListUpdate.eq( “listOfCountry” ,ListofCountry)}  
if (StatesRegion) {ListUpdate= ListUpdate.eq( “statesRegion” ,StatesRegion)}  
if (NameofHotel) {ListUpdate= ListUpdate.eq( “nameOfHotels” , NameofHotel)}

function Options(items) {  
**return** items.map (curr=\>{  
**return** {label:curr, value:curr}  
})  
}

function CountryDropdown() {  
ListUpdate  
.limit ( 1000 )  
.ascending( “listOfCountry” )  
.distinct( “listOfCountry” )  
.then(results =\> {  
let CountryList = Options(results.items);  
$w( '# ListofCountryDropdown ’ ).options = CountryList; })  
}  
CountryDropdown()

function StatesRegionDropdown() {  
ListUpdate  
.limit ( 1000 )  
.ascending( “statesRegion” )  
.distinct( “statesRegion” )  
.then(results =\> {  
let StatesRegionList = Options(results.items);  
$w( '# StatesRegionDropdown ’ ).options = StatesRegionList ; })  
}  
StatesRegionDropdown()

function HotelsDropdown() {  
ListUpdate  
.limit ( 1000 )  
.ascending( “nameOfHotels” )  
.distinct( “nameOfHotels” )  
.then(results =\> {  
let HotelList = Options(results.items);  
$w( '# NameofHotelDropdown ’ ).options = HotelList ; })  
}  
HotelsDropdown()  
}  
DropdownListUpdate()

$w( '# ListofCountryDropdown ’ ).onChange(()=\>{DropdownListUpdate(); })  
$w( '# StatesRegionDropdown ’ ).onChange(()=\>{DropdownListUpdate(); })  
$w( '# NameofHotelDropdown ’ ).onChange(()=\>{DropdownListUpdate(); })  
}

**Code Explanation**  
To run a **distinct()** function, you must perform a wixData.query. To run a wixData.query, you must first import wixData. So the first line of your code must be import wixData from “wix-data” ;

Then as always, if you want the function to work, you need on ready your function: $w.onReady( function () {}

Because you can only run the **distinct()** function using wixData.query which as I’ve said are connected to your collection where else all filter functions are connected to your dataset, hence why there is no filter function that can narrow down your query list to provide that multiple filter affect to your multiple dropdown. However, a query function also allows specific “filtering” ability by using the **eq()** or **contains()** function that will be the key to building this multiple filtering effect.

The first function: **DropdownListUpdate()** need to be run the moment the site start up to build up your first list of drop down. Hence why you will see the function being called after I’ve closed the code to the function. As the drop down list need to be refresh every time something is selected, you then see the **onChange** function that was set which basically say, on changes made to the drop down, update the information within the drop down to update the list within the drop down.

I’m also a rather beginner to wix code and have gotten this after multiple reading up of the API and testing the codes on my own website. It works perfectly on mine but if you have any issue putting this up for yourself, you can leave a comment and I’ll see what I can do to help.

I’m not working for wix, so please don’t expect an immediate response if you are leaving a comment. I’m just posting this because a lot of posting on this forum has helped me build my codes and I thought I can help others with this.

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [February 18, 2021, 6:22pm UTC](https://forum.wixstudio.com/t/unique-list-on-multiple-dropdown-that-are-linked-directly-to-your-data-collection/9118/2 "2021-02-18T18:22:25Z")

</div>

Well done Irene. Some more informations to this topic needed?

example from dropdownAPI…

```javascript
$w("#myDropdown").options = [
  {"label": "Who's on first!", "value": "first"},
  {"label": "What's on second", "value": "second"},
  {"label": "I Don't Know is on third", "value": "third"}
];

```

```javascript
var DATABASE = "YourCollectionNameHere"
var FieldToBeDistinct = "FieldIdToBeDistinct"

async function get_uniqueDropDown() {console.log("load_Unique-DropDown")
    let ARRAY = []
    let dropdownOptions = []
    let query = await wixData.query(DATABASE)
    
    query.distinct(FieldToBeDistinct)
    .then((results) => {console.log("Distinct-Results: ", results)
        let items = results.items
        for (var b = 0; b <items.length; b++) { 
           console.log("Item: ", items[b])
           //ARRAY.push({"_id": String(b), "item": items[b]})
	   dropdownOptions.push({"label": items[b], "value": items[b]})
        }
    })
    .then(()=>{
        $w('#myDropDown').options = dropdownOptions
    })
}

```

Of course this can be improved much more and is just an example.  
This code can be expanded to make it much more flexible and universal.

(Code not tested, but should normaly work) 😁

An upgraded version of this code-example you will find here in action…  
[https://www.media-junkie.com/pflegeservice](https://www.media-junkie.com/pflegeservice)

---

<div class="post-metadata">

**Author:** ![angirene7](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/angirene7/32/20920_2.png) [@angirene7](https://forum.wixstudio.com/u/angirene7)\
**Post date:** [February 18, 2021, 6:40pm UTC](https://forum.wixstudio.com/t/unique-list-on-multiple-dropdown-that-are-linked-directly-to-your-data-collection/9118/3 "2021-02-18T18:40:03Z")

</div>

Thanks for the comment. I don’t quite understand the codes you’ve placed in your comment or how does that work. 😄

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [February 18, 2021, 6:45pm UTC](https://forum.wixstudio.com/t/unique-list-on-multiple-dropdown-that-are-linked-directly-to-your-data-collection/9118/4 "2021-02-18T18:45:09Z")

</div>

@angirene7  
Then GO trough all the given POSTs in this “interactive-example”…  
[https://www.media-junkie.com/databases](https://www.media-junkie.com/databases)

And perhaps at the end you will understand more about it 😉  
Perhaps you should START with the original POSTs…

[https://www.wix.com/velo/forum/community-discussion/wix-storage-checkboxgroup/p-1/dl-5fcc17364ce20b002d0ffbdf-5fcc15f3a032ca0034d80c97-1](https://www.wix.com/velo/forum/community-discussion/wix-storage-checkboxgroup/p-1/dl-5fcc17364ce20b002d0ffbdf-5fcc15f3a032ca0034d80c97-1)

> **[From successful search to a dynamic page - no return possible after that - :(...](https://community.wix.com/velo/forum/coding-with-velo/from-successful-search-to-a-dynamic-page-no-return-possible-after-that-i-have-to-cry-please-support-me)**
>
> I have to make the search 3 levels filter 1 gender activates nationality and this activates availability. But as soon as I jump to a dynamic page I can't go back - all filters disappear because I have an on ready function. who can solve my problem -...

You will recognize your used “CODE” which you will find also in the original post.  
I just upgraded it a lot 😅.

---

<div class="post-metadata">

**Author:** ![angirene7](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/angirene7/32/20920_2.png) [@angirene7](https://forum.wixstudio.com/u/angirene7)\
**Post date:** [February 18, 2021, 11:35pm UTC](https://forum.wixstudio.com/t/unique-list-on-multiple-dropdown-that-are-linked-directly-to-your-data-collection/9118/5 "2021-02-18T23:35:28Z")

</div>

I’ll spend some time reading through this for sure. Always good to learn something. Thanks 😉

---

<div class="post-metadata">

**Author:** ![eulapro](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@eulapro](https://forum.wixstudio.com/u/eulapro)\
**Post date:** [April 1, 2023, 9:03pm UTC](https://forum.wixstudio.com/t/unique-list-on-multiple-dropdown-that-are-linked-directly-to-your-data-collection/9118/6 "2023-04-01T21:03:23Z")

</div>

hi, Irene I test the code is perfect but when I do a new search on the dropdown, its not allowed to do it, dropdown stock on the previous search, I have to reload the page then I can make a new search. let me know if you have any ideas why that happen, thank you

---

<div class="post-metadata">

**Author:** ![eulapro](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@eulapro](https://forum.wixstudio.com/u/eulapro)\
**Post date:** [April 1, 2023, 9:07pm UTC](https://forum.wixstudio.com/t/unique-list-on-multiple-dropdown-that-are-linked-directly-to-your-data-collection/9118/7 "2023-04-01T21:07:55Z")

</div>

hi, I Also have refresh button but still dropdown wont allowed me to do the new search until I reload the page  
Thank you

---

<div class="post-metadata">

**Author:** ![angirene7](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/angirene7/32/20920_2.png) [@angirene7](https://forum.wixstudio.com/u/angirene7)\
**Post date:** [April 2, 2023, 1:23pm UTC](https://forum.wixstudio.com/t/unique-list-on-multiple-dropdown-that-are-linked-directly-to-your-data-collection/9118/8 "2023-04-02T13:23:45Z")

</div>

I don’t know how you’ve set the search function in your page, but if you want to refresh the list, you can simply add a code that says:

$w(‘#RefreshButton’).onClick(()=\>{ DropdownListUpdate() })

---

<div class="post-metadata">

**Author:** ![angirene7](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/angirene7/32/20920_2.png) [@angirene7](https://forum.wixstudio.com/u/angirene7)\
**Post date:** [April 2, 2023, 1:29pm UTC](https://forum.wixstudio.com/t/unique-list-on-multiple-dropdown-that-are-linked-directly-to-your-data-collection/9118/9 "2023-04-02T13:29:03Z")

</div>

Again as mentioned above, I don’t really how you’re setting your search function, but assuming you’re looking a code that says:

$w( '# ListofCountryDropdown ’ ).onChange(()=\>{  
**your codes for the search** })

It should automatically make the new search upon selecting a new item from the dropdown list, no?

---

<div class="post-metadata">

**Author:** ![eulapro](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@eulapro](https://forum.wixstudio.com/u/eulapro)\
**Post date:** [April 2, 2023, 3:13pm UTC](https://forum.wixstudio.com/t/unique-list-on-multiple-dropdown-that-are-linked-directly-to-your-data-collection/9118/10 "2023-04-02T15:13:20Z")

</div>

Hi irene, no the dropdown wont expand after you do the first search, so i need to reload the page, thank you

---

<div class="post-metadata">

**Author:** ![eulapro](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@eulapro](https://forum.wixstudio.com/u/eulapro)\
**Post date:** [April 2, 2023, 3:14pm UTC](https://forum.wixstudio.com/t/unique-list-on-multiple-dropdown-that-are-linked-directly-to-your-data-collection/9118/11 "2023-04-02T15:14:40Z")

</div>

Hi irene let me try it  
thank you

---

<div class="post-metadata">

**Author:** ![eulapro](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@eulapro](https://forum.wixstudio.com/u/eulapro)\
**Post date:** [April 2, 2023, 3:25pm UTC](https://forum.wixstudio.com/t/unique-list-on-multiple-dropdown-that-are-linked-directly-to-your-data-collection/9118/12 "2023-04-02T15:25:24Z")

</div>

Hi Irene its working perfect now adding this code you said $w(‘#RefreshButton’).onClick(()=\>{ DropdownListUpdate() })

Thank you, very much  
can I follow your project some how?

---

<div class="post-metadata">

**Author:** ![angirene7](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/angirene7/32/20920_2.png) [@angirene7](https://forum.wixstudio.com/u/angirene7)\
**Post date:** [April 3, 2023, 12:51pm UTC](https://forum.wixstudio.com/t/unique-list-on-multiple-dropdown-that-are-linked-directly-to-your-data-collection/9118/13 "2023-04-03T12:51:17Z")

</div>

Then there must be some issue with your search code. If you’ve followed my original set of code and the explanation I’ve included, the function DropdownListpdate() is generally the code to run the whole function set the {}

Basically, every time you call for this function code = DropdownListUpdate(), the function you’ve set within it will run.

The reason why when you refresh the page, it works, is because the page is set to run this function every time the page load and that command was right at the very bottom of my code that simply says: DropdownListUpdate()

So if you want the list to update after your search without refreshing your page, simply insert DropdownListUpdate() within your search code.

---

<div class="post-metadata">

**Author:** ![angirene7](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/angirene7/32/20920_2.png) [@angirene7](https://forum.wixstudio.com/u/angirene7)\
**Post date:** [April 3, 2023, 12:52pm UTC](https://forum.wixstudio.com/t/unique-list-on-multiple-dropdown-that-are-linked-directly-to-your-data-collection/9118/14 "2023-04-03T12:52:30Z")

</div>

I don’t think there is the capability of letting you follow the content of my project without granting you access to make amendments to it. Sorry.

---

<div class="post-metadata">

**Author:** ![eulapro](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@eulapro](https://forum.wixstudio.com/u/eulapro)\
**Post date:** [April 3, 2023, 2:55pm UTC](https://forum.wixstudio.com/t/unique-list-on-multiple-dropdown-that-are-linked-directly-to-your-data-collection/9118/15 "2023-04-03T14:55:11Z")

</div>

Ok  
Thank you again
