hi,
I created a database with links to others database. Now I want to filter it and see only that I want.
I have this code below and my problem is that we can’t see the label in the dropdown (at the end of the buildOptions function). Is ok if I put two time “curr” but it’s the ID and I would want to see the name (field title of the database Autres_Escapes_Organisme).
Somebody can help me?
import wixData from "wix-data";
let lastFilterOrganisme;
let lastFilterLieu;
let title;
$w.onReady(() => {
// query retourne toutes les valeurs des items
wixData.query("Autres_Escapes")
// Limite maximale d'items
.limit(1000)
.ascending('Organisme')
.find()
.then(results => {
// Appelle la function qui crée une liste unique
const uniqueOrga = getUniqueOrga(results.items);
// Apelle la function qui construit la liste DropDown
$w("#iOrganisme").options =buildOptions(uniqueOrga);
});
// Builds an array from the "Title" field only from each item in
// the collection and then removes the duplicates
function getUniqueOrga(items) {
// Use the map method to create the titlesOnly object containing all the titles from the query results
const titlesOnly = items.map(item => item.organisme);
// Return an array with a list of unique titles
return [...new Set(titlesOnly)];
}
// Creates an array of objects in the form {label: "label", value: "value"} from the array of titles
function buildOptions(uniqueList) {
return uniqueList.map(curr => {
// Use the map method to build the options list in the format {label:uniqueTitle, value:uniqueTitle}
//Début TEST Récupération du nom avec l'ID
wixData.query("Autres_Escapes_Organisme") // get the item from the database collection.
.eq("_id", curr)
.ascending('Organisme')
.find()
.then((res) => {
const saveit = res.items[0];
title = saveit.title
console.log(title);//Affiche le résultat dans la console
console.log(curr);//Affiche l'ID de la valeur
});
// Fin test
return {label: title, value: curr};
});
}
});
export function iOrganisme_change() {
filter($w('#iOrganisme').value, lastFilterLieu);
}
export function iLieu_change() {
filter(lastFilterOrganisme, $w('#iLieu').value);
}
function filter(organisme, lieu){
if (lastFilterOrganisme !== organisme || lastFilterLieu !== lieu) {
let newFilter = wixData.filter();
if (organisme)
newFilter = newFilter.eq('organisme', organisme);
if (lieu)
newFilter = newFilter.eq('lieu', lieu);
$w('#dataset1').setFilter(newFilter);
lastFilterOrganisme = organisme;
lastFilterLieu = lieu;
}
}