Hi,
- I was wondering how do i filter the database by level then subject then location. Is there a .then() function that I can use? Currently, I am filtering the database by all at once and .contains(). Therefore, the filtering is not really accurate.
- Secondly, how do i create a view all button as the #repeater2 i have created for the view all only display 20 items.
Here are my lines of code.
import wixData from “wix-data”;
import wixLocation from ‘wix-location’;
$w.onReady(() => {
$w(’ #repeater1 ‘).collapse();
$w(’ #repeater2 ‘).collapse();
loadLevel();
loadLocation();
});
let lastFilterLevel;
let lastFilterSubject;
let lastFilterLocation;
export function iButton_click(event, $w) {
if ($w(’ #iLevel ‘).value === “Primary 1” || $w(’ #iLevel ‘).value === “Primary 2” || $w(’ #iLevel ‘).value === “Primary 3” || $w(’ #iLevel ‘).value === “Primary 4”)
{
$w(’ #iLevel ‘).value = “Primary (P1~P4)”;
}
else if ($w(’ #iLevel ‘).value === “Primary 5” || $w(’ #iLevel ‘).value === “Primary 6”) {
$w(’ #iLevel ‘).value = “PSLE (P5~P6)”;
}
else if ($w(’ #iLevel ‘).value === “Secondary 1” || $w(’ #iLevel ‘).value === “Secondary 2”) {
$w(’ #iLevel ‘).value = “Secondary (Sec1~Sec2)”;
}
else if ($w(’ #iLevel ‘).value === “Secondary 3” || $w(’ #iLevel ‘).value === “Secondary 4” || $w(’ #iLevel ‘).value === “Secondary 5” || $w(’ #iLevel ‘).value === “IGCSE”) {
$w(’ #iLevel ‘).value = “Secondary (Sec3~Sec5)”;
}
else if ($w(’ #iLevel ‘).value === “Junior College Year 1” || $w(’ #iLevel ‘).value === “Junior College Year 2”) {
$w(’ #iLevel ‘).value = “A Level (JC1~JC2)”;
} else if ($w(’ #iLevel ‘).value === “IB”) {
$w(’ #iLevel ‘).value = “IB”;
}
if ($w(’ #iLevel ‘).value === “All Levels”) {
$w(’ #iLevel ‘).value = “”;
}
if ($w(’ #iSubject ‘).value === “All Subjects”) {
$w(’ #iSubject ‘).value = “”;
}
if ($w(’ #iLocation ‘).value === “All Locations”) {
$w(’ #iLocation ‘).value = “”;
}
filter($w(’ #iLevel ‘).value, $w(’ #iSubject ‘).value, $w(’ #iLocation ‘).value);
if ($w(’ #iLevel ‘).value === “PSLE (P1~P6)” || $w(’ #iLevel ‘).value === “Secondary (Sec1~Sec2)” || $w(’ #iLevel ‘).value === “Secondary (Sec3~Sec5)” || $w(’ #iLevel ‘).value === “A Level (JC1~JC2)” || $w(’ #iLevel ‘).value === “IB”) {
$w(’ #iLevel ‘).value = “”;
$w(’ #iSubject ‘).value = “”;
$w(’ #iSubject ‘).disable();
}
setTimeout(function() {
$w(’ #text31 ‘).hide();
$w(’ #repeater1 ‘).expand();
}, 1000);
}
function filter(Level, Subject, Location){
if(lastFilterLevel !== Level || lastFilterSubject !== Subject || lastFilterLocation !== Location) {
let newFilter = wixData.filter();
if(Level)
newFilter = newFilter.contains(‘level’, Level)
if(Subject)
newFilter = newFilter.contains(‘subject’, Subject)
if(Location)
newFilter = newFilter.contains(‘location’, Location);
$w(’ #dataset1 ‘).setFilter(newFilter);
lastFilterLevel = Level;
lastFilterSubject = Subject;
lastFilterLocation = Location;
}
}
function loadPriSubjects(){
wixData.query(‘primarySubjects’)
.ascending(‘title’)
.find()
.then(res => {
let options = [{“value”: ‘’, “label”: ‘All Subjects’}];
options.push(…res.items.map(subject => {
return {“value”: subject.title, “label”: subject.title};
}));
$w(’ #iSubject ‘).options = options;
});
}
function loadSecSubjects(){
wixData.query(‘secondarySubjects’)
.ascending(‘title’)
.find()
.then(res => {
let options = [{“value”: ‘’, “label”: ‘All Subjects’}];
options.push(…res.items.map(subject => {
return {“value”: subject.title, “label”: subject.title};
}));
$w(’ #iSubject ‘).options = options;
});
}
function loadSecSubjects1(){
wixData.query(‘secondarySubjects1’)
.ascending(‘title’)
.find()
.then(res => {
let options = [{“value”: ‘’, “label”: ‘All Subjects’}];
options.push(…res.items.map(subject => {
return {“value”: subject.title, “label”: subject.title};
}));
$w(’ #iSubject ‘).options = options;
});
}
function loadJcSubjects(){
wixData.query(‘jcSubjects’)
.ascending(‘title’)
.find()
.then(res => {
let options = [{“value”: ‘’, “label”: ‘All Subjects’}];
options.push(…res.items.map(subject => {
return {“value”: subject.title, “label”: subject.title};
}));
$w(’ #iSubject ‘).options = options;
});
}
function loadSubjects(){
wixData.query(‘Subjects’)
.ascending(‘title’)
.find()
.then(res => {
let options = [{“value”: ‘All Subjects’, “label”: ‘All Subjects’}];
options.push(…res.items.map(subject => {
return {“value”: subject.title, “label”: subject.title};
}));
$w(’ #iSubject ‘).options = options;
});
}
function loadLevel() {
wixData.query(‘AcademicLevel’)
.ascending(‘title’)
.find()
.then(res => {
let options= [{“value”: ‘All Levels’, “label”: ‘All Levels’}];
options.push(…res.items.map(level => {
return {“value”: level.title, “label”: level.title};
}));
$w(’ #iLevel ‘).options = options;
});
}
function loadLocation() {
wixData.query(‘Location’)
.ascending(‘title’)
.find()
.then(res => {
let options= [{“value”: ‘All Locations’, “label”: ‘All Locations’}];
options.push(…res.items.map(location => {
return {“value”: location.title, “label”: location.title};
}));
$w(’ #iLocation ‘).options = options;
});
}
export function iView_click(event, $w) {
wixLocation.to(’/tutor/’ + $w(" #dataset1 “).getCurrentItem()._id)
}
export function iLevel_change(event, $w) {
if ($w(’ #iLevel ‘).value === “Primary 1” || $w(’ #iLevel ‘).value === “Primary 2” || $w(’ #iLevel ‘).value === “Primary 3” || $w(’ #iLevel ‘).value === “Primary 4” || $w(’ #iLevel ‘).value === “Primary 5” || $w(’ #iLevel ‘).value === “Primary 6”)
{
$w(’ #iSubject ‘).options = loadPriSubjects();
$w(’ #iSubject ‘).enable();
}
else if ($w(’ #iLevel ‘).value === “Secondary 1” || $w(’ #iLevel ‘).value === “Secondary 2”) {
$w(’ #iSubject ‘).options = loadSecSubjects();
$w(’ #iSubject ‘).enable();
}
else if ($w(’ #iLevel ‘).value === “Secondary 3” || $w(’ #iLevel ‘).value === “Secondary 4” || $w(’ #iLevel ‘).value === “Secondary 5” || $w(’ #iLevel ‘).value === “IGCSE”) {
$w(’ #iSubject ‘).options = loadSecSubjects1();
$w(’ #iSubject ‘).enable();
}
else if ($w(’ #iLevel ‘).value === “Junior College Year 1” || $w(’ #iLevel ‘).value === “Junior College Year 2” || $w(’ #iLevel ‘).value === “IB”) {
$w(’ #iSubject ‘).options = loadJcSubjects();
$w(’ #iSubject ‘).enable();
}
else if($w(’ #iLevel ‘).value === “All Levels”) {
$w(’ #iSubject ‘).options = loadSubjects();
$w(’ #iSubject ‘).enable();
}
else {
$w(’ #iLevel ‘).value = ‘’;
$w(’ #iSubject ').disable();
}
setTimeout(function() {
$w(” #iSubject ").enable();
}, 1000);
}
export function iViewAll_click(event, $w) {
$w(’ #text31 ‘).hide();
$w(’ #repeater2 ').expand();
}
Here is the database which is used for the repeater.