OnReady function frustration

@jonatandor35 Hi Chief. It clears the filters so it shows the database without duplicates:

export function clearFIlters_onClick(event) {
$w(“#dataset1”).setFilter(wixData.filter(undefined));
$w(“#dropdown1”).value = 0;
$w(“#dropdown2”).value = 0;
$w(“#iTitle”).value = “”;
}

@ladanimario ok. So in that case, you need to make a small change.
First take my code out of the $w.onReady() and dataset.onReady(0, and put it in a function:

function omitDuplicates() {
//put here my code. Make sure you don't copy the onReady() parantheses to here.
}
$w.onReady( function() {
$w("#dataset1").onReady( () => {
 omitDuplicates();
})
})
 export function clearFIlters_onClick(event) { 
//keep your filtering code and the add:
omitDuplicates();
}

@jonatandor35 I think I am an idiot… Look at this now?

@ladanimario you have an extra }) there (twice! ). Just delete them. They belong to the onReady() functions
(I mean: delete lines 36-37 of your screenshot)

@jonatandor35 Hi Big Boss, I deleted it and the code shows no errors however the filter still does not do what it should:

export function clearFIlters_onClick(event) {
$w(“#dataset1”).setFilter(wixData.filter(undefined));
$w(“#dropdown1”).value = 0;
$w(“#dropdown2”).value = 0;
$w(“#iTitle”).value = “”;
omitDuplicates();
}

@ladanimario setFilter() is a Promise. that means you need to wait for its completion before you continue to the next code.
Therefore:

export function clearFIlters_onClick(event) { 
$w("#dropdown1").value = 0; 
$w("#dropdown2").value = 0; 
$w("#iTitle").value = "";
$w("#dataset1").setFilter(wixData.filter())
.then(() => {//<< You need this "then"
    omitDuplicates();
})
}

@jonatandor35 I learned so much from you today - this is unbelievable!!! Many thanks. I will store it in my code books!!! Please do not see me as a begger but I have one more issue with this code.

When chosing an item from the dropdown, it lists the duplicates as well. Any chance that I can fix that easily?

Btw. are you a professional at wix? anyway that I can reach out to your supervisor and tell them what an awesome job you did today?

Many thanks,
Mario

@ladanimario , to do that, you’ll need an extra code. But I’m not sure what you currently display in your dropdown. The firma name? something else?

btw,
I’m neither professional nor at Wix, just a Wix user like you.
and thanks for the complements. :slight_smile:

@jonatandor35 Hi Hombre, I have two dropdowns that communicate with each other. When i click on a value of the first dropdown it shows again the “firma” duplicates. Kindly see here


Here the code for the dropdown function

function uniqueDropDown1 (){
wixData.query(“Memberlistev2”)
.limit(1000)
.find()
.then(results => {
const uniqueTitles = getUniqueTitles(results.items);
$w(“#dropdown1”).options = buildOptions(uniqueTitles);
});

function getUniqueTitles(items) {
const titlesOnly = items.map(item => item.hauptkategorie);
return [… new Set(titlesOnly)];
}
function buildOptions(uniqueList) {
return uniqueList.map(curr => {
return {label:curr, value:curr};
});
}
}

export function dropdown1_onChange(event) {
$w(“#dataset1”).setFilter( wixData.filter()
.eq(“hauptkategorie”, $w(“#dropdown1”).value)
);
uniqueDropDown2();
}

@ladanimario instead of this code (which I haven’t fully read), I’d just add a few lines to the omitDuplicates() function. Something like:

function omitDuplicates() {
    let data = $w("#repeater1").data;
    let newData = [];
    let dropdownOptions = [];
    data.forEach(e => {
        if (!newData.some(o => o.firma === e.firma)){
            newData.push(e);
            dropdownOptions.push({"label": e.firma, "value": e.firma});
            }
        })
    $w("#repeater1").data = newData;
    $w("#firmaDropdown").options = dropdownOptions;
}

@jonatandor35 Unfortunately that did not help me, it has mixed up our previous setting. When loading the page it shows the duplicates of “firma”. When clicking reset, and click then on the dropdown, I suddenly see the “firma” in the dropdown list :slight_smile:

My problem: when clicking on any dropdown1 value (hauptkategorie), the various companies (firma) are listed more than once.

I hope I explained myself better :slight_smile:

@ladanimario actually I’ma little bit confused and not sure what exactly you’re saying.
It sounds like you have a complicated flow there, and something is missing. That’s why it doesn’t work.
But I think you can find the basic concept and direction in my code and adjust it to your specific scenario.

@jonatandor35 Sorry for the confusion. Let me clear things up.

I have a database that includes various collumns such as companies, country, main category etc…

Now on my website I have connected this database and have two dropdown list that are connected to the database via wix code.

Dropdown1 fetches the info “main category” and shows me all the entries. However the entries include duplocates which I want to get rid off.

Please see the website: https://www.swiss-medtech.ch/members2

When using the first dropdown, I receive duplicates.

Many thanks in advance.

Cheers,
Mario

@ladanimario you need to call omitDuplicates() every time you run a filter.
so for example, if you run a filter when the user selects an option from the dropdown, you should call the omitDuplicates() once the setFilter() promise has been resolved (like you did for the “Reset” filter).
Does it answer your question?

@jonatandor35 It certainly makes sense, but when adding that - it unfortunately does not do anything:

export function dropdown1_onChange(event) {
$w(“#dataset1”).setFilter( wixData.filter()
.eq(“hauptkategorie”, $w(“#dropdown1”).value)
);
omitDuplicates();
uniqueDropDown2();
}

Sorry for bothering you…

@ladanimario you forgot the .then() part:

 $w("#dataset1").setFilter( wixData.filter()
.eq("hauptkategorie", $w("#dropdown1").value) )
.then(()=> {
   omitDuplicates(); 
})

@jonatandor35 Hi Hombre - sorry for my late reply. I fell asleep unofrtunately. However I am up and running but my code does not haha. I have tried to swirl it around before bothering you… but no luck :frowning:

Bro I played around

I figured it out. THANK YOU SOOOOOO MUCH BIG CHIEF

Hey Boss, I have a huge issue and a huge favor to ask.

I have connected a pagination bar and connected it with the database. However when I click on to next page, it shows the duplicates again.

Any ideas from your side. I am desperate again :frowning:

Cheers,
mario

@ladanimario ,I’ve almost never used pagination, so I can’t provide a solution without exploring it. Maybe you should try calling the omitDuplicates() function after you move to the previous/next pagination page (check the api documentation to see how to do it, and if it’s a promise, remember to use .then() ).