I have a data collection that has fields referenced in another collection, I would like to filter the pro gallery using multiple dropdowns, I need the user to choose Work, Year, Month and Week from the dropdowns, the problem happens when I have, for example, Works 1 and 2 that have data from the same month. In this situation the filter stops working. Below is my code
import wixData from ‘wix-data’;
$w.onReady(function () {
listarSemana("Imagens", "ano", $w('#ddAno'), "Todos")
listarSemana("Imagens", "mes", $w('#ddMes'),"Todos")
listarSemana("Imagens", "semana", $w('#ddSemana'), "Todos")
listarSemana("Obras", "nome_obra", $w('#ddObra'), "Todos")
$w('#ddAno, #ddMes, #ddSemana').onChange(()=>{
filtrarLista()
})
});
function listarSemana(collection, campo, $dropdown, valorInicial){
wixData.aggregate(collection)
.ascending(campo)
.group(campo)
.run()
.then((result) => {
let op = [{value: "todos", label: valorInicial}]
result.items.map((item) => {
op.push({ value: item[campo], label: item[campo] })
})
$dropdown.options = op
$dropdown.value = op[0].value
})
}
function filtrarLista(){
let obraFilter = $w('#ddObra').value === "Todos" ? wixData.filter().isNotEmpty("reference") : wixData.filter().eq("reference", $w('#ddObra').value)
let anoFilter = $w('#ddAno').value === "Todos" ? wixData.filter().isNotEmpty("ano") : wixData.filter().eq("ano", $w('#ddAno').value)
let mesFilter = $w('#ddMes').value === "Todos" ? wixData.filter().isNotEmpty("mes") : wixData.filter().eq("mes", $w('#ddMes').value)
let semanaFilter = $w('#ddSemana').value === "Todos" ? wixData.filter().isNotEmpty("semana") : wixData.filter().eq("semana", $w('#ddSemana').value)
$w('#dataset1').setFilter((anoFilter).and(mesFilter).and(semanaFilter))
}