[resolved] How filter data on 2 or 3 collections

Hello, I used the code proposed in the example https://www.wix.com/corvid/example/mega-search
, where a repeater is fed from the result of a query

I use a primary collection “Events” and 2 secondary collection “compagnie” and 'salleLieu"

Repeater uses data and it is displayed correctly of the data coming from 3 collections

The user can filter the results, and there are no problems when the filter uses data from the primary collection, but I don’t know how to filter based on secondary data ?

Below, the code (simplified) that i used :

import wixData from 'wix-data';
import { debounce } from 'lodash';

$w.onReady(
 function () {
        initRepeater();
        buildFiltersAndPopulateRepeater();  
    }
);

async function buildFiltersAndPopulateRepeater() {
 let dataQuery = wixData.query('Events').include('compagnie').include('salleLieu').limit(200);

 // if #isalle is validated, this code works, and query is correctly filtered : 
 // NameField is a field of the collection Events
 if ($w('#iSalle').value) {
        dataQuery = dataQuery.eq('NameField', $w('#iSalle').value);
    }
// if #iDepartment is validated, this code does not work, and query is empty :
// departement is a field of the collection salleLieu
 if ($w('#iDepartment').value) {
        dataQuery = dataQuery.eq('salleLieu.departement', $w('#Department').value);
    }

// this code and the rest is good
 if ($w('#iTri').value) {
        dataQuery = dataQuery.ascending($w('#iTri').value) ;
    }

    $w('#repeaterEvenement').data = await dataQuery.find().then((res) => res.items);
}

function initRepeater() {
        // field of Events :
        $item('#txtDate').text = itemData.date ;          
        $item('#btnLieu').label = itemData.salleLieu.title;
        $item('#txtTitre').text = itemData.title;
        $item('#txtSousTitre').text = itemData.infoSpcialeSoustitre;
        $item('#txtDetail').text = itemData.dtail;
        // fields of salleLieu :
        if (itemData.salleLieu) {
            $item('#btnLieu').link = itemData.salleLieu['link-salles-lieux-title'];
            $item('#txtDeptVille').text = itemData.salleLieu.departement;
        }    
        // fields of compagnie
        if (itemData.compagnie) {
            $item('#btnCompagnie').label = itemData.compagnie.title;
            $item('#btnCompagnie').link = itemData.compagnie['link-compagnies-1-title'] ;
            $item('#btnCompagnie').style.color = "#0000FF" ;
        }else{
            $item('#btnCompagnie').label = "?";
        }           
     });
}

have you ever had this experience? Did you find a solution ?

https://www.wix.com/corvid/reference/wix-data/wixdatafilter/and

hello,
thanks for this url, but I have looked at how to use and () but it is noted “The collections referenced by both the initial query and the query passed to the and function must be the same.”, while in my case, there is events collection and salleLieu collection. I dont know ?

@bjar
What if you put all the 3 single-collections into ONE-BIG-BUNDLE?

—> Array[] ???

And then Filtering ?

(Dit moi, quand to a trouver ton solution) :wink:

I find solution

i replace this :

$w('#repeaterEvenement').data = await dataQuery.find().then((res) => res.items);

by this :

resultat = await dataQuery.find().then((res) => res.items);

AfficheSalle = $w('#iFiltreSalle').value ;
AfficheDept = $w('#iFiltreDepartement').value.trim() ; 	
AfficheTri = $w('#iTri').value ;

// si tri par date ou par salleLieu ?

if (AfficheTri==='date'){
	resultat.sort(function(a, b){return a.date - b.date});
} else {
	resultat.sort(function(a, b){
	var x = a.salleLieu.title.toLowerCase()+a.date.toISOString() ;
     	var y = b.salleLieu.title.toLowerCase()+b.date.toISOString() ;
     	return x.localeCompare(y) ; 
   	});
}

// si filtre sur salleLieu.département ?

if (AfficheDept) {
  resultat2 = resultat.filter( res => res.salleLieu.departement === AfficheDept.trim()) ;
} 

// si filtre par salleLieu.title ?

if (AfficheSalle) {
  resultat2 = resultat.filter( res => res.salleLieu.title === AfficheSalle) ;
} else {
  resultat2 = resultat ;
}


$w('#repeaterEvenement').data = resultat2 ;

you can see the result here: https://www.dnc44.fr/spectacles-de-danse

Looks good. Well done!