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…
- 2x Dropdowns (“dropdownReg” + “dropdownDep”)
- 1x Your database —> “Listepraticiens”
- 2x The database-fields you are using for your dropdowns (“region”/“department”)
- 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…