Several concerns

Hello,

For several hours I have been trying to display the practitioners in random order.

For example if I have 10 practitioners in the region of Occitania, I want that at each submission I do not have the same order and the same when I choose the department or when it displays all practitioners and I would also like to be able to change region or department without resetting with the reset button.

Thank you in advance for your help.

Here is the modified code:

import wixData from 'wix-data';

$w.onReady(function () {
    $w("#dropdownreg, #dropdowndep").onChange(function () {
        search();
    });
    dropdownreg(); // appel de la fonction dropdownreg lors du chargement de la page
});

function dropdownreg() {
    wixData.query("Listepraticiens")
        .limit(1000)
        .find()
        .then(results => {
            const uniqueTitles = getUniqueTitles(results.items);
            $w("#dropdownreg").options = buildOptions(uniqueTitles);
            $w("#dropdownreg").value = undefined; // réinitialisation de la valeur du dropdown lorsque la fonction est appelée
            dropdowndep(); // appel de la fonction dropdowndep lorsque la région est changée
        });

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

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

function locationCityFilter() {
    const region = $w("#dropdownreg").value; // récupération de la valeur de la région
    $w("#dataPraticien").setFilter(wixData.filter()
        .eq("region", region)
        .and(
            wixData.filter().contains("departement", String($w('#dropdowndep').value))
        )
    );
}

function dropdowndep() {
    const region = $w("#dropdownreg").value; // récupération de la valeur de la région
    wixData.query("Listepraticiens")
        .contains("region", region)
        .limit(1000)
        .find()
        .then(results => {
            const uniqueTitles = getUniqueTitles(results.items);
            $w("#dropdowndep").options = buildOptions(uniqueTitles);
            $w("#dropdowndep").value = undefined; // réinitialisation de la valeur du dropdown lorsque la fonction est appelée
            locationCityFilter(); // appel de la fonction locationCityFilter lorsque le département est changé
        });

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

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

function search() {
    locationCityFilter(); // appel de la fonction pour filtrer les résultats en fonction de la région et du département choisis
    const query = wixData.query("Listepraticiens")
        .contains('departement', String($w('#dropdowndep').value))
        .and(wixData.filter().eq("region", String($w('#dropdownreg').value)))
        .ascending("_random"); // ajout de l'ordre aléatoire

    $w("#dataPraticien").onReady(() => {
        $w("#dataPraticien").setFilter(query)
            .then(count);
    });

     //COUNT FUNCTION👍
     function count() {
         let total = $w('#dataPraticien').getTotalCount();
         if (total > 1) {
             $w('#nbrpraticiens').text = `${total} praticiens trouvés`;
             $w('#nbrpraticiens').show();
         }
         if (total === 1) {
             $w('#nbrpraticiens').text = `${total} praticien trouvé`;
             $w('#nbrpraticiens').show();
         }
         if (total === 0) {
             $w('#nbrpraticiens').text = "Nous n'avons pas encore de praticien dans ce secteur";
             $w('#nbrpraticiens').show();
         }

         //SCROLL TO THE TOP WHEN PAGINATION IS CHANGED ⚡

         $w("#pagination1").onChange(() => {

             $w("#haut").scrollTo();
         });
     }
 }

Here is the original code:

 import wixData from 'wix-data';

 $w.onReady(function () {
     $w("#dropdownreg, #dropdowndep").onChange(function () {
         search();
     });
 });
 dropdownreg();

 //SEARCH BUTTON TRIGGER⚡
 $w("#searchButton2").onClick(function () {
     search();
 });

 function dropdownreg() {
     wixData.query("#dataPraticien")
         .limit(1000)
         .find()
         .then(results => {
             const uniqueTitles = getUniqueTitles(results.items);
             $w("#dropdownreg").options = buildOptions(uniqueTitles);
         });

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

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

 export function dropdownreg_change(event, $w) {
     dropdowndep();
     $w("#dropdowndep").enable();
     locationCityFilter();

 }

 function locationCityFilter() {
     $w("#dataPraticien").setFilter(wixData.filter()
         .eq("region", $w("#dropdownreg").value)

     );

 }

 function dropdowndep() {
     wixData.query("#dataPraticien")
         .contains("departement", $w("#dropdownreg").value)
         .limit(1000)
         .find()
         .then(results => {
             const uniqueTitles = getUniqueTitles(results.items);
             $w("#dropdowndep").options = buildOptions(uniqueTitles);

         });

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

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

         });
     }
 }

 function search() {
     $w("#dataPraticien").onReady(function () {
         $w("#dataPraticien").setFilter(wixData.filter().contains('departement', String($w('#dropdowndep').value))
                 .and(wixData.filter().contains("region", String($w('#dropdownreg').value))))

             .then(count)

     });

     //CLEAR FILTER 🚀
     $w("#button31").onClick(function () {
         $w("#dropdowndep").value = undefined;
         $w("#dropdownreg").value = undefined;

         $w("#dataPraticien").setFilter(wixData.filter());
         $w('#nbrpraticiens').hide();

     });
     //COUNT FUNCTION👍
     function count() {
         let total = $w('#dataPraticien').getTotalCount();
         if (total > 1) {
             $w('#nbrpraticiens').text = `${total} praticiens trouvés`;
             $w('#nbrpraticiens').show();
         }
         if (total === 1) {
             $w('#nbrpraticiens').text = `${total} praticien trouvé`;
             $w('#nbrpraticiens').show();
         }
         if (total === 0) {
             $w('#nbrpraticiens').text = "Nous n'avons pas encore de praticien dans ce secteur";
             $w('#nbrpraticiens').show();
         }

         //SCROLL TO THE TOP WHEN PAGINATION IS CHANGED ⚡

         $w("#pagination1").onChange(() => {

             $w("#haut").scrollTo();
         });
     }
 }

If you would —> NOT <— change your posts and —> EDIT <---- them every few hours —> maybe you would get another suggestion.

It is very difficult to give you an answer —> when you change your whole post including the code.

Do —> NOT <— change your already added posts → instead just add another post (comment/reply) if you want to correct or to edit something, or do it like…

EDIT:
//here your edited (upgraded or changed informations)

And also do not reopen your post agin ang again from new with the same issue.

It works after I just modified my comment in the other post because since it’s not the same concern anymore I thought it was logical that I don’t mix it up, by the way that’s why I made a new post and the original code is the base I had to work on.

And now I don’t intend to make any changes without a comment because I have no idea what’s wrong.

Your problem is, that you are mixing Wix-Data with DATASET-Code-Parts.
It is the most common issue you will find here inside the Velo-Forum.

Everyone who tries to mix wix-data with datasets → end in NOWHERE with a lot of issues —> LIKE YOU NOW <—.

Also your code-structure is very CHAOTIC.

And if i take a look onto those code-parts…

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

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

…it feels like it is my own code. Where did you get it from ?

I also did not see this code-part in your very first post (which you have deleted or updated).

Ok, back to your problem:
You also do not use ASYNC-AWAIT or .then()-method the right way.
You do not place functions onto the right place in your code.
You do not give the right names for your functions, which can cause → CONFUSIONS.

If i would take some more time onto your code. surely i would find even more mistakes and strange code-parts.

Sooooo, let’s do it the right way, like it should be…

Your code ----> ALWAYS <— should start with…

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

I think you will agree with me, because this part of code you did well.
But already the next CHECK-POINT you did not well ! What do i mean?

When you are starting your SERACH-ENGINE, by changing one of your 2-dropdowns, your Search-Function → starts a further function → “locationCityFilter()”.

Inside of this function you are working with a —> DATASET <—.

WHAT??? YOU ARE ALREADY WORKING WITH A DATASET, WITHOUT HAVE NEVER WAITING THE DATASET TO BE READY ???

Ok, let’s do it the right way…

So our last codeposition was —> $w.onReady()
And the very next code-line we want to have should be???
EXACTLY —> We want to wait our dataset first to be ready , before we start to work with it…

$w.onReady(()=> { 
    $w('#dataPraticien').onReady(()=>{
    
    });
});

Huray, first of your issues → resolved!

What next?

Oh yeah, you have some Dropdowns i forgot…:grin: and also i forgot to mention that the very first code-line of your page code of course should be an IMPORT, if you are using some API-Functions…

import wixData from 'wix-data';

$w.onReady(()=> {
    $w('#dataPraticien').onReady(()=>{

        $w("#dropdownreg").onChange(async()=>{
            let dropdownregValue = $w("#dropdownreg").value;
            let resultReg = await search(dropdownregValue);
            console.log(resultReg);
        });

        $w("#dropdowndep").onChange(async()=>{
            let dropdowndepValue = $w("#dropdowndep").value;
            let resultDep = await search(dropdowndepValue);
            console.log(resultDep);
        });
        xxxxx(); 
    });
});

Huray! Your new CODE!

But what the hell is that?
AWAIT ? And what is this —> xxxxx. What the hell are you programming man?:grin:
And there is even an ASYNC, what is that for?

Take a look onto which IDs and names you used inside your code…

$w("#dropdownreg, #dropdowndep").onChange(function(){........
export function dropdownreg_change(event, $w){..........
function dropdownreg(){wixData.query("#dataPraticien").....
  1. It seems like everything inside your code is —> dropdownreg <—
    You are using that for dropdowns, functions and export functions.
    Surely not the best practise!

  2. You have splitted your dropdown-event onto two different code-parts.
    -inside your onReady-Code-block and in an export-function.
    Surely not the best practise!

How to name your used elements the best way?
If you use a dropdown → ddn + elementname —> “ddnReg” / “ddnDep”
If if would be an input-element —> inpReg / inpDep
If it would be a box -------> boxReg / boxDep
and so on…
Use logical element-IDs to be able to read your code easier and faster.

How to name your functions?
Use a name for your function which will describe functions functionality.

What does do → dropdownreg? (wait! wasn’t it a dropdown?)
Or no wait, it is a function… (you already understand what i mean?)

function dropdownreg() {
     wixData.query("#dataPraticien")
         .limit(1000)
         .find()
         .then(results => {
             const uniqueTitles = getUniqueTitles(results.items);
             $w("#dropdownreg").options = buildOptions(uniqueTitles);
         });

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

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

So one more time → Which name should this function have???
My suggestion would be —> “get_DataPraticien”. This would be a GOOD name for this function, but surely not —> “dropdownreg”.

Of course this was not the full solution (only 15% of your code) and so much failures and chaos!!!

First —> REWRITE your code giving it a much better STRUCTURE.
Second → use more → CONSOLE-LOGS to inspect and understand your own code better.

You will have to modify and edit a lot, to get the result how it should be.

Thank you very much, but I have this error:

I may have forgotten something in your explanations but in any case thank you for your help.
UserError: An error occurred in one of datasetReady callbacks Caused by ReferenceError: xxxxx is not defined

And I have an error on line 8 and 14 at the :

‘await’ has no effect on the type of this expression.

And I also have the XXX which I don’t see what it is for.

And I have the get_DataPraticien function which is transparent.

Here is the complete new code:

import wixData from 'wix-data';

$w.onReady(()=> {
    $w('#dataPraticien').onReady(()=>{

        $w("#dropdownreg").onChange(async()=>{
            let dropdownregValue = $w("#dropdownreg").value;
            let resultReg = await search(dropdownregValue);
            console.log(resultReg);
        });

        $w("#dropdowndep").onChange(async()=>{
            let dropdowndepValue = $w("#dropdowndep").value;
            let resultDep = await search(dropdowndepValue);
            console.log(resultDep);
        });
        xxxxx(); 
    });
});

 function get_DataPraticien() {
     wixData.query("#dataPraticien")
         .limit(1000)
         .find()
         .then(results => {
             const uniqueTitles = getUniqueTitles(results.items);
             $w("#dropdownreg").options = buildOptions(uniqueTitles);
         });

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

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

 export function dropdownreg_change(event, $w) {
     dropdowndep();
     $w("#dropdowndep").enable();
     locationCityFilter();

 }

 function locationCityFilter() {
     $w("#dataPraticien").setFilter(wixData.filter()
         .eq("region", $w("#dropdownreg").value)

     );

 }

 function dropdowndep() {
     wixData.query("Listepraticiens")
         .contains("departement", $w("#dropdownreg").value)
         .limit(1000)
         .find()
         .then(results => {
             const uniqueTitles = getUniqueTitles(results.items);
             $w("#dropdowndep").options = buildOptions(uniqueTitles);

         });

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

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

         });
     }
 }

 function search() {
     $w("#dataPraticien").onReady(function () {
         $w("#dataPraticien").setFilter(wixData.filter().contains('departement', String($w('#dropdowndep').value))
                 .and(wixData.filter().contains("region", String($w('#dropdownreg').value))))

             .then(count)

     });

     //CLEAR FILTER 🚀
     $w("#button31").onClick(function () {
         $w("#dropdowndep").value = undefined;
         $w("#dropdownreg").value = undefined;

         $w("#dataPraticien").setFilter(wixData.filter());
         $w('#nbrpraticiens').hide();

     });
     //COUNT FUNCTION👍
     function count() {
         let total = $w('#dataPraticien').getTotalCount();
         if (total > 1) {
             $w('#nbrpraticiens').text = `${total} praticiens trouvés`;
             $w('#nbrpraticiens').show();
         }
         if (total === 1) {
             $w('#nbrpraticiens').text = `${total} praticien trouvé`;
             $w('#nbrpraticiens').show();
         }
         if (total === 0) {
             $w('#nbrpraticiens').text = "Nous n'avons pas encore de praticien dans ce secteur";
             $w('#nbrpraticiens').show();
         }

         //SCROLL TO THE TOP WHEN PAGINATION IS CHANGED ⚡

         $w("#pagination1").onChange(() => {

             $w("#haut").scrollTo();
         });
     }
 }

I may have forgotten something in your explanations but in any case thank you for your help.

This is why you still get ERRORs…


I already mentioned, that i just have reconstructed about 15% of your whole chaotic code. There are still about 85% to be modified.

You better should give some information about your setup.

-You are using 1x-dataset!
-You are using 2x-DropDowns (“dropdownReg” + “dropdownDep”)
-You are using 1x-databases (“Listepraticiens” + “one more ???”)

Please show your setup.
Please show which databases are involved to your issue.
Please show which DATABASE-FIELDS are involved to your issue.
Please describe what you are trying to achive.
Please describe the flow of how the user would use your dropdowns.
Are you trying to generate a cascade-dropdown? (dropdown-1 fills dropdown-2 by selected item of dropdown-1) ???
Why you are mixing DATASET+CODE-PARTS ?
Show your DATASET-SETUP.

Describe what happens step-by-step, from 0-end.

Example of a detailed step-by-step-description

  1. User has 2 dropdowns in front of him…
  2. User clicks dropdown-1 and chooses an option
  3. Drodown-2 gets filled automatically by the choice-result of dropdown-1
  4. Also all values of dropdown-2 are unique.
  5. Now the user has the opertunity to choose from dropdown-2.
  6. After choice of dropdown-2 → a SEARCH-FUNCTION starts.
  7. …and so on… and so on…

NOW YOUR TURN —> DESCRIBE YOUR SETUP AND THE PROCESS-FLOW —> STEP-BY-STEP as detailed as possible.

Then i will take a look one more time onto your code.

THERE ARE TOO MANY OPEN QUESTIONS!

It works.

What do you mean by installation here?

For the dataset I use it because I thought it was the right solution but since it’s the first time I manipulate it, I wouldn’t be.

The dropdownReg is for the dropdown button that concerns the regions and the dropdownDep is the same but for the different departments associated with the region used.

For this part I use only this database.

The database fiels involved are the first name, the last name, the expertise, the zip, the city and the link on the “make an appointment” button that redirects to the practitioner’s file.

Then I try to get that when you arrive on the page you see practitioners randomly each time and the same when the user chooses the region and the department. And I would like to be able to allow the user to change the region and the department dynamically without the need for a reset button.

For example if I’m looking for a practitioner in the region of Occitania and in fact I want to see a practitioner in Ile de France, I can change the region without resetting and the same for the departments associated with the regions.

I don’t know why I mixed dataset + code shares

I show you how the dataset-setup

  1. The user has 2 dropdown lists in front of him and practitioners are displayed randomly

  2. The user clicks on the drop down list 1 and choose a region.

  3. After choosing the region from the drop down list a search function starts

  4. The user chooses a suitable practitioner otherwise (6)

  5. In addition, all values in the drop-down lists are unique.

  6. The user now has the possibility to choose from the drop-down list 2 which concerns the departments associated with the region.

  7. After selecting the drop-down list a search function starts.

Edit

Oh yes, I almost forgot, there is a pagination that doesn’t work correctly when you choose a region, but if you choose a department, it works correctly.

It’s just for the clarification for this piece of code even if I know that by reordering the code, it will fix it.

Throw your old code out of the window and try to get this one to work…

import wixData from 'wix-data';

//-------------------------USER-INTERFACE-----------------------------------------------
const DATABASE = "Listepraticiens"; //<-- Paste in here the ID of your DATABASE!
const DB_FIELD1 = "region";         //<-- Paste in here the ID of the Database-Field-1!
const DB_FIELD2 = "departement";    //<-- Paste in here the ID of the Database-Field-2!
const ddn1 = "#dropdownReg";        //<-- Here the ID of your Dropdown-1.
const ddn2 = "#dropdownDep";        //<-- Here the ID of your Dropdown-2.
//-------------------------USER-INTERFACE-----------------------------------------------

$w.onReady(async()=>{console.log("Page is ready...");
    //Loading dropdown-1 immediately when page is ready...
    let itemData = await get_ItemData(); 
    console.log("Item-Data: ", itemData);
    create_UniqueDropdown(itemData, DB_FIELD1, ddn1);

    //The user clicks on the dropdown-1 and choose a REGION.
    //This will affect on dropdown-2 and fill it with unique-values.
    //Regarding the choice taken on dropdown-1.
    $w(ddn1).onChange(async()=>{
        let itemData = await get_ItemData(); 
        create_UniqueDropdown(itemData, DB_FIELD2, ddn2); 
    }); 
    $w(ddn2).onChange(async()=>{
        //????????????????????????
        //CONTINUE...............
        //Write your code here......
        //What should happen next when changing ddn-2???
        let items = await startSearchEngine();
        console.log("HURAY --> !!! " + items);
        if (items[0]) {
            $w('#nbrpraticiens').text = `${total} praticiens trouvés`;
            $w('#nbrpraticiens').show();
        }
        if (total === 1) {
            $w('#nbrpraticiens').text = `${total} praticien trouvé`;
            $w('#nbrpraticiens').show();
        }
        if (total === 0) {
            $w('#nbrpraticiens').text = "Nous n'avons pas encore de praticien dans ce secteur";
            $w('#nbrpraticiens').show();
        }
    }); 
    $w("#pagination1").onChange(()=> {$w("#haut").scrollTo();});
});

function get_ItemData() {
    let QUERY = wixData.query(DATABASE);
    QUERY.limit(1000)
    QUERY.find()
    .then((res)=> {console.log(res);
        if (res.totalCount>0) {
            let itemData = res.items; 
            return itemData;
        }
        else {console.log("No DB-Entries found!");}
    }).catch((err)=>{console.log(err);});
}

function create_UniqueDropdown(items, dbfield, dropdown) { 
    const uniqueTitles = getUniqueTitles(items); 
    $w(dropdown).options = buildOptions(uniqueTitles); 
    function getUniqueTitles(items) {
        const titlesOnly = items.map(item => item[dbfield]); 
        return [...new Set(titlesOnly)];
    }
    function buildOptions(uniqueList) {
        return uniqueList.map(curr => {
            return {
                label:curr, value:curr
            };
        });
    }
}

function startSearchEngine() {
    wixData.query(DATABASE)
    .limit(1000)
    .contains('departement', String($w('#dropdowndep').value))
    .eq("region", String($w('#dropdownreg').value))
    //.ascending("_random")
    .find()
    .then((res)=>{console.log("My found RESULTS: ", res);
        if (res.totalCount>0) {let items = res.items;
            console.log("Items: ", items);
            return items;
        }
        else {console.log("No RESULTS found!!!");}
    })
    .catch((err)=>{console.log("We have a problem ! ---> " + err);});
}

You will need…

  1. 2x Dropdowns (“dropdownReg” + “dropdownDep”)
  2. 1x Your database —> “Listepraticiens”
  3. 2x The database-fields you are using for your dropdowns (“region”/“department”)
  4. You do not need a dataset in this version. Disconnect your dropdowns from your dataset.
    Your dropdowns should be filled automatically and directly from the DATABSE.

This code-line:

//.ascending("_random")

Where did you get it from ???
It is deactivated.

Further:
If you want to connect a repeater to show results, you will have to code it (expanding your code).

Also take a look onto all the given CONSOLE-LOGS, which will help you to bring this CODE to → WORK!

And for more informations, read this post…

Now you should be able to understand what you are doing and be able to generate your wished functionality.

And this one…

Thanks a lot, the ascending random I was using to try to display the practitioners randomly.

For example: Practitioner A
Practitioner b
Practitioner c
Practitioner d
Practitioner e
Practitioner f

I want the display order to change with each submission.

And the ddn-2 which concerns the departments, I would like to be able to choose the department according to the region and to be able to change the department dynamically as for the ddn-1 which concerns the region and which currently functions.

And I have an error on the total level, could you please help me a little more?

And I also have an error Item-Data: undefined.

In the meantime I’ll read the links you gave me and see if I can finish alone.

Ok, i did some few UPGRADES on my provided code…
See changes here…

import wixData from 'wix-data';
//-------------------USER-INTERFACE------------------------
const DATABASE = "CARS"; //<-- Paste in here the ID of your DATABASE!
const DB_FIELD1 = "model";      //<-- Paste in here the ID of the Database-Field-1!
const DB_FIELD2 = "type";   //<-- Paste in here the ID of the Database-Field-2!
const ddn1 = "#ddnModel";       //<-- Here the ID of your Dropdown-1.
const ddn2 = "#ddnType";        //<-- Here the ID of your Dropdown-2.
//-------------------USER-INTERFACE------------------------
$w.onReady(async()=>{console.log("Page is ready...");
    let itemData = await get_ItemData1(); 
    console.log("Item-Data: ", itemData);
    create_UniqueDropdown(itemData, DB_FIELD1, ddn1);
    $w(ddn1).onChange(async()=>{
        let value = String($w(ddn1).value);
        let itemData = await get_ItemData2(value); 
        create_UniqueDropdown(itemData, DB_FIELD2, ddn2); 
        console.log("xxx", itemData);
    }); 
    $w(ddn2).onChange(async()=>{
        let items = await startSearchEngine();
        console.log("HURAY --> !!! " + items);
    }); 
});

function get_ItemData1() {
    let query = wixData.query(DATABASE)
    query.limit(1000)
    return query.find()
    .then((res)=> {console.log(res);
        console.log(res.totalCount)
        if (res.totalCount>0) {
            let itemData = res.items; 
            return itemData;
        }
        else {console.log("No DB-Entries found!");}
    }).catch((err)=>{console.log(err);});
}

function get_ItemData2(value) {
    return  wixData.query(DATABASE)
    .limit(1000)
    .eq(DB_FIELD1, value)
    .find()
    .then((res)=> {console.log(res);
        console.log(res.totalCount)
        if (res.totalCount>0) {
            let itemData = res.items; 
            return itemData;
        }
        else {console.log("No DB-Entries found!");}
    }).catch((err)=>{console.log(err);});
}
function create_UniqueDropdown(items, dbfield, dropdown) { 
    const uniqueTitles = getUniqueTitles(items); 
    $w(dropdown).options = buildOptions(uniqueTitles); 
    function getUniqueTitles(items) {
        const titlesOnly = items.map(item => item[dbfield]); 
        return [...new Set(titlesOnly)];
    }
    function buildOptions(uniqueList) {
        return uniqueList.map(curr => {
            return {
                label:curr, value:curr
            };
        });
    }
}
function startSearchEngine() {
    wixData.query(DATABASE)
    .limit(1000)
    .contains('departement', String($w(ddn1).value))
    .eq("region", String($w(ddn2).value))
    .ascending("_random")
    .find()
    .then((res)=>{console.log("My found RESULTS: ", res);
        if (res.totalCount>0) {let items = res.items;
            console.log("Items: ", items);
            return items;
        }
        else {console.log("No RESULTS found!!!");}
    })
    .catch((err)=>{console.log("We have a problem ! ---> " + err);});
}

This CODE has been tested now and is working well.

You can find here a little example…

https://russian-dima.wixsite.com/my-site-4/conditional-dropdowns

Here a SCREENSHOT of the used DATABASE…

Some CONSOLE-LOGS…for better understandings and proovement for the correct working of the code.

USED-SETUP:

//------------------------USER-INTERFACE------------------
const DATABASE = "CARS"; //<-- Paste in here the ID of your DATABASE!
const DB_FIELD1 = "model";      //<-- Paste in here the ID of the Database-Field-1!
const DB_FIELD2 = "type";   //<-- Paste in here the ID of the Database-Field-2!
const ddn1 = "#ddnModel";       //<-- Here the ID of your Dropdown-1.
const ddn2 = "#ddnType";        //<-- Here the ID of your Dropdown-2.
//------------------------USER-INTERFACE-------------------

Result inside dropdowns…

Thank you very much but maybe I’m stupid or something but I have the department button that doesn’t work.

And how can I get this list to update and randomly rank with each submission ??

From what I see it’s a repeater the whole cards.

And for the number of practitioners how do I get it to display on the sentence that is right above the cards and it is called #nbrpraticiens ??

Here are some screenshots.

!Do —>NEVER<— show —>SENSITIVE-DATA<— on your pics!
Hide sensitive data (on pics).

You saw the working example?
Just copy and paste the new code to your project.
Modify the USER-INTERFACE for your own needs…

WRONG-USER-INTERFACE…

//-------------------USER-INTERFACE---------------
const DATABASE = "CARS";
const DB_FIELD1 = "model";
const DB_FIELD2 = "type";
const ddn1 = "#ddnModel";
const ddn2 = "#ddnType";
//-------------------USER-INTERFACE---------------

RIGHT-USER-INTERFACE…

//-------------------USER-INTERFACE---------------
const DATABASE = "Listepraticiens";
const DB_FIELD1 = "region";        
const DB_FIELD2 = "departement";  
const ddn1 = "#dropdownReg";     
const ddn2 = "#dropdownDep";    
//-------------------USER-INTERFACE---------------

After you have (1)copied the whole code, (2) pasted it into your project and (3) choosed the right “User-Interface”, normaly everything should work like a charm.

Check all IDs of your elements if they fit the User-Interface-Data.

Thank you very much but maybe I’m stupid or something but I have the department button that doesn’t work.

You surely have seen, that some code-parts were cut off.
I am showing you just the main functionality, i am not coding your whole project for you.

You will have to modify the code (expanding it and it’s functionality).

You want to connect a repeater?

You will need this one…for example…

$w('#myRepeaterID').onItemReady(($item, itemData, index)=>{
     //IMAGE inside REPEATER
     $item('#elementIDwhichIsInsideRepeaterHere').src = itemData['datafieldID'];
     //TEXT inside REPEATER
     $item('#elementIDwhichIsInsideRepeaterHere').text = itemData['datafieldID'];
});

…after you have loaded the → ITEMS (itemData) ← into your repeater by code.
(Of course you will have to disconnect your repeater from dataset, because now everythign will work by code).

items <— let’s say you have generated your itemData (results.items)

$w('#myRepeaterID').data = items;

Paste this to the right code-line inside of your code…

$w(‘#nbrpraticiens’).text = …

.then((res)=>{console.log("My found RESULTS: ", res);
    if (res.totalCount>0) {let items = res.items;
        $w('#nbrpraticiens').text = res.totalCount;
        console.log("Items: ", items);
        return items;
    }
    .....
    ...
    ..
    .

As you can see, of course the whole code is not complete!

If you put everything together, it should later look like this one…
(with some added modifications)

import wixData from 'wix-data';

//-------------------USER-INTERFACE---------------
const DATABASE = "Listepraticiens";
const DB_FIELD1 = "region";        
const DB_FIELD2 = "departement";  
const ddn1 = "#dropdownReg";     
const ddn2 = "#dropdownDep";    
//-------------------USER-INTERFACE---------------

$w.onReady(async()=>{console.log("Page is ready...");
    let itemData = await get_ItemData1(); 
    console.log("Item-Data: ", itemData);
    create_UniqueDropdown(itemData, DB_FIELD1, ddn1);

    $w(ddn1).onChange(async()=>{
        let value1 = String($w(ddn1).value);
        let itemData = await get_ItemData2(value1); 
        create_UniqueDropdown(itemData, DB_FIELD2, ddn2); 
        console.log("xxx", itemData);
    }); 
    $w(ddn2).onChange(async()=>{
        let value1 = String($w(ddn1).value);
        let value2 = String($w(ddn2).value);
        let items = await startSearchEngine(value1, value2);
        console.log("HURAY --> !!! " + items);
        //------------------------------------
        $w('#myRepeaterID').data = items;
    }); 

    $w('#myRepeaterID').onItemReady(($item, itemData, index)=>{
        //IMAGE inside REPEATER
        $item('#elementIDwhichIsInsideRepeaterHere').src = itemData['datafieldID'];
        //TEXT inside REPEATER
        $item('#elementIDwhichIsInsideRepeaterHere').text = itemData['datafieldID'];
    });
});

function get_ItemData1() {
    let query = wixData.query(DATABASE)
    query.limit(1000)
    return query.find()
    .then((res)=> {console.log(res);
        console.log(res.totalCount)
        if (res.totalCount>0) {
            let itemData = res.items; 
            return itemData;
        }
        else {console.log("No DB-Entries found!");}
    }).catch((err)=>{console.log(err);});
}

function get_ItemData2(value1) {
    return  wixData.query(DATABASE)
    .limit(1000)
    .eq(DB_FIELD1, value1)
    .find()
    .then((res)=> {console.log(res);
        console.log(res.totalCount)
        if (res.totalCount>0) {
            let itemData = res.items; 
            return itemData;
        }
        else {console.log("No DB-Entries found!");}
    }).catch((err)=>{console.log(err);});
}

function create_UniqueDropdown(items, dbfield, dropdown) { 
    const uniqueTitles = getUniqueTitles(items); 
    $w(dropdown).options = buildOptions(uniqueTitles); 
    function getUniqueTitles(items) {
        const titlesOnly = items.map(item => item[dbfield]); 
        return [...new Set(titlesOnly)];
    }
    function buildOptions(uniqueList) {
        return uniqueList.map(curr => {
            return {
                label:curr, value:curr
            };
        });
    }
}

function startSearchEngine(value1, value2) {
    wixData.query(DATABASE)
    .limit(1000)
    .contains(DB_FIELD1, value1)
    .contains(DB_FIELD2, value2)
    //.ascending("_random")
    .find()
    .then((res)=>{console.log("My found RESULTS: ", res);
        if (res.totalCount>0) {let items = res.items;
            console.log("Items: ", items);
            $w('#nbrpraticiens').text = res.totalCount;
            return items;
        }
        else {console.log("No RESULTS found!!!");}
    })
    .catch((err)=>{console.log("We have a problem ! ---> " + err);});
}

All your other functions and features you will have to code, if you want to have some more functionality.

Improve the code… good luck!:wink:

Some similar issues, but solved different… (in parallel running post)…
https://community.wix.com/velo/forum/coding-with-velo/auto-filling-input-fields-from-collection-drop-down-menu