PAID JOB - Dropdown Code

Hey, please could someone tell me where I am going wrong with my code? I need my drop down box to show all countries in A-Z and remove duplicates. Thanks!

import wixData from “wix-data”;

$w.onReady( function () {

$w(" #button1 “).target = “_self”;
$w(” #dataset1 “).onReady( () => {
let options = $w(” #iContinent “).options;
options.sort( function (a, b) {
let labelA = a.label.toUpperCase();
let labelB = b.label.toUpperCase();
if (labelA < labelB) {
return -1;
}
if (labelA > labelB) {
return 1;
}
return 0;
});
$w(” #iContinent ").options = options;
})
})

$w.onReady(() => {
loadContinents();
});

let lastFilterTitle;
let lastFilterContinent;
let debounceTimer;
export function iTitle_keyPress(event, $w) {
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = undefined;
} debounceTimer = setTimeout(() => {

filter($w(’ #iTitle ‘).value, lastFilterContinent);
}, 500);
}
export function iContinent_change(event, $w) {
filter(lastFilterTitle, $w(’ #iContinent ').value);
}

function filter(title, continent) {
 if (lastFilterTitle !== title || lastFilterContinent !== continent) {
 let newFilter = wixData.filter();
 if (title)
      newFilter = newFilter.contains('articleTitle', title);
 if (continent)
      newFilter = newFilter.contains('continent', continent);
    $w('#dataset1').setFilter(newFilter);   
    lastFilterTitle = title; 
    lastFilterContinent = continent;
  }
}

function loadContinents() {
  wixData.query('Continents')
    .find()
    .then(res => {
 let options = [{"value": '', "label": 'All Countries'}];
      options.push(...res.items.map(continent => {
 return {"value": continent.title, "label": continent.title};
      }));
      $w('#iContinent').options = options;
    });
}

Paid Job - if someone can fix!

I can see that the code is copied from an example so you might want to check for correct ids.

The below code will allow you to display only the unique values provided that you have set the correct database name, database field keys & the id of the dropdown.

function loadContinents() {
    wixData.query('Continents')
    .find()
    .then( (results) => {
         const uniqueTitles = getUniqueTitles(results.items);
         let cleanList = buildOptions(uniqueTitles);
         cleanList.unshift({"value": '', "label": 'All Countries'});
         $w("#iContinent").options = cleanList;
    });
}

function getUniqueTitles(items) {
    const titlesOnly = items.map(item => item.continent);
    return [...new Set(titlesOnly)];
}

function buildOptions(uniqueTitles) {
    return uniqueTitles.map( (curr) => {
    return {label: curr, value: curr};
  });
}

Hi,

I’ve triple checked, all connected correctly. The issue is the code works perfectly in the editor, everything working in preview and shows correctly.

The second I try on the live site (everything still works) however the duplicates on the dropdown are back (even though they are not there in preview mode!!)

Pulling my hair out here, I’m sure its a Wix error!

@tom-maughan Try out my code

@shantanukumar847 Thanks Shan,

I tried but it just shows the label ‘All Countries’ and doesn’t show any other options now, I’ve double checked it all so not sure where its going wrong, maybe because its linked with another search box?

@tom-maughan I just tested the code and it works fine. Maybe there is something else that is causing this.

Try to have an expert from the Arena look at it.

@shantanukumar847 I did but the cheapest wanted £1000 and said it would take 10 hours to look into which is a joke lol…

If I send you the code, would you be able to look into it, how much would you charge? I’m sure its a quick fix that’s I’m doing wrong

@shantanukumar847 will do thanks